Source for file Container.php
Documentation is available at Container.php
* Part of the Joomla Framework DI Package
* @copyright Copyright (C) 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* Holds the shared instances.
* Holds the keys, their callbacks, and whether or not
* the item is meant to be a shared resource.
* Parent for hierarchical containers.
* Constructor for the DI Container
* @param Container $parent Parent for hierarchical containers.
* Create an alias for a given key for easy access.
* @param string $alias The alias name
* @param string $key The key to alias
public function alias($alias, $key)
* Search the aliases property for a matching alias key.
* @param string $key The key to search for.
* Build an object of class $key;
* @param string $key The class name to build.
* @param boolean $shared True to create a shared resource.
* @return mixed Instance of class specified by $key with all dependencies injected.
* Returns an object if the class exists and false otherwise
$reflection =
new \
ReflectionClass($key);
catch
(\
ReflectionException $e)
$constructor =
$reflection->getConstructor();
// If there are no parameters, just return a new object.
$callback =
function () use
($key) {
// Create a callable for the dataStore
$callback =
function () use
($reflection, $newInstanceArgs) {
return $reflection->newInstanceArgs($newInstanceArgs);
return $this->set($key, $callback, $shared)->get($key);
* Convenience method for building a shared object.
* @param string $key The class name to build.
* @return object Instance of class specified by $key with all dependencies injected.
* Create a child Container with a new property scope that
* that has the ability to access the parent scope when resolving.
return new static($this);
* Extend a defined service Closure by wrapping the existing one with a new Closure. This
* works very similar to a decorator pattern. Note that this only works on service Closures
* that have been defined in the current Provider, not parent providers.
* @param string $key The unique identifier for the Closure or property.
* @param \Closure $callable A Closure to wrap the original service Closure.
* @throws \InvalidArgumentException
public function extend($key, \
Closure $callable)
throw
new \
InvalidArgumentException(sprintf('The requested key %s does not exist to extend.', $key));
$closure =
function ($c) use
($callable, $raw) {
return $callable($raw['callback']($c), $c);
$this->set($key, $closure, $raw['shared']);
* Build an array of constructor parameters.
* @param \ReflectionMethod $method Method for which to build the argument array.
* @return array Array of arguments to pass to the method.
* @throws DependencyResolutionException
foreach ($method->getParameters() as $param)
$dependency =
$param->getClass();
$dependencyVarName =
$param->getName();
// If we have a dependency, that means it has been type-hinted.
$dependencyClassName =
$dependency->getName();
// If the dependency class name is registered with this container or a parent, use it.
if ($this->getRaw($dependencyClassName) !==
null)
$depObject =
$this->get($dependencyClassName);
if ($depObject instanceof
$dependencyClassName)
$methodArgs[] =
$depObject;
// Finally, if there is a default parameter, use it.
if ($param->isOptional())
$methodArgs[] =
$param->getDefaultValue();
// Couldn't resolve dependency, and no default was provided.
* Method to set the key and callback to the dataStore array.
* @param string $key Name of dataStore key to set.
* @param mixed $value Callable function to run or string to retrive when requesting the specified $key.
* @param boolean $shared True to create and store a shared instance.
* @param boolean $protected True to protect this item from being overwritten. Useful for services.
* @return \Joomla\DI\Container This instance to support chaining.
* @throws \OutOfBoundsException Thrown if the provided key is already set and is protected.
public function set($key, $value, $shared =
false, $protected =
false)
throw
new \
OutOfBoundsException(sprintf('Key %s is protected and can\'t be overwritten.', $key));
// If the provided $value is not a closure, make it one now for easy resolution.
if (!($value instanceof \
Closure))
$value =
function () use
($value) {
'protected' =>
$protected
* Convenience method for creating protected keys.
* @param string $key Name of dataStore key to set.
* @param callable $callback Callable function to run when requesting the specified $key.
* @param bool $shared True to create and store a shared instance.
* @return \Joomla\DI\Container This instance to support chaining.
public function protect($key, $callback, $shared =
false)
return $this->set($key, $callback, $shared, true);
* Convenience method for creating shared keys.
* @param string $key Name of dataStore key to set.
* @param callable $callback Callable function to run when requesting the specified $key.
* @param bool $protected True to create and store a shared instance.
* @return \Joomla\DI\Container This instance to support chaining.
public function share($key, $callback, $protected =
false)
return $this->set($key, $callback, true, $protected);
* Method to retrieve the results of running the $callback for the specified $key;
* @param string $key Name of the dataStore key to get.
* @param boolean $forceNew True to force creation and return of a new instance.
* @return mixed Results of running the $callback for the specified $key.
* @throws \InvalidArgumentException
public function get($key, $forceNew =
false)
throw
new \
InvalidArgumentException(sprintf('Key %s has not been registered with the container.', $key));
if (!isset
($this->instances[$key]) ||
$forceNew)
$this->instances[$key] =
$raw['callback']($this);
return $raw['callback']($this);
* Get the raw data assigned to a key.
* @param string $key The key for which to get the stored item.
protected function getRaw($key)
* Method to force the container to return a new instance
* of the results of the callback for requested $key.
* @param string $key Name of the dataStore key to get.
* @return mixed Results of running the $callback for the specified $key.
return $this->get($key, true);
* Register a service provider to the container.
* @param ServiceProviderInterface $provider The service provider to register.w
* @return Container This object for chaining.
$provider->register($this);
Documentation generated on Tue, 19 Nov 2013 14:56:48 +0100 by phpDocumentor 1.4.3