Skip to content

Commit

Permalink
Minor fixes required to get the DependencyInjectorProxy to work
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocramius committed Jul 5, 2012
1 parent 4040563 commit 436fa13
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 152 deletions.
9 changes: 3 additions & 6 deletions library/Zend/Di/Definition/CompilerDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function addCodeScannerFile(FileScanner $fileScanner)
if ($this->directoryScanner == null) {
$this->directoryScanner = new DirectoryScanner();
}

$this->directoryScanner->addFileScanner($fileScanner);
}

Expand All @@ -103,8 +103,8 @@ public function addCodeScannerFile(FileScanner $fileScanner)
*/
public function compile()
{
/*
* @var $classScanner \Zend\Code\Scanner\DerivedClassScanner
/*
* @var $classScanner \Zend\Code\Scanner\DerivedClassScanner
*/
foreach ($this->directoryScanner->getClassNames() as $class) {
$this->processClass($class);
Expand Down Expand Up @@ -246,9 +246,6 @@ protected function processClass($class)
}
}
}


//var_dump($this->classes);
}

protected function processParams(&$def, Reflection\ClassReflection $rClass, Reflection\MethodReflection $rMethod)
Expand Down
9 changes: 3 additions & 6 deletions library/Zend/Di/Definition/RuntimeDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function setIntrospectionStrategy(IntrospectionStrategy $introspectionStr
{
$this->introspectionStrategy = $introspectionStrategy;
}

/**
* @return IntrospectionStrategy
*/
Expand Down Expand Up @@ -82,7 +82,7 @@ public function forceLoadClass($class)

/**
* Retrieves registered classes names
*
*
* @return array
*/
public function getClasses()
Expand All @@ -101,7 +101,7 @@ public function hasClass($class)
if ($this->explicitLookups === true) {
return (array_key_exists($class, $this->classes));
}

return class_exists($class) || interface_exists($class);
}

Expand Down Expand Up @@ -324,9 +324,6 @@ protected function processClass($class)
}
}
}


//var_dump($this->classes);
}

