Skip to content

Commit

Permalink
Working on container.
Browse files Browse the repository at this point in the history
Removing some old methods such as share() in favor of the more common
and documented singleton().
  • Loading branch information
taylorotwell committed Dec 22, 2016
1 parent 3be65cc commit 1a1969b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 83 deletions.
66 changes: 7 additions & 59 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ public function bound($abstract)
{
$abstract = $this->normalize($abstract);

return isset($this->bindings[$abstract]) || isset($this->instances[$abstract]) || $this->isAlias($abstract);
return isset($this->bindings[$abstract]) ||
isset($this->instances[$abstract]) ||
$this->isAlias($abstract);
}

/**
Expand All @@ -160,7 +162,8 @@ public function resolved($abstract)
$abstract = $this->getAlias($abstract);
}

return isset($this->resolved[$abstract]) || isset($this->instances[$abstract]);
return isset($this->resolved[$abstract]) ||
isset($this->instances[$abstract]);
}

/**
Expand Down Expand Up @@ -188,15 +191,6 @@ public function bind($abstract, $concrete = null, $shared = false)

$concrete = $this->normalize($concrete);

// If the given types are actually an array, we will assume an alias is being
// defined and will grab this "real" abstract class name and register this
// alias with the container so that it can be used as a shortcut for it.
if (is_array($abstract)) {
list($abstract, $alias) = $this->extractAlias($abstract);

$this->alias($abstract, $alias);
}

// If no concrete type was given, we will simply set the concrete type to the
// abstract type. After that, the concrete type to be registered as shared
// without being forced to state their classes in both of the parameters.
Expand Down Expand Up @@ -291,28 +285,6 @@ public function singleton($abstract, $concrete = null)
$this->bind($abstract, $concrete, true);
}

/**
* Wrap a Closure such that it is shared.
*
* @param \Closure $closure
* @return \Closure
*/
public function share(Closure $closure)
{
return function ($container) use ($closure) {
// We'll simply declare a static variable within the Closures and if it has
// not been set we will execute the given Closures to resolve this value
// and return it back to these consumers of the method as an instance.
static $object;

if (is_null($object)) {
$object = $closure($container);
}

return $object;
};
}

/**
* "Extend" an abstract type in the container.
*
Expand Down Expand Up @@ -344,27 +316,14 @@ public function extend($abstract, Closure $closure)
*/
public function instance($abstract, $instance)
{
$abstract = $this->normalize($abstract);

// First, we will extract the alias from the abstract if it is an array so we
// are using the correct name when binding the type. If we get an alias it
// will be registered with the container so we can resolve it out later.
if (is_array($abstract)) {

This comment has been minimized.

Copy link
@neyaoz

neyaoz Jan 2, 2017

Contributor

@taylorotwell The users like me who are using $abstract argument as an array, going to start to get an error? What is the reason of deleting this statement? Btw, I am going to update my service providers if it is deprecated.

This comment has been minimized.

Copy link
@GrahamCampbell

GrahamCampbell Jan 2, 2017

Member

Removed feature because it's already possible by calling the alias method. I've also never seen anyone use that array functionality before. Not sure if we ever documented it?

This comment has been minimized.

Copy link
@neyaoz

neyaoz Jan 3, 2017

Contributor

You have never, but I am always in core while working Laravel to figureout what is in my hands. Because you can not documente all, right? Laravel has bigger potential in core.

Yes, I was using sometimes to define abstract and it's alias in the same argument,
$this->app->instance([\Foo\Bar\Zoo::class, 'zoo']);

But of course it is not neccessary as you see, I am just wondered.

This comment has been minimized.

Copy link
@jralph

jralph Feb 7, 2017

I came across this breaking some code of mine the other day and took me a while to figure out as the error message that gets thrown is very vague.

It's not in the documentation on the laravel website, but it was commented in the code so you can assume people have read the comment and used the functionality.

See ticket: #17793

This comment has been minimized.

Copy link
@apollopy

apollopy Feb 28, 2017

Contributor

the bind function comment: * @param string|array $abstract https://github.com/laravel/framework/blob/master/src/Illuminate/Container/Container.php#L194
If removed, please delete

This comment has been minimized.

Copy link
@MostafaEzzelden

MostafaEzzelden Sep 4, 2017

$this->app->singleton('foo', function($app) {
return ;
});

list($abstract, $alias) = $this->extractAlias($abstract);

$this->alias($abstract, $alias);
}

unset($this->aliases[$abstract]);
unset($this->aliases[$abstract = $this->normalize($abstract)]);

// We'll check to determine if this type has been bound before, and if it has
// we will fire the rebound callbacks registered with the container and it
// can be updated with consuming classes that have gotten resolved here.
$bound = $this->bound($abstract);

$this->instances[$abstract] = $instance;

if ($bound) {
if ($this->bound($abstract)) {
$this->rebound($abstract);
}
}
Expand Down Expand Up @@ -422,17 +381,6 @@ public function alias($abstract, $alias)
$this->aliases[$alias] = $this->normalize($abstract);
}

