* @author Bob Mckee <bmckee@bywires.com>
* @author James Pepin <james@jamespepin.com>
* @category Horde
- * @package Horde_Injector
+ * @package Injector
*/
class Horde_Injector implements Horde_Injector_Scope
{
+ /**
+ * @var Horde_Injector_Scope
+ */
private $_parentInjector;
+
+ /**
+ * @var array
+ */
private $_bindings = array();
+
+ /**
+ * @var array
+ */
private $_instances;
/**
}
/**
- * TODO
- *
- * @param string $name TODO
- * @param array $name TODO
+ * Method overloader. Handles $this->bind[BinderType] type calls.
*
- * @return TODO
+ * @return Horde_Injector_Binder See _bind().
* @throws BadMethodCallException
*/
public function __call($name, $args)
$reflectionClass = new ReflectionClass('Horde_Injector_Binder_' . $type);
- if ($reflectionClass->getConstructor()) {
- $this->_addBinder($interface, $reflectionClass->newInstanceArgs($args));
- } else {
- $this->_addBinder($interface, $reflectionClass->newInstance());
- }
+ $this->_addBinder(
+ $interface,
+ $reflectionClass->getConstructor()
+ ? $reflectionClass->newInstanceArgs($args)
+ : $reflectionClass->newInstance()
+ );
return $this->_getBinder($interface);
}
*
* This is the method by which we bind an interface to a concrete
* implentation or factory. For convenience, binders may be added by
- * bind[BinderType].
+ * $this->bind[BinderType].
*
- * bindFactory - creates a Horde_Injector_Binder_Factory
- * bindImplementation - creates a Horde_Injector_Binder_Implementation
+ * bindFactory - Creates a Horde_Injector_Binder_Factory
+ * bindImplementation - Creates a Horde_Injector_Binder_Implementation
*
* All subsequent arguments are passed to the constructor of the
* Horde_Injector_Binder object.
*
- * Any Horde_Injector_Binder object may be created this way.
- *
* @param string $interface The interface to bind to.
* @param Horde_Injector_Binder $binder The binder to be bound to the
* specified $interface.
}
/**
- * TODO
+ * @see self::addBinder()
*/
private function _addBinder($interface, Horde_Injector_Binder $binder)
{
- // first we check to see if our parent already has an equal binder set.
+ // First we check to see if our parent already has an equal binder set.
// if so we don't need to do anything
if (!$binder->equals($this->_parentInjector->getBinder($interface))) {
$this->_bindings[$interface] = $binder;
}
/**
- * TODO
+ * Get the Binder associated with the specified instance.
+ *
+ * @param string $interface The interface to retrieve binding information
+ * for.
+ *
+ * @return Horde_Injector_Binder The binder object created. Useful for
+ * method chaining.
*/
private function _getBinder($interface)
{
*
* This method gets you an instance, and saves a reference to that
* instance for later requests.
+ *
* Interfaces must be bound to a concrete class to be created this way.
* Concrete instances may be created through reflection.
+ *
* It does not gaurantee that it is a new instance of the object. For a
* new instance see createInstance().
*
* @param string $interface The interface name, or object class to be
* created.
*
- * @return mixed An object that implements $interface, not necessarily a
- * new one.
+ * @return mixed An object that implements $interface, but not
+ * necessarily a new one.
*/
public function getInstance($interface)
{
- // do we have an instance?
+ // Do we have an instance?
if (!isset($this->_instances[$interface])) {
- // do we have a binding for this interface? if so then we don't ask our parent
+ // Do we have a binding for this interface? If so then we don't
+ // ask our parent
if (!isset($this->_bindings[$interface])) {
- // does our parent have an instance?
+ // Does our parent have an instance?
if ($instance = $this->_parentInjector->getInstance($interface)) {
return $instance;
}
}
- // we have to make our own instance
+ // We have to make our own instance
$this->setInstance($interface, $this->createInstance($interface));
}
return $this->_instances[$interface];
}
+
}
* PHP version 5
*
* @category Horde
- * @package Horde_Injector
+ * @package Injector
* @author Bob Mckee <bmckee@bywires.com>
* @author James Pepin <james@jamespepin.com>
* @license http://opensource.org/licenses/bsd-license.php BSD
* Describes a binding class that is able to create concrete object instances.
*
* @category Horde
- * @package Horde_Injector
+ * @package Injector
* @author Bob Mckee <bmckee@bywires.com>
* @author James Pepin <james@jamespepin.com>
* @license http://opensource.org/licenses/bsd-license.php BSD
/**
* Create an instance.
*
- * @param Horde_Injector $injector The injector should provide all required
- * dependencies for creating the instance.
+ * @param Horde_Injector $injector The injector should provide all
+ * required dependencies for creating the
+ * instance.
*
* @return mixed The concrete instance.
*/
/**
* Determine if one binder equals another binder
*
- * @param Horde_Injector_Binder $binder The binder to compare against $this
+ * @param Horde_Injector_Binder $binder The binder to compare against
+ * $this.
*
- * @return bool true if they are equal, or false if they are not equal
+ * @return boolean True if equal, false if not equal.
*/
public function equals(Horde_Injector_Binder $binder);
+
}
* @author James Pepin <james@jamespepin.com>
* @author Chuck Hagenbuch <chuck@horde.org>
* @category Horde
- * @package Horde_Injector
+ * @package Injector
*/
class Horde_Injector_Binder_AnnotatedSetters implements Horde_Injector_Binder
{
private $_dependencyFinder;
/**
+ * Constructor.
+ *
+ * @param Horde_Injector_Binder $binder TODO
+ * @param Horde_Injector_DependencyFinder $finder TODO
*
*/
- public function __construct(Horde_Injector_Binder $binder, Horde_Injector_DependencyFinder $dependencyFinder = null)
+ public function __construct(Horde_Injector_Binder $binder,
+ Horde_Injector_DependencyFinder $finder = null)
{
$this->_binder = $binder;
-
- if (is_null($dependencyFinder)) { $dependencyFinder = new Horde_Injector_DependencyFinder(); }
- $this->_dependencyFinder = $dependencyFinder;
+ $this->_dependencyFinder = is_null($finder)
+ ? new Horde_Injector_DependencyFinder()
+ : $finder;
}
/**
* TODO
+ *
+ * @param Horde_Injector_Binder $binder TODO
+ *
+ * @return boolean Equality.
*/
public function equals(Horde_Injector_Binder $otherBinder)
{
$this->getBinder()->equals($otherBinder->getBinder());
}
+ /**
+ * TODO
+ *
+ * @return Horde_Injector_Binder TODO
+ */
public function getBinder()
{
return $this->_binder;
* Find all public methods in $reflectionClass that are annotated with
* @inject.
*
- * @param ReflectionClass $reflectionClass
+ * @param ReflectionClass $reflectionClass TODO
*
- * @return array
+ * @return array TODO
*/
private function _findAnnotatedSetters(ReflectionClass $reflectionClass)
{
}
/**
- * Is a method a setter method, by the criteria we define (has a doc comment
- * that includes @inject).
+ * Is a method a setter method, by the criteria we define (has a doc
+ * comment that includes @inject).
*
- * @param ReflectionMethod $reflectionMethod
+ * @param ReflectionMethod $reflectionMethod TODO
*/
private function _isSetterMethod(ReflectionMethod $reflectionMethod)
{
* Call each ReflectionMethod in the $setters array, filling in its
* dependencies with the $injector.
*
- * @param array $setters Array of ReflectionMethods to call
- * @param Horde_Injector $injector The Horde_Injector to get dependencies from
- * @param object $instance The object to call setters on
+ * @param array $setters Array of ReflectionMethods to call.
+ * @param Horde_Injector $injector The injector to get dependencies from.
+ * @param object $instance The object to call setters on.
*/
- private function _callSetters(array $setters, Horde_Injector $injector, $instance)
+ private function _callSetters(array $setters, Horde_Injector $injector,
+ $instance)
{
foreach ($setters as $setterMethod) {
$setterMethod->invokeArgs(
);
}
}
+
}
* Horde_Injector and return an object that satisfies the instance
* requirement. For example:
*
+ * <pre>
* $injector->bindClosure('database', function($injector) { return new my_mysql(); });
+ * </pre>
*
* @author Bob Mckee <bmckee@bywires.com>
* @author James Pepin <james@jamespepin.com>
* @author Chuck Hagenbuch <chuck@horde.org>
* @category Horde
- * @package Horde_Injector
+ * @package Injector
*/
class Horde_Injector_Binder_Closure implements Horde_Injector_Binder
{
/**
* TODO
+ *
+ * @var Horde_Injector_Binder_Closure
*/
private $_closure;
/**
* TODO
+ *
+ * @param Horde_Injector_Binder $otherBinder TODO
+ *
+ * @return boolean Equality.
*/
public function equals(Horde_Injector_Binder $otherBinder)
{
{
$childInjector = $injector->createChildInjector();
$closure = $this->_closure;
+
return $closure($childInjector);
}
+
}
* provide a method or methods that accept a Horde_Injector, and return an
* object that satisfies the instance requirement. For example:
*
+ * <pre>
* class MyFactory {
* ...
* public function create(Horde_Injector $injector)
* }
* ...
* }
+ * </pre>
*
* @author Bob Mckee <bmckee@bywires.com>
* @author James Pepin <james@jamespepin.com>
* @category Horde
- * @package Horde_Injector
+ * @package Injector
*/
class Horde_Injector_Binder_Factory implements Horde_Injector_Binder
{
/**
* TODO
+ *
+ * @var string
*/
private $_factory;
/**
* TODO
+ *
+ * @var string
*/
private $_method;
/**
* TODO
+ *
+ * @param Horde_Injector_Binder $otherBinder TODO
+ *
+ * @return boolean Equality.
*/
public function equals(Horde_Injector_Binder $otherBinder)
{
* type. */
return $childInjector->getInstance($this->_factory)->{$this->_method}($childInjector);
}
+
}
* @author Bob Mckee <bmckee@bywires.com>
* @author James Pepin <james@jamespepin.com>
* @category Horde
- * @package Horde_Injector
+ * @package Injector
*/
class Horde_Injector_Binder_Implementation implements Horde_Injector_Binder
{
/**
* TODO
*/
- public function __construct($implementation, Horde_Injector_DependencyFinder $dependencyFinder = null)
+ public function __construct($implementation,
+ Horde_Injector_DependencyFinder $finder = null)
{
$this->_implementation = $implementation;
-
- if (is_null($dependencyFinder)) { $dependencyFinder = new Horde_Injector_DependencyFinder(); }
- $this->_dependencyFinder = $dependencyFinder;
+ $this->_dependencyFinder = is_null($finder)
+ ? new Horde_Injector_DependencyFinder()
+ : $finder;
}
/**
* TODO
+ *
+ * @return TODO
*/
public function getImplementation()
{
/**
* TODO
+ *
+ * @return boolean Equality.
*/
public function equals(Horde_Injector_Binder $otherBinder)
{
/**
* TODO
*/
- protected function _getInstance(Horde_Injector $injector, ReflectionClass $class)
+ protected function _getInstance(Horde_Injector $injector,
+ ReflectionClass $class)
{
return $class->getConstructor()
? $class->newInstanceArgs($this->_dependencyFinder->getMethodDependencies($injector, $class->getConstructor()))
: $class->newInstance();
}
+
}
* @author James Pepin <james@jamespepin.com>
* @author Chuck Hagenbuch <chuck@horde.org>
* @category Horde
- * @package Horde_Injector
+ * @package Injector
*/
class Horde_Injector_DependencyFinder
{
/**
+ * TODO
+ *
+ * @param Horde_Injector $injector TODO
+ * @param ReflectionMethod $method TODO
+ *
+ * @return array TODO
+ * @throws Horde_Injector_Exception
*/
- public function getMethodDependencies(Horde_Injector $injector, ReflectionMethod $method)
+ public function getMethodDependencies(Horde_Injector $injector,
+ ReflectionMethod $method)
{
$dependencies = array();
}
/**
+ * TODO
+ *
+ * @param Horde_Injector $injector TODO
+ * @param ReflectionParameter $method TODO
+ *
+ * @return mixed TODO
+ * @throws Horde_Injector_Exception
*/
- public function getParameterDependency(Horde_Injector $injector, ReflectionParameter $parameter)
+ public function getParameterDependency(Horde_Injector $injector,
+ ReflectionParameter $parameter)
{
if ($parameter->getClass()) {
return $injector->getInstance($parameter->getClass()->getName());
throw new Horde_Injector_Exception("Untyped parameter \$" . $parameter->getName() . "can't be fulfilled");
}
+
}
<?php
/**
- * @author Bob Mckee <bmckee@bywires.com>
- * @author James Pepin <james@jamespepin.com>
+ * @author Bob Mckee <bmckee@bywires.com>
+ * @author James Pepin <james@jamespepin.com>
* @category Horde
- * @package Horde_Injector
+ * @package Injector
*/
-class Horde_Injector_Exception extends Exception
-{
-}
+class Horde_Injector_Exception extends Exception {}
/**
* Interface for injector scopes
*
- * Injectors implement a Chain of Responsibility pattern. This is the required
- * interface for injectors to pass on responsibility to parent objects in the chain.
+ * Injectors implement a Chain of Responsibility pattern. This is the
+ * required interface for injectors to pass on responsibility to parent
+ * objects in the chain.
*
* @category Horde
- * @package Horde_Injector
+ * @package Injector
*/
interface Horde_Injector_Scope
{
/**
- * Returns the Horde_Injector_Binder object mapped to the request interface if such a
+ * Returns the Horde_Injector_Binder object mapped to the request
+ * interface if such a
* mapping exists
*
+ * @param string $interface Interface name of object whose binding if
+ * being retrieved.
+ *
* @return Horde_Injector_Binder|null
- * @param string $interface interface name of object whose binding if being retrieved
*/
public function getBinder($interface);
/**
- * Returns instance of requested object if proper configuration has been provided.
+ * Returns instance of requested object if proper configuration has been
+ * provided.
+ *
+ * @param string $interface Interface name of object which is being
+ * requested.
*
* @return Object
- * @param string $interface interface name of object which is being requested
*/
public function getInstance($interface);
+
}
* This class returns a Horde_Injector_Binder_Implementation with the requested
* $interface mapped to itself. This is the default case, and for conrete
* classes should work all the time so long as you constructor parameters are
- * typed
+ * typed.
*
- * @author Bob Mckee <bmckee@bywires.com>
- * @author James Pepin <james@jamespepin.com>
+ * @author Bob Mckee <bmckee@bywires.com>
+ * @author James Pepin <james@jamespepin.com>
* @category Horde
- * @package Horde_Injector
+ * @package Injector
*/
class Horde_Injector_TopLevel implements Horde_Injector_Scope
{
/**
* Get an Implementation Binder that maps the $interface to itself
*
- * @param string $interface The interface to retrieve binding information for
- * @return Horde_Injector_Binder_ImplementationWithSetters a new binding object that maps the interface to itself, with setter injection
+ * @param string $interface The interface to retrieve binding information
+ * for.
+ *
+ * @return Horde_Injector_Binder_ImplementationWithSetters
+ * A new binding object that maps the interface to itself, with
+ * setter injection.
*/
public function getBinder($interface)
{
$dependencyFinder = new Horde_Injector_DependencyFinder();
$implementationBinder = new Horde_Injector_Binder_Implementation($interface, $dependencyFinder);
+
return new Horde_Injector_Binder_AnnotatedSetters($implementationBinder, $dependencyFinder);
}
* Always return null. Object doesn't keep instance references
*
* Method is necessary because this object is the default parent Injector.
- * The child of this injector will ask it for instances in the case where no
- * bindings are set on the child. This should always return null.
+ * The child of this injector will ask it for instances in the case where
+ * no bindings are set on the child. This should always return null.
*
* @param string $interface The interface in question
* @return null
{
return null;
}
+
}