FOF30\Pimple\Container::extend PHP Method

extend() public method

Useful when you want to extend an existing object definition, without necessarily loading that object.
public extend ( string $id, callable $callable ) : callable
$id string The unique identifier for the object
$callable callable A service definition to extend the original
return callable The wrapped callable
    public function extend($id, $callable)
    {
        if (!isset($this->keys[$id])) {
            throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
        }
        if (!is_object($this->values[$id]) || !method_exists($this->values[$id], '__invoke')) {
            throw new \InvalidArgumentException(sprintf('Identifier "%s" does not contain an object definition.', $id));
        }
        if (!is_object($callable) || !method_exists($callable, '__invoke')) {
            throw new \InvalidArgumentException('Extension service definition is not a Closure or invokable object.');
        }
        $factory = $this->values[$id];
        $extended = function ($c) use($callable, $factory) {
            return $callable($factory($c), $c);
        };
        if (isset($this->factories[$factory])) {
            $this->factories->detach($factory);
            $this->factories->attach($extended);
        }
        return $this[$id] = $extended;
    }