/**
* Extract the type and alias from a given definition.
*
* @param array $definition
* @return array
*/
protected function extractAlias(array $definition)
{
return [key($definition), current($definition)];
}

/**
* Bind a new callback to an abstract's rebind event.
*
Expand Down
4 changes: 3 additions & 1 deletion src/Illuminate/Container/ContextualBindingBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public function needs($abstract)
*/
public function give($implementation)
{
$this->container->addContextualBinding($this->concrete, $this->needs, $implementation);
$this->container->addContextualBinding(
$this->concrete, $this->needs, $implementation
);
}
}
6 changes: 3 additions & 3 deletions src/Illuminate/Routing/RoutingServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function register()
*/
protected function registerRouter()
{
$this->app['router'] = $this->app->share(function ($app) {
$this->app->singleton('router', function ($app) {
return new Router($app['events'], $app);
});
}
Expand All @@ -47,7 +47,7 @@ protected function registerRouter()
*/
protected function registerUrlGenerator()
{
$this->app['url'] = $this->app->share(function ($app) {
$this->app->singleton('url', function ($app) {
$routes = $app['router']->getRoutes();

// The URL generator needs the route collection that exists on the router.
Expand Down Expand Up @@ -95,7 +95,7 @@ protected function requestRebinder()
*/
protected function registerRedirector()
{
$this->app['redirect'] = $this->app->share(function ($app) {
$this->app->singleton('redirect', function ($app) {
$redirector = new Redirector($app['url']);

// If the session is set on the application instance, we'll inject it into
Expand Down
21 changes: 1 addition & 20 deletions tests/Container/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,25 +132,6 @@ public function testAliases()
$this->assertEquals('bar', $container->make('foo'));
$this->assertEquals('bar', $container->make('baz'));
$this->assertEquals('bar', $container->make('bat'));
$container->bind(['bam' => 'boom'], function () {
return 'pow';
});
$this->assertEquals('pow', $container->make('bam'));
$this->assertEquals('pow', $container->make('boom'));
$container->instance(['zoom' => 'zing'], 'wow');
$this->assertEquals('wow', $container->make('zoom'));
$this->assertEquals('wow', $container->make('zing'));
}

public function testShareMethod()
{
$container = new Container;
$closure = $container->share(function () {
return new stdClass;
});
$class1 = $closure($container);
$class2 = $closure($container);
$this->assertSame($class1, $class2);
}

public function testBindingsCanBeOverridden()
Expand All @@ -174,7 +155,7 @@ public function testExtendedBindings()

$container = new Container;

$container['foo'] = $container->share(function () {
$container->singleton('foo', function () {
return (object) ['name' => 'taylor'];
});
$container->extend('foo', function ($old, $container) {
Expand Down

0 comments on commit 1a1969b

Please sign in to comment.