<?php
/**
- * A binder object for binding an interface to a factory class and method
+ * A binder object for binding an interface to a factory class and method.
*
* An interface may be bound to a factory class. That factory class must
* provide a method or methods that accept a Horde_Injector, and return an
- * object that satisfies the instance requirement. ie:
- * class MyFactory {
- * ...
- * public function create(Horde_Injector $injector) {
- * return new MyClass($injector->getInstance('Collaborator'), new MyOtherClass(17));
- * }
- * ...
+ * object that satisfies the instance requirement. For example:
*
- * @author Bob Mckee <bmckee@bywires.com>
- * @author James Pepin <james@jamespepin.com>
+ * class MyFactory {
+ * ...
+ * public function create(Horde_Injector $injector)
+ * {
+ * return new MyClass($injector->getInstance('Collaborator'), new MyOtherClass(17));
+ * }
+ * ...
+ * }
+ *
+ * @author Bob Mckee <bmckee@bywires.com>
+ * @author James Pepin <james@jamespepin.com>
* @category Horde
- * @package Horde_Injector
+ * @package Horde_Injector
*/
class Horde_Injector_Binder_Factory implements Horde_Injector_Binder
{
+ /**
+ * TODO
+ */
private $_factory;
+
+ /**
+ * TODO
+ */
private $_method;
/**
- * create a new Horde_Injector_Binder_Factory instance
+ * Create a new Horde_Injector_Binder_Factory instance.
*
- * @param string $factory The factory class to use for creating objects
- * @param string $method The method on that factory to use for creating objects
+ * @param string $factory The factory class to use for creating objects.
+ * @param string $method The method on that factory to use for creating
+ * objects.
*/
public function __construct($factory, $method)
{
$this->_method = $method;
}
+ /**
+ * TODO
+ */
public function equals(Horde_Injector_Binder $otherBinder)
{
- if (!$otherBinder instanceof Horde_Injector_Binder_Factory) {
- return false;
- }
-
- if ($otherBinder->getFactory() != $this->_factory) {
- return false;
- }
-
- if ($otherBinder->getMethod() != $this->_method) {
- return false;
- }
-
- return true;
+ return (($otherBinder instanceof Horde_Injector_Binder_Factory) &&
+ ($otherBinder->getFactory() == $this->_factory) &&
+ ($otherBinder->getMethod() == $this->_method));
}
/**
- * get the factory classname that this binder was bound to
+ * Get the factory classname that this binder was bound to.
*
- * @return string The factory classname this binder is bound to
+ * @return string The factory classname this binder is bound to.
*/
public function getFactory()
{
}
/**
- * get the method that this binder was bound to
+ * Get the method that this binder was bound to.
*
- * @return string The method this binder is bound to
+ * @return string The method this binder is bound to.
*/
public function getMethod()
{
/**
* Create instance using a factory method
*
- * If the factory depends on a Horde_Injector we want to limit its scope so
- * it cannot change anything that effects any higher-level scope. A factory
- * should not have the responsibility of making a higher-level scope change.
+ * If the factory depends on a Horde_Injector we want to limit its scope
+ * so it cannot change anything that effects any higher-level scope. A
+ * factory should not have the responsibility of making a higher-level
+ * scope change.
* To enforce this we create a new child Horde_Injector. When a
- * Horde_Injector is requested from a Horde_Injector it will return itself.
- * This means that the factory will only ever be able to work on the child
- * Horde_Injector we give it now.
+ * Horde_Injector is requested from a Horde_Injector it will return
+ * itself. This means that the factory will only ever be able to work on
+ * the child Horde_Injector we give it now.
*
- * @param Horde_Injector $injector
+ * @param Horde_Injector $injector Injector object.
+ *
+ * @return TODO
*/
public function create(Horde_Injector $injector)
{
$childInjector = $injector->createChildInjector();
- // we use getInstance here because we don't want to have to create this
- // factory more than one time to create more objects of this type.
+
+ /* We use getInstance() here because we don't want to have to create
+ * this factory more than one time to create more objects of this
+ * type. */
return $childInjector->getInstance($this->_factory)->{$this->_method}($childInjector);
}
+
}
<?php
/**
+ * TODO
*
- * @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 Horde_Injector
*/
class Horde_Injector_Binder_Implementation implements Horde_Injector_Binder
{
+ /**
+ * TODO
+ */
private $_implementation;
+
+ /**
+ * TODO
+ */
private $_setters;
+ /**
+ * TODO
+ */
public function __construct($implementation)
{
$this->_implementation = $implementation;
$this->_setters = array();
}
+ /**
+ * TODO
+ */
public function getImplementation()
{
return $this->_implementation;
}
+ /**
+ * TODO
+ */
public function bindSetter($method)
{
$this->_setters[] = $method;
return $this;
}
+ /**
+ * TODO
+ */
public function equals(Horde_Injector_Binder $otherBinder)
{
- if (!$otherBinder instanceof Horde_Injector_Binder_Implementation) {
- return false;
- }
-
- if ($otherBinder->getImplementation() != $this->_implementation) {
- return false;
- }
-
- return true;
+ return (($otherBinder instanceof Horde_Injector_Binder_Implementation) &&
+ ($otherBinder->getImplementation() == $this->_implementation));
}
+ /**
+ * TODO
+ */
public function create(Horde_Injector $injector)
{
$reflectionClass = new ReflectionClass($this->_implementation);
return $instance;
}
+ /**
+ * TODO
+ */
private function _validateImplementation(ReflectionClass $reflectionClass)
{
if ($reflectionClass->isAbstract() || $reflectionClass->isInterface()) {
- throw new Horde_Injector_Exception('Cannot bind interfaces or abstract classes "' .
- $this->_implementation . '" to an interface.');
+ throw new Horde_Injector_Exception('Cannot bind interfaces or abstract classes "' . $this->_implementation . '" to an interface.');
}
}
- private function _getInstance(Horde_Injector $injector, ReflectionClass $class)
+ /**
+ * TODO
+ */
+ private function _getInstance(Horde_Injector $injector,
+ ReflectionClass $class)
{
- if ($class->getConstructor()) {
- return $class->newInstanceArgs(
- $this->_getMethodDependencies($injector, $class->getConstructor())
- );
- }
- return $class->newInstance();
+ return $class->getConstructor()
+ ? $class->newInstanceArgs($this->_getMethodDependencies($injector, $class->getConstructor()))
+ : $class->newInstance();
}
- private function _getMethodDependencies(Horde_Injector $injector, ReflectionMethod $method)
+ /**
+ * TODO
+ */
+ private function _getMethodDependencies(Horde_Injector $injector,
+ ReflectionMethod $method)
{
$dependencies = array();
+
foreach ($method->getParameters() as $parameter) {
$dependencies[] = $this->_getParameterDependency($injector, $parameter);
}
+
return $dependencies;
}
- private function _getParameterDependency(Horde_Injector $injector, ReflectionParameter $parameter)
+ /**
+ * TODO
+ */
+ private function _getParameterDependency(Horde_Injector $injector,
+ ReflectionParameter $parameter)
{
if ($parameter->getClass()) {
- $dependency = $injector->getInstance($parameter->getClass()->getName());
+ return $injector->getInstance($parameter->getClass()->getName());
} elseif ($parameter->isOptional()) {
- $dependency = $parameter->getDefaultValue();
- } else {
- throw new Horde_Injector_Exception('Unable to instantiate class "' . $this->_implementation .
- '" because a value could not be determined untyped parameter "$' .
- $parameter->getName() . '"');
+ return $parameter->getDefaultValue();
}
- return $dependency;
+
+ throw new Horde_Injector_Exception('Unable to instantiate class "' . $this->_implementation . '" because a value could not be determined untyped parameter "$' . $parameter->getName() . '"');
}
+ /**
+ * TODO
+ */
private function _callSetters(Horde_Injector $injector, $instance)
{
foreach ($this->_setters as $setter) {
);
}
}
+
}