Skip to content

Commit

Permalink
Documenting code for GeneratorInstance, allowing string callbacks in …
Browse files Browse the repository at this point in the history
…DependencyInjectorProxy, fixing instance name when an instance is generated via callback
  • Loading branch information
Ocramius committed Jul 5, 2012
1 parent 632487c commit aa00d2f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
16 changes: 13 additions & 3 deletions library/Zend/Di/ServiceLocator/DependencyInjectorProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public function newInstance($name, array $params = array(), $isShared = true)
if ($instance instanceof GeneratorInstance) {
/* @var $instance GeneratorInstance */
$instance->setShared($isShared);

// When a callback is used, we don't know instance the class name.
// That's why we assume $name as the instance alias
if (null === $instance->getName()) {
$instance->setAlias($name);
}
}

return $instance;
Expand Down Expand Up @@ -82,14 +88,18 @@ public function createInstanceViaConstructor($class, $params, $alias = null)
*/
public function createInstanceViaCallback($callback, $params, $alias)
{
if (is_string($callback)) {
$callback = explode('::', $callback);
}

if (!is_callable($callback)) {
throw new Exception\InvalidCallbackException('An invalid constructor callback was provided');
}

// @todo add support for string callbacks?
if (!is_array($callback) || is_object($callback[0])) {
throw new Exception\InvalidCallbackException('For purposes of service locator generation, constructor callbacks must refer to static methods only');
throw new Exception\InvalidCallbackException(
'For purposes of service locator generation, constructor callbacks must refer to static methods only'
);
}

$class = $callback[0];
Expand Down
23 changes: 17 additions & 6 deletions library/Zend/Di/ServiceLocator/GeneratorInstance.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class GeneratorInstance
{
/**
* @var string
* @var string|null
*/
protected $class;

Expand Down Expand Up @@ -49,22 +49,28 @@ public function __construct($class, $alias, $constructor, array $params)
}

/**
* @return string instance or class name
* Retrieves the best available name for this instance (instance alias first then class name)
*
* @return string|null
*/
public function getName()
{
return $this->alias ? $this->alias : $this->class;
}

/**
* @return string
* Class of the instance. Null if class is unclear (such as when the instance is produced by a callback)
*
* @return string|null
*/
public function getClass()
{
return $this->class;
}

/**
* Alias for the instance (if any)
*
* @return string|null
*/
public function getAlias()
Expand Down Expand Up @@ -102,15 +108,16 @@ public function setAlias($alias)
/**
* Get instantiator
*
* @return mixed constructor method or callable for the instance
* @return mixed constructor method name or callable responsible for generating instance
*/
public function getConstructor()
{
return $this->constructor;
}

/**
* Get params
* Parameters passed to the instantiator as an ordered list of parameters. Each parameter that refers to another
* instance fetched recursively is a GeneratorInstance itself
*
* @return array
*/
Expand Down Expand Up @@ -144,7 +151,9 @@ public function addMethod($method)
}

/**
* Retrieves an ordered list of methods called on the instance
* Retrieves a list of methods that are called on the instance in their call order. Each returned element has form
* array('method' => 'methodName', 'params' => array( ... ordered list of call parameters ... ), where every call
* parameter that is a recursively fetched instance is a GeneratorInstance itself
*
* @return array
*/
Expand All @@ -162,6 +171,8 @@ public function setShared($shared)
}

/**
* Retrieves whether the instance is shared or not
*
* @return bool
*/
public function isShared()
Expand Down

0 comments on commit aa00d2f

Please sign in to comment.