protected function processParams(&$def, Reflection\ClassReflection $rClass, Reflection\MethodReflection $rMethod)
Expand Down
5 changes: 3 additions & 2 deletions library/Zend/Di/Di.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ protected function handleInjectDependencies($instance, $injectionMethods, $param
$methodParams = $definitions->getMethodParameters($type, $typeInjectionMethod);
if ($methodParams) {
foreach ($methodParams as $methodParam) {
if (get_class($objectToInject) == $methodParam[1] || $this->isSubclassOf(get_class($objectToInject), $methodParam[1])) {
$objectToInjectClass = $objectToInject instanceof ServiceLocator\GeneratorInstance ? $objectToInject->getClass() : get_class($objectToInject);
if ($objectToInjectClass == $methodParam[1] || $this->isSubclassOf($objectToInjectClass, $methodParam[1])) {
if ($this->resolveAndCallInjectionMethodForInstance($instance, $typeInjectionMethod, array($methodParam[0] => $objectToInject), $instanceAlias, true, $type)) {
$calledMethods[$typeInjectionMethod] = true;
}
Expand All @@ -315,7 +316,7 @@ protected function handleInjectDependencies($instance, $injectionMethods, $param
}
if ($methodsToCall) {
foreach ($methodsToCall as $methodInfo) {
$this->resolveAndCallInjectionMethodForInstance($instance, $methodInfo['method'], $methodInfo['args'], $instanceAlias, true, get_class($instance));
$this->resolveAndCallInjectionMethodForInstance($instance, $methodInfo['method'], $methodInfo['args'], $instanceAlias, true, $instanceClass);
}
}
}
Expand Down
125 changes: 40 additions & 85 deletions library/Zend/Di/ServiceLocator/DependencyInjectorProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ public function __construct(Di $di)
$this->instanceManager = $di->instanceManager();
}

/**
* Methods with functionality overrides
*/

/**
* Override, as we want it to use the functionality defined in the proxy
*
Expand All @@ -35,19 +31,21 @@ public function __construct(Di $di)
*/
public function get($name, array $params = array())
{
$im = $this->instanceManager();

if ($params) {
$fastHash = $im->hasSharedInstanceWithParameters($name, $params, true);
if ($fastHash) {
return $im->getSharedInstanceWithParameters(null, array(), $fastHash);
}
} else {
if ($im->hasSharedInstance($name, $params)) {
return $im->getSharedInstance($name, $params);
}
return parent::get($name, $params);
}
/**
* {@inheritDoc}
*/
public function newInstance($name, array $params = array(), $isShared = true)
{
$instance = parent::newInstance($name, $params, $isShared);

if ($instance instanceof GeneratorInstance) {
/* @var $instance GeneratorInstance */
$instance->setShared($isShared);
}
return $this->newInstance($name, $params);

return $instance;
}

/**
Expand All @@ -63,14 +61,15 @@ public function get($name, array $params = array())
public function createInstanceViaConstructor($class, $params, $alias = null)
{
$callParameters = array();

if ($this->di->definitions->hasMethod($class, '__construct')
&& (count($this->di->definitions->getMethodParameters($class, '__construct')) > 0)
) {
$callParameters = $this->resolveMethodParameters(
$class, '__construct', $params, true, $alias, true
);
$callParameters = $this->resolveMethodParameters($class, '__construct', $params, $alias, true, true);
$callParameters = $callParameters ?: array();
}
return new GeneratorInstance($class, '__construct', $callParameters);

return new GeneratorInstance($class, $alias, '__construct', $callParameters);
}

/**
Expand All @@ -81,12 +80,14 @@ public function createInstanceViaConstructor($class, $params, $alias = null)
* @return GeneratorInstance
* @throws Exception\InvalidCallbackException
*/
public function createInstanceViaCallback($callback, $params, $alias = null)
public function createInstanceViaCallback($callback, $params, $alias)
{
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');
}
Expand All @@ -96,89 +97,43 @@ public function createInstanceViaCallback($callback, $params, $alias = null)

$callParameters = array();
if ($this->di->definitions->hasMethod($class, $method)) {
$callParameters = $this->resolveMethodParameters($class, $method, $params, true, $alias, true);
$callParameters = $this->resolveMethodParameters($class, $method, $params, $alias, true, true);
}

return new GeneratorInstance(null, $callback, $callParameters);
$callParameters = $callParameters ?: array();

return new GeneratorInstance(null, $alias, $callback, $callParameters);
}

/**
* Retrieve metadata for injectible methods
*
* @param string $class
* @param string $method
* @param array $params
* @param string $alias
* @return array
* {@inheritDoc}
*/
public function handleInjectionMethodForObject($class, $method, $params, $alias, $isRequired)
{
$callParameters = $this->resolveMethodParameters($class, $method, $params, false, $alias, $isRequired);
return array(
'method' => $method,
'params' => $callParameters,
'params' => $this->resolveMethodParameters($class, $method, $params, $alias, $isRequired),
);
}

/**
* Override new instance creation
*
* @param string $name
* @param array $params
* @param bool $isShared
* @return GeneratorInstance
* @throws Exception\ClassNotFoundException
* @throws Exception\RuntimeException
* {@inheritDoc}
*/
public function newInstance($name, array $params = array(), $isShared = true)
protected function resolveAndCallInjectionMethodForInstance($instance, $method, $params, $alias, $methodIsRequired, $methodClass = null)
{
$definition = $this->definitions();
$instanceManager = $this->instanceManager();

if ($instanceManager->hasAlias($name)) {
$class = $instanceManager->getClassFromAlias($name);
$alias = $name;
} else {
$class = $name;
$alias = null;
}

if (!$definition->hasClass($class)) {
$aliasMsg = ($alias) ? '(specified by alias ' . $alias . ') ' : '';
throw new Exception\ClassNotFoundException(
'Class ' . $aliasMsg . $class . ' could not be located in provided definitions.'
);
if (!$instance instanceof GeneratorInstance) {
parent::resolveAndCallInjectionMethodForInstance($instance, $method, $params, $alias, $methodIsRequired, $methodClass);
}

$instantiator = $definition->getInstantiator($class);
$injectionMethods = $definition->getMethods($class);

if ($instantiator === '__construct') {
$object = $this->createInstanceViaConstructor($class, $params, $alias);
if (in_array('__construct', $injectionMethods)) {
unset($injectionMethods[array_search('__construct', $injectionMethods)]);
}
} elseif (is_callable($instantiator)) {
$object = $this->createInstanceViaCallback($instantiator, $params, $alias);
$object->setName($class);
} else {
throw new Exception\RuntimeException('Invalid instantiator');
}
/* @var $instance GeneratorInstance */
$methodClass = $instance->getClass();
$callParameters = $this->resolveMethodParameters($methodClass, $method, $params, $alias, $methodIsRequired);

if ($injectionMethods) {
foreach ($injectionMethods as $injectionMethod) {
$methodMetadata[] = $this->handleInjectionMethodForObject($class, $injectionMethod, $params, $alias);
}
if ($callParameters !== false) {
$instance->addMethod(array(
'method' => $method,
'params' => $callParameters,
));
}

if ($isShared) {
if ($params) {
$instanceManager->addSharedInstanceWithParameters($object, $name, $params);
} else {
$instanceManager->addSharedInstance($object, $name);
}
}

return $object;
}
}
Loading

0 comments on commit 436fa13

Please sign in to comment.