Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Remove Container dependency on Illuminate\Support #30518

Merged
merged 2 commits into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Remove Container dependency on Illuminate\Support
For standalone projects, pulling in all of Illuminate\Support is a bit
overkill, given that we're using only a couple of functions from the
package. This commit opts to duplicate the two helper functions inside
the container namespace (so they'll be picked up by a subtree split),
which also removes reliance on any custom global functions.

This goes beyond effectively reverting #29959, as the container used
Arr::wrap() before adding in the value() helper.
  • Loading branch information
iansltx committed Nov 5, 2019
commit 534990ba4fc260bdf9df022d2a554d07c8131528
4 changes: 2 additions & 2 deletions src/Illuminate/Container/BoundMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected static function callClass($container, $target, array $parameters = [],
protected static function callBoundMethod($container, $callback, $default)
{
if (! is_array($callback)) {
return value($default);
return Util::unwrapIfClosure($default);
}

// Here we need to turn the array callable into a Class@method string we can use to
Expand All @@ -87,7 +87,7 @@ protected static function callBoundMethod($container, $callback, $default)
return $container->callMethodBinding($method, $callback[0]);
}

return value($default);
return Util::unwrapIfClosure($default);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Exception;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Container\Container as ContainerContract;
use Illuminate\Support\Arr;
use LogicException;
use ReflectionClass;
use ReflectionException;
Expand Down Expand Up @@ -144,7 +143,7 @@ public function when($concrete)
{
$aliases = [];

foreach (Arr::wrap($concrete) as $c) {
foreach (Util::arrayWrap($concrete) as $c) {
$aliases[] = $this->getAlias($c);
}

Expand Down
3 changes: 1 addition & 2 deletions src/Illuminate/Container/ContextualBindingBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Container\ContextualBindingBuilder as ContextualBindingBuilderContract;
use Illuminate\Support\Arr;

class ContextualBindingBuilder implements ContextualBindingBuilderContract
{
Expand Down Expand Up @@ -63,7 +62,7 @@ public function needs($abstract)
*/
public function give($implementation)
{
foreach (Arr::wrap($this->concrete) as $concrete) {
foreach (Util::arrayWrap($this->concrete) as $concrete) {
$this->container->addContextualBinding($concrete, $this->needs, $implementation);
}
}
Expand Down
38 changes: 38 additions & 0 deletions src/Illuminate/Container/Util.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Illuminate\Container;

// Utility functions pulled from Illuminate\Support to remove the need to pull in all of Support
// when using the container standalone.
class Util
{
/**
* If the given value is not an array and not null, wrap it in one.
*
* From Arr::wrap() in Illuminate\Support.
*
* @param mixed $value
* @return array
*/
public static function arrayWrap($value)
{
if (is_null($value)) {
return [];
}

return is_array($value) ? $value : [$value];
}

/**
* Return the default value of the given value.
*
* From global value() helper in Illuminate\Support.
*
* @param mixed $value
* @return mixed
*/
public static function unwrapIfClosure($value)
{
return $value instanceof \Closure ? $value() : $value;
}
}
1 change: 0 additions & 1 deletion src/Illuminate/Container/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"require": {
"php": "^7.2",
"illuminate/contracts": "^6.0",
"illuminate/support": "^6.0",
"psr/container": "^1.0"
},
"autoload": {
Expand Down
42 changes: 42 additions & 0 deletions tests/Container/UtilTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Illuminate\Tests\Container;

use Illuminate\Container\Util;
use PHPUnit\Framework\TestCase;

class UtilTest extends TestCase
{
public function testUnwrapIfClosure()
{
$this->assertSame('foo', Util::unwrapIfClosure('foo'));
$this->assertSame('foo', UtiL::unwrapIfClosure(function () {
Copy link
Contributor

@paulredmond paulredmond Nov 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iansltx I noticed a capital L on this line.

Actual: UtiL::unwrapIfClosure
Expected: Util::unwrapIfClosure

// @driesvints

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops.

Mind PR'ing a typo fix here, as this has already been merged?

return 'foo';
}));
}

public function testArrayWrap()
{
$string = 'a';
$array = ['a'];
$object = new \stdClass;
$object->value = 'a';
$this->assertEquals(['a'], Util::arrayWrap($string));
$this->assertEquals($array, Util::arrayWrap($array));
$this->assertEquals([$object], Util::arrayWrap($object));
$this->assertEquals([], Util::arrayWrap(null));
$this->assertEquals([null], Util::arrayWrap([null]));
$this->assertEquals([null, null], Util::arrayWrap([null, null]));
$this->assertEquals([''], Util::arrayWrap(''));
$this->assertEquals([''], Util::arrayWrap(['']));
$this->assertEquals([false], Util::arrayWrap(false));
$this->assertEquals([false], Util::arrayWrap([false]));
$this->assertEquals([0], Util::arrayWrap(0));

$obj = new \stdClass;
$obj->value = 'a';
$obj = unserialize(serialize($obj));
$this->assertEquals([$obj], Util::arrayWrap($obj));
$this->assertSame($obj, Util::arrayWrap($obj)[0]);
}
}