FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fixing some formatting. · JavaDevl/framework@6547e64 · GitHub

Commit 6547e64

Browse files
committed
Fixing some formatting.
2 parents ca87778 + dfdee18 commit 6547e64

4 files changed

Lines changed: 48 additions & 53 deletions

File tree

‎src/Illuminate/Container/Container.php‎

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ class Container implements ArrayAccess, ContainerContract {
4545
*/
4646
protected $aliases = array();
4747

48+
/**
49+
* The extension closures for services.
50+
*
51+
* @var array
52+
*/
53+
protected $extenders = [];
54+
4855
/**
4956
* All of the registered tags.
5057
*
@@ -302,11 +309,6 @@ public function bindShared($abstract, Closure $closure)
302309
*/
303310
public function extend($abstract, Closure $closure)
304311
{
305-
if ( ! isset($this->bindings[$abstract]))
306-
{
307-
throw new \InvalidArgumentException("Type {$abstract} is not bound.");
308-
}
309-
310312
if (isset($this->instances[$abstract]))
311313
{
312314
$this->instances[$abstract] = $closure($this->instances[$abstract], $this);
@@ -315,32 +317,10 @@ public function extend($abstract, Closure $closure)
315317
}
316318
else
317319
{
318-
$extender = $this->getExtender($abstract, $closure);
319-
320-
$this->bind($abstract, $extender, $this->isShared($abstract));
320+
$this->extenders[$abstract][] = $closure;
321321
}
322322
}
323323

324-
/**
325-
* Get an extender Closure for resolving a type.
326-
*
327-
* @param string $abstract
328-
* @param \Closure $closure
329-
* @return \Closure
330-
*/
331-
protected function getExtender($abstract, Closure $closure)
332-
{
333-
// To "extend" a binding, we will grab the old "resolver" Closure and pass it
334-
// into a new one. The old resolver will be called first and the result is
335-
// handed off to the "new" resolver, along with this container instance.
336-
$resolver = $this->bindings[$abstract]['concrete'];
337-
338-
return function($container) use ($resolver, $closure)
339-
{
340-
return $closure($resolver($container), $container);
341-
};
342-
}
343-
344324
/**
345325
* Register an existing instance as shared in the container.
346326
*
@@ -672,6 +652,14 @@ public function make($abstract, $parameters = array())
672652
$object = $this->make($concrete, $parameters);
673653
}
674654

655+
// If we defined any extenders for this type, we'll need to spin through them
656+
// and apply them to the object being built. This allows for the extension
657+
// of services, such as changing configuration or decorating the object.
658+
foreach ($this->getExtenders($abstract) as $extender)
659+
{
660+
$object = $extender($object, $this);
661+
}
662+
675663
// If the requested type is registered as a singleton we'll want to cache off
676664
// the instances in "memory" so we can return it later without creating an
677665
// entirely new instance of an object on each subsequent request for it.
@@ -705,7 +693,8 @@ protected function getConcrete($abstract)
705693
// since the container should be able to resolve concretes automatically.
706694
if ( ! isset($this->bindings[$abstract]))
707695
{
708-
if ($this->missingLeadingSlash($abstract) && isset($this->bindings['\\'.$abstract]))
696+
if ($this->missingLeadingSlash($abstract) &&
697+
isset($this->bindings['\\'.$abstract]))
709698
{
710699
$abstract = '\\'.$abstract;
711700
}
@@ -741,6 +730,22 @@ protected function missingLeadingSlash($abstract)
741730
return is_string($abstract) && strpos($abstract, '\\') !== 0;
742731
}
743732

733+
/**
734+
* Get the extender callbacks for a given type.
735+
*
736+
* @param string $abstract
737+
* @return array
738+
*/
739+
protected function getExtenders($abstract)
740+
{
741+
if (isset($this->extenders[$abstract]))
742+
{
743+
return $this->extenders[$abstract];
744+
}
745+
746+
return [];
747+
}
748+
744749
/**
745750
* Instantiate a concrete instance of the given type.
746751
*

‎src/Illuminate/Foundation/Application.php‎

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -518,29 +518,6 @@ public function bound($abstract)
518518
return isset($this->deferredServices[$abstract]) || parent::bound($abstract);
519519
}
520520

521-
/**
522-
* "Extend" an abstract type in the container.
523-
*
524-
* (Overriding Container::extend)
525-
*
526-
* @param string $abstract
527-
* @param \Closure $closure
528-
* @return void
529-
*
530-
* @throws \InvalidArgumentException
531-
*/
532-
public function extend($abstract, Closure $closure)
533-
{
534-
$abstract = $this->getAlias($abstract);
535-
536-
if (isset($this->deferredServices[$abstract]))
537-
{
538-
$this->loadDeferredProvider($abstract);
539-
}
540-
541-
return parent::extend($abstract, $closure);
542-
}
543-
544521
/**
545522
* Determine if the application has booted.
546523
*

‎tests/Container/ContainerTest.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,19 @@ public function testExtendIsLazyInitialized()
214214
}
215215

216216

217+
public function testExtendCanBeCalledBeforeBind()
218+
{
219+
$container = new Container;
220+
$container->extend('foo', function($old, $container)
221+
{
222+
return $old.'bar';
223+
});
224+
$container['foo'] = 'foo';
225+
226+
$this->assertEquals('foobar', $container->make('foo'));
227+
}
228+
229+
217230
public function testParametersCanBePassedThroughToClosure()
218231
{
219232
$container = new Container;

‎tests/Foundation/FoundationApplicationTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function testDeferredServicesAreLazilyInitialized()
8686
$this->assertTrue($app->bound('foo'));
8787
$this->assertFalse(ApplicationDeferredServiceProviderStub::$initialized);
8888
$app->extend('foo', function($instance, $container) { return $instance.'bar'; });
89-
$this->assertTrue(ApplicationDeferredServiceProviderStub::$initialized);
89+
$this->assertFalse(ApplicationDeferredServiceProviderStub::$initialized);
9090
$this->assertEquals('foobar', $app->make('foo'));
9191
$this->assertTrue(ApplicationDeferredServiceProviderStub::$initialized);
9292
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL