Ejemplo n.º 1
0
 /**
  * Extends a service definition.
  *
  * @param string   $name
  * @param callable $callable
  * @param boolean  $strict
  *
  * @return callable
  *
  * @throws InvalidArgumentException
  */
 public function extend($name, $callable, $strict = true)
 {
     if (!$this->services->has($name)) {
         if ($strict) {
             throw new InvalidArgumentException(sprintf('Service "%s" is not defined.', $name));
         } else {
             return false;
         }
     }
     $factory = $this->services->get($name);
     if (!is_object($factory) || !method_exists($factory, '__invoke')) {
         throw new InvalidArgumentException(sprintf('Service "%s" does not contain an object definition.', $name));
     }
     if (!is_object($callable) || !method_exists($callable, '__invoke')) {
         throw new InvalidArgumentException('Extension service definition is not a Closure or invokable object.');
     }
     $extended = function ($c) use($callable, $factory) {
         /** @var Closure $factory */
         return $callable($factory($c), $c);
     };
     if ($this->factories->contains($factory)) {
         $this->factories->detach($factory);
         $this->factories->attach($extended);
     }
     $this->services->unlock($name);
     $this->services->set($name, $extended);
     return $this->services->get($name);
 }
Ejemplo n.º 2
0
 public function testContents()
 {
     $lockbox = new LockBox();
     $lockbox->set('item1', 'value1');
     $lockbox->set('item2', 'value2');
     $lockbox->set('item3', 'value3');
     $lockbox->set('item4', 'value4');
     $this->assertEquals(['item1', 'item2', 'item3', 'item4'], $lockbox->contents());
 }