Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' into no
Browse files Browse the repository at this point in the history
  • Loading branch information
roheim committed Feb 23, 2012
3 parents 8cb611e + c958861 + d688369 commit b8b41bc
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 55 deletions.
72 changes: 37 additions & 35 deletions src/Di.php
Expand Up @@ -8,7 +8,7 @@ class Di implements DependencyInjection
* @var DefinitionList
*/
protected $definitions = null;

/**
* @var InstanceManager
*/
Expand All @@ -18,18 +18,18 @@ class Di implements DependencyInjection
* @var string
*/
protected $instanceContext = array();

/**
* All the class dependencies [source][dependency]
*
* @var array
*
* @var array
*/
protected $currentDependencies = array();

/**
* All the class references [dependency][source]
*
* @var array
*
* @var array
*/
protected $references = array();

Expand Down Expand Up @@ -89,7 +89,7 @@ public function setInstanceManager(InstanceManager $instanceManager)
}

/**
*
*
* @return InstanceManager
*/
public function instanceManager()
Expand All @@ -101,22 +101,23 @@ public function instanceManager()
/**
* Lazy-load a class
*
* Attempts to load the class (or service alias) provided. If it has been
* Attempts to load the class (or service alias) provided. If it has been
* loaded before, the previous instance will be returned (unless the service
* definition indicates shared instances should not be used).
*
*
* @param string $name Class name or service alias
* @param null|array $params Parameters to pass to the constructor
* @return object|null
*/
public function get($name, array $params = array())
{
array_push($this->instanceContext, array('GET', $name));

$im = $this->instanceManager;

if ($params) {
if (($fastHash = $im->hasSharedInstanceWithParameters($name, $params, true))) {
$fastHash = $im->hasSharedInstanceWithParameters($name, $params, true);
if ($fastHash) {
array_pop($this->instanceContext);
return $im->getSharedInstanceWithParameters(null, array(), $fastHash);
}
Expand Down Expand Up @@ -157,14 +158,14 @@ public function newInstance($name, array $params = array(), $isShared = true)
}

array_push($this->instanceContext, array('NEW', $class, $alias));

if (!$definitions->hasClass($class)) {
$aliasMsg = ($alias) ? '(specified by alias ' . $alias . ') ' : '';
throw new Exception\ClassNotFoundException(
'Class ' . $aliasMsg . $class . ' could not be located in provided definitions.'
);
}

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

Expand All @@ -190,7 +191,7 @@ public function newInstance($name, array $params = array(), $isShared = true)
} else {
$msg = 'Invalid instantiator';
}
throw new \RuntimeException($msg);
throw new \RuntimeException($msg);
}

