From 1a1969b6e6f793c3b2a479362641487ee9cbf736 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 22 Dec 2016 15:50:57 -0600 Subject: [PATCH] Working on container. Removing some old methods such as share() in favor of the more common and documented singleton(). --- src/Illuminate/Container/Container.php | 66 ++----------------- .../Container/ContextualBindingBuilder.php | 4 +- .../Routing/RoutingServiceProvider.php | 6 +- tests/Container/ContainerTest.php | 21 +----- 4 files changed, 14 insertions(+), 83 deletions(-) diff --git a/src/Illuminate/Container/Container.php b/src/Illuminate/Container/Container.php index e34a352461e1..68c1fb6d4d32 100755 --- a/src/Illuminate/Container/Container.php +++ b/src/Illuminate/Container/Container.php @@ -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); } /** @@ -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]); } /** @@ -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. @@ -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. * @@ -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)) { - 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); } } @@ -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. * diff --git a/src/Illuminate/Container/ContextualBindingBuilder.php b/src/Illuminate/Container/ContextualBindingBuilder.php index fc5cd611d08e..58b70cee4bda 100644 --- a/src/Illuminate/Container/ContextualBindingBuilder.php +++ b/src/Illuminate/Container/ContextualBindingBuilder.php @@ -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 + ); } } diff --git a/src/Illuminate/Routing/RoutingServiceProvider.php b/src/Illuminate/Routing/RoutingServiceProvider.php index e8e7bfb30677..49a8d5d79293 100755 --- a/src/Illuminate/Routing/RoutingServiceProvider.php +++ b/src/Illuminate/Routing/RoutingServiceProvider.php @@ -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); }); } @@ -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. @@ -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 diff --git a/tests/Container/ContainerTest.php b/tests/Container/ContainerTest.php index af6e9fdddda9..20a34ea3b5c6 100755 --- a/tests/Container/ContainerTest.php +++ b/tests/Container/ContainerTest.php @@ -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() @@ -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) {