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

Commit

Permalink
Merge pull request #4435 from noopable/issue/4434
Browse files Browse the repository at this point in the history
[WIP] Di compatibility (#4434)
  • Loading branch information
weierophinney committed May 9, 2013
2 parents 3af6f8c + c18896c commit e3af457
Show file tree
Hide file tree
Showing 11 changed files with 435 additions and 84 deletions.
10 changes: 10 additions & 0 deletions library/Zend/Di/Definition/ArrayDefinition.php
Expand Up @@ -9,6 +9,7 @@

namespace Zend\Di\Definition;

use Zend\Di\Definition\Builder\InjectionMethod;
/**
* Class definitions based on a given array
*/
Expand All @@ -28,6 +29,15 @@ public function __construct(array $dataArray)
// force lower names
$dataArray[$class] = array_change_key_case($dataArray[$class], CASE_LOWER);
}
foreach ($dataArray as $class => $definition) {
if (isset($definition['methods']) && is_array($definition['methods'])) {
foreach ($definition['methods'] as $type => $requirement) {
if (!is_int($requirement)) {
$dataArray[$class]['methods'][$type] = InjectionMethod::detectMethodRequirement($requirement);
}
}
}
}
$this->dataArray = $dataArray;
}

Expand Down
51 changes: 50 additions & 1 deletion library/Zend/Di/Definition/Builder/InjectionMethod.php
Expand Up @@ -9,6 +9,8 @@

namespace Zend\Di\Definition\Builder;

use Zend\Di\Di;

/**
* Definitions for an injection endpoint method
*/
Expand Down Expand Up @@ -55,7 +57,7 @@ public function addParameter($name, $class = null, $isRequired = null, $default
$this->parameters[] = array(
$name,
$class,
($isRequired == null) ? true : false,
self::detectMethodRequirement($isRequired),
$default,
);

Expand All @@ -69,4 +71,51 @@ public function getParameters()
{
return $this->parameters;
}

/**
*
* @param mixed $requirement
* @return int
*/
public static function detectMethodRequirement($requirement)
{
if (is_bool($requirement)) {
return $requirement ? Di::METHOD_IS_REQUIRED : Di::METHOD_IS_OPTIONAL;
}

if (null === $requirement) {
//This is mismatch to ClassDefinition::addMethod is it ok ? is optional?
return Di::METHOD_IS_REQUIRED;
}

if (is_int($requirement)) {
return $requirement;
}

if (is_string($requirement)) {
switch (strtolower($requirement)) {
default:
case "require":
case "required":
return Di::METHOD_IS_REQUIRED;
break;
case "aware":
return Di::METHOD_IS_AWARE;
break;
case "optional":
return Di::METHOD_IS_OPTIONAL;
break;
case "constructor":
return Di::MEHTOD_IS_CONSTRUCTOR;
break;
case "instantiator":
return Di::METHOD_IS_INSTANTIATOR;
break;
case "eager":
return Di::METHOD_IS_EAGER;
break;
}
}
return 0;
}
}
23 changes: 18 additions & 5 deletions library/Zend/Di/Definition/ClassDefinition.php
Expand Up @@ -9,6 +9,9 @@

namespace Zend\Di\Definition;

use Zend\Di\Di;
use Zend\Di\Definition\Builder\InjectionMethod;

/**
* Class definitions for a single class
*/
Expand Down Expand Up @@ -71,15 +74,21 @@ public function setSupertypes(array $supertypes)

/**
* @param string $method
* @param bool|null $isRequired
* @param mixed|bool|null $isRequired
* @return self
*/
public function addMethod($method, $isRequired = null)
{
if ($isRequired === null) {
$isRequired = ($method === '__construct') ? true : false;
if ($isRequired === null) {
if ($method === '__construct') {
$methodRequirementType = Di::METHOD_IS_CONSTRUCTOR;
}
$methodRequirementType = Di::METHOD_IS_OPTIONAL;
} else {
$methodRequirementType = InjectionMethod::detectMethodRequirement($isRequired);
}
$this->methods[$method] = (bool) $isRequired;

$this->methods[$method] = $methodRequirementType;

return $this;
}
Expand All @@ -93,7 +102,11 @@ public function addMethod($method, $isRequired = null)
public function addMethodParameter($method, $parameterName, array $parameterInfo)
{
if (!array_key_exists($method, $this->methods)) {
$this->methods[$method] = ($method === '__construct') ? true : false;
if ($method === '__construct') {
$this->methods[$method] = Di::METHOD_IS_CONSTRUCTOR;
} else {
$this->methods[$method] = Di::METHOD_IS_OPTIONAL;
}
}

if (!array_key_exists($method, $this->methodParameters)) {
Expand Down
11 changes: 6 additions & 5 deletions library/Zend/Di/Definition/RuntimeDefinition.php
Expand Up @@ -12,7 +12,7 @@
use Zend\Code\Annotation\AnnotationCollection;
use Zend\Code\Reflection;
use Zend\Di\Definition\Annotation;

use Zend\Di\Di;
/**
* Class definitions based on runtime reflection
*/
Expand Down Expand Up @@ -248,7 +248,7 @@ protected function processClass($class)
}

if ($rClass->hasMethod('__construct')) {
$def['methods']['__construct'] = true; // required
$def['methods']['__construct'] = Di::METHOD_IS_CONSTRUCTOR; // required
$this->processParams($def, $rClass, $rClass->getMethod('__construct'));
}

Expand All @@ -266,7 +266,8 @@ protected function processClass($class)
if (($annotations instanceof AnnotationCollection)
&& $annotations->hasAnnotation('Zend\Di\Definition\Annotation\Inject')) {

$def['methods'][$methodName] = true;
// use '@inject' and search for parameters
$def['methods'][$methodName] = Di::METHOD_IS_EAGER;
$this->processParams($def, $rClass, $rMethod);
continue;
}
Expand All @@ -278,7 +279,7 @@ protected function processClass($class)
foreach ($methodPatterns as $methodInjectorPattern) {
preg_match($methodInjectorPattern, $methodName, $matches);
if ($matches) {
$def['methods'][$methodName] = false; // check ot see if this is required?
$def['methods'][$methodName] = Di::METHOD_IS_OPTIONAL; // check ot see if this is required?
$this->processParams($def, $rClass, $rMethod);
continue 2;
}
Expand All @@ -304,7 +305,7 @@ protected function processClass($class)
// constructor not allowed in interfaces
continue;
}
$def['methods'][$rMethod->getName()] = true;
$def['methods'][$rMethod->getName()] = Di::METHOD_IS_AWARE;
$this->processParams($def, $rClass, $rMethod);
}
continue 2;
Expand Down

0 comments on commit e3af457

Please sign in to comment.