if ($isShared) {
Expand Down Expand Up @@ -243,7 +244,8 @@ public function newInstance($name, array $params = array(), $isShared = true)
if ($objectsToInject) {
foreach ($objectsToInject as $objectToInject) {
foreach ($injectionMethods as $injectionMethod => $methodIsRequired) {
if ($methodParams = $definitions->getMethodParameters($class, $injectionMethod)) {
$methodParams = $definitions->getMethodParameters($class, $injectionMethod);
if ($methodParams) {
foreach ($methodParams as $methodParam) {
if (get_class($objectToInject) == $methodParam[1] ||
$this->isSubclassOf(get_class($objectToInject), $methodParam[1])) {
Expand All @@ -270,15 +272,15 @@ public function newInstance($name, array $params = array(), $isShared = true)
}
}
}





array_pop($this->instanceContext);
return $instance;
}

/**
* @todo
* @todo
* @param unknown_type $object
*/
/*
Expand Down Expand Up @@ -348,7 +350,7 @@ protected function createInstanceViaCallback($callback, $params, $alias)
if (!is_callable($callback)) {
throw new Exception\InvalidCallbackException('An invalid constructor callback was provided');
}

if (is_array($callback)) {
$class = (is_object($callback[0])) ? get_class($callback[0]) : $callback[0];
$method = $callback[1];
Expand All @@ -362,7 +364,7 @@ protected function createInstanceViaCallback($callback, $params, $alias)
if ($this->definitions->hasMethod($class, $method)) {
$callParameters = $this->resolveMethodParameters($class, $method, $params, true, $alias, true);
}
return call_user_func_array($callback, $callParameters);
return call_user_func_array($callback, $callParameters);
}

/**
Expand Down Expand Up @@ -401,7 +403,7 @@ protected function resolveMethodParameters($class, $method, array $callTimeUserP
{
// parameters for this method, in proper order, to be returned
$resolvedParams = array();

// parameter requirements from the definition
$injectionMethodParameters = $this->definitions->getMethodParameters($class, $method);

Expand All @@ -411,21 +413,21 @@ protected function resolveMethodParameters($class, $method, array $callTimeUserP
'required' => array(),
'optional' => array()
);

// retrieve instance configurations for all contexts
$iConfig = array();
$aliases = $this->instanceManager->getAliases();

// for the alias in the dependency tree
if ($alias && $this->instanceManager->hasConfiguration($alias)) {
$iConfig['thisAlias'] = $this->instanceManager->getConfiguration($alias);
}

// for the current class in the dependency tree
if ($this->instanceManager->hasConfiguration($class)) {
$iConfig['thisClass'] = $this->instanceManager->getConfiguration($class);
}

// for the parent class, provided we are deeper than one node
list($requestedClass, $requestedAlias) = ($this->instanceContext[0][0] == 'NEW')
? array($this->instanceContext[0][1], $this->instanceContext[0][2])
Expand Down Expand Up @@ -465,7 +467,7 @@ protected function resolveMethodParameters($class, $method, array $callTimeUserP
$computedParams['required'][$fqParamPos] = array(
$callTimeUserParams[$name],
$this->instanceManager->getClassFromAlias($callTimeCurValue)
);
);
} elseif ($this->definitions->hasClass($callTimeUserParams[$name])) {
// was a known class provided?
$computedParams['required'][$fqParamPos] = array(
Expand All @@ -483,12 +485,12 @@ protected function resolveMethodParameters($class, $method, array $callTimeUserP
unset($callTimeCurValue);
continue;
}

// PRIORITY 2 -specific instance configuration (thisAlias) - this alias
// PRIORITY 3 -THEN specific instance configuration (thisClass) - this class
// PRIORITY 4 -THEN specific instance configuration (requestedAlias) - requested alias
// PRIORITY 5 -THEN specific instance configuration (requestedClass) - requested class

foreach (array('thisAlias', 'thisClass', 'requestedAlias', 'requestedClass') as $thisIndex) {
// check the provided parameters config
if (isset($iConfig[$thisIndex]['parameters'][$fqParamPos])
Expand Down Expand Up @@ -531,9 +533,9 @@ protected function resolveMethodParameters($class, $method, array $callTimeUserP
}

}

// PRIORITY 6 - globally preferred implementations

// next consult alias level preferred instances
if ($alias && $this->instanceManager->hasTypePreferences($alias)) {
$pInstances = $this->instanceManager->getTypePreferences($alias);
Expand Down Expand Up @@ -575,7 +577,7 @@ protected function resolveMethodParameters($class, $method, array $callTimeUserP
if ($type && $isRequired && $methodIsRequired) {
$computedParams['required'][$fqParamPos] = array($type, $type);
}

}

$index = 0;
Expand Down Expand Up @@ -615,7 +617,7 @@ protected function resolveMethodParameters($class, $method, array $callTimeUserP
} else {
$resolvedParams[$index] = null;
}

$index++;
}

Expand Down
41 changes: 21 additions & 20 deletions src/ServiceLocator/DependencyInjectorProxy.php
Expand Up @@ -13,7 +13,7 @@ class DependencyInjectorProxy extends Di
protected $di;

/**
* @param DependencyInjector $di
* @param DependencyInjector $di
* @return void
*/
public function __construct(Di $di)
Expand All @@ -29,17 +29,18 @@ public function __construct(Di $di)

/**
* Override, as we want it to use the functionality defined in the proxy
*
* @param string $name
* @param array $params
*
* @param string $name
* @param array $params
* @return GeneratorInstance
*/
public function get($name, array $params = array())
{
$im = $this->instanceManager();

if ($params) {
if (($fastHash = $im->hasSharedInstanceWithParameters($name, $params, true))) {
$fastHash = $im->hasSharedInstanceWithParameters($name, $params, true);
if ($fastHash) {
return $im->getSharedInstanceWithParameters(null, array(), $fastHash);
}
} else {
Expand All @@ -54,9 +55,9 @@ public function get($name, array $params = array())
* Override createInstanceViaConstructor method from injector
*
* Returns code generation artifacts.
*
* @param string $class
* @param null|array $params
*
* @param string $class
* @param null|array $params
* @param null|string $alias
* @return GeneratorInstance
*/
Expand All @@ -75,9 +76,9 @@ public function createInstanceViaConstructor($class, $params, $alias = null)

/**
* Override instance creation via callback
*
* @param callback $callback
* @param null|array $params
*
* @param callback $callback
* @param null|array $params
* @return GeneratorInstance
*/
public function createInstanceViaCallback($callback, $params, $alias = null)
Expand All @@ -103,11 +104,11 @@ public function createInstanceViaCallback($callback, $params, $alias = null)

/**
* Retrieve metadata for injectible methods
*
* @param string $class
* @param string $method
* @param array $params
* @param string $alias
*
* @param string $class
* @param string $method
* @param array $params
* @param string $alias
* @return array
*/
public function handleInjectionMethodForObject($class, $method, $params, $alias, $isRequired)
Expand All @@ -121,10 +122,10 @@ public function handleInjectionMethodForObject($class, $method, $params, $alias,

/**
* Override new instance creation
*
* @param string $name
* @param array $params
* @param bool $isShared
*
* @param string $name
* @param array $params
* @param bool $isShared
* @return GeneratorInstance
*/
public function newInstance($name, array $params = array(), $isShared = true)
Expand Down

0 comments on commit b8b41bc

Please sign in to comment.