This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of git://github.com/zendframework/zf2
- Loading branch information
Showing
73 changed files
with
4,119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
namespace Zend\Di; | ||
|
||
use Traversable; | ||
|
||
class Configuration | ||
{ | ||
protected $data = array(); | ||
|
||
/** | ||
* @var Zend\Di\DependencyInjector | ||
*/ | ||
protected $di = null; | ||
|
||
public function __construct($data) | ||
{ | ||
if ($data instanceof Traversable) { | ||
if (method_exists($data, 'toArray')) { | ||
$data = $data->toArray(); | ||
} else { | ||
$data = iterator_to_array($data, true); | ||
} | ||
} elseif (!is_array($data)) { | ||
throw new Exception\InvalidArgumentException('Configuration data must be of type Zend\Config\Config or an array'); | ||
} | ||
$this->data = $data; | ||
} | ||
|
||
public function configure(DependencyInjector $di) | ||
{ | ||
if (isset($this->data['definition'])) { | ||
$this->configureDefinition($di, $this->data['definition']); | ||
} | ||
|
||
if (isset($this->data['definitions'])) { | ||
$this->configureDefinitions($di, $this->data['definitions']); | ||
} | ||
|
||
/* | ||
if (isset($this->data['compiler'])) { | ||
$this->configureCompiler($di, $this->data['compiler']); | ||
} | ||
*/ | ||
|
||
if (isset($this->data['instance'])) { | ||
$this->configureInstance($di, $this->data['instance']); | ||
} | ||
|
||
} | ||
|
||
public function configureDefinitions(DependencyInjector $di, $definitionsData) | ||
{ | ||
if ($di->hasDefinition()) { | ||
if (!$di->getDefinition() instanceof Definition\AggregateDefinition) { | ||
throw new Exception\InvalidArgumentException('In order to configure multiple definitions, the primary definition must not be set, or must be of type AggregateDefintion'); | ||
} | ||
} else { | ||
$di->setDefinition($di->createDefinition('Zend\Di\Definition\AggregateDefinition')); | ||
} | ||
|
||
foreach ($definitionsData as $definitionData) { | ||
$this->configureDefinition($di, $definitionData); | ||
} | ||
} | ||
|
||
public function configureDefinition(DependencyInjector $di, $definitionData) | ||
{ | ||
if ($di->hasDefinition()) { | ||
$aggregateDef = $di->getDefinition(); | ||
if (!$aggregateDef instanceof Definition\AggregateDefinition) { | ||
throw new Exception\InvalidArgumentException('In order to configure multiple definitions, the primary definition must not be set, or must be of type AggregateDefintion'); | ||
} | ||
} /* else { | ||
$aggregateDef = $di->createDefinition('Zend\Di\Definition\AggregateDefinition'); | ||
$di->setDefinition($aggregateDef); | ||
} */ | ||
|
||
if (isset($definitionData['class'])) { | ||
$definition = $di->createDefinition($definitionData['class']); | ||
unset($definitionData['class']); | ||
if ($definition instanceof Definition\BuilderDefinition) { | ||
$definition->createClassesFromArray($definitionData); | ||
} else { | ||
// @todo other types | ||
} | ||
} | ||
|
||
if (isset($aggregateDef)) { | ||
$aggregateDef->addDefinition($definition); | ||
} else { | ||
$di->setDefinition($definition); | ||
} | ||
} | ||
|
||
public function configureInstance(DependencyInjector $di, $instanceData) | ||
{ | ||
$im = $di->getInstanceManager(); | ||
|
||
foreach ($instanceData as $target => $data) { | ||
switch (strtolower($target)) { | ||
case 'aliases': | ||
case 'alias': | ||
foreach ($data as $aliasName => $className) { | ||
$im->addAlias($aliasName, $className); | ||
} | ||
break; | ||
case 'properties': | ||
case 'property': | ||
foreach ($data as $classOrAlias => $properties) { | ||
foreach ($properties as $propName => $propValue) { | ||
$im->setProperty($classOrAlias, $propName, $propValue); | ||
} | ||
} | ||
break; | ||
case 'preferences': | ||
case 'preferredinstances': | ||
case 'preferredinstance': | ||
foreach ($data as $classOrAlias => $preferredValueOrValues) { | ||
if (is_array($preferredValueOrValues)) { | ||
foreach ($preferredValueOrValues as $preferredValue) { | ||
$im->addPreferredInstance($classOrAlias, $preferredValue); | ||
} | ||
} else { | ||
$im->addPreferredInstance($classOrAlias, $preferredValueOrValues); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Zend\Di; | ||
|
||
interface Definition | ||
{ | ||
public function getClasses(); | ||
public function hasClass($class); | ||
public function getClassSupertypes($class); | ||
public function getInstantiator($class); | ||
public function hasInjectionMethods($class); | ||
public function getInjectionMethods($class); | ||
public function hasInjectionMethod($class, $method); | ||
public function getInjectionMethodParameters($class, $method); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
namespace Zend\Di\Definition; | ||
|
||
use Zend\Di\Definition; | ||
|
||
class AggregateDefinition implements Definition | ||
{ | ||
|
||
protected $definitions = array(); | ||
|
||
|
||
public function addDefinition(Definition $definition) | ||
{ | ||
$this->definitions[] = $definition; | ||
} | ||
|
||
public function getClasses() | ||
{ | ||
$classes = array(); | ||
foreach ($this->definitions as $definition) { | ||
$classes = array_merge($classes, $definition->getClasses()); | ||
} | ||
return $classes; | ||
} | ||
|
||
public function hasClass($class) | ||
{ | ||
foreach ($this->definitions as $definition) { | ||
if ($definition->hasClass($class)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public function getClassSupertypes($class) | ||
{ | ||
$superTypes = array(); | ||
foreach ($this->definitions as $definition) { | ||
$superTypes = array_merge($superTypes, $definition->getClassSupertypes()); | ||
} | ||
return $superTypes; | ||
} | ||
|
||
public function getInstantiator($class) | ||
{ | ||
foreach ($this->definitions as $definition) { | ||
if ($definition->hasClass($class)) { | ||
return $definition->getInstantiator($class); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public function hasInjectionMethods($class) | ||
{ | ||
foreach ($this->definitions as $definition) { | ||
if ($definition->hasClass($class)) { | ||
return $definition->hasInjectionMethods($class); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public function hasInjectionMethod($class, $method) | ||
{ | ||
foreach ($this->definitions as $definition) { | ||
if ($definition->hasClass($class)) { | ||
return $definition->hasInjectionMethod($class, $method); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public function getInjectionMethods($class) | ||
{ | ||
foreach ($this->definitions as $definition) { | ||
if ($definition->hasClass($class)) { | ||
return $definition->getInjectionMethods($class); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public function getInjectionMethodParameters($class, $method) | ||
{ | ||
foreach ($this->definitions as $definition) { | ||
if ($definition->hasClass($class) && $definition->hasInjectionMethod($class, $method)) { | ||
return $definition->getInjectionMethodParameters($class, $method); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
namespace Zend\Di\Definition; | ||
|
||
use Zend\Di\Definition; | ||
|
||
class ArrayDefinition implements Definition | ||
{ | ||
|
||
protected $dataArray = array(); | ||
|
||
public function __construct(Array $dataArray) | ||
{ | ||
$this->dataArray = $dataArray; | ||
} | ||
|
||
public function getClasses() | ||
{ | ||
return array_keys($this->dataArray); | ||
} | ||
|
||
public function hasClass($class) | ||
{ | ||
return array_key_exists($class, $this->dataArray); | ||
} | ||
|
||
public function getClassSupertypes($class) | ||
{ | ||
if (!isset($this->dataArray[$class])) { | ||
return array(); | ||
} | ||
|
||
if (!isset($this->dataArray[$class]['superTypes'])) { | ||
return array(); | ||
} | ||
|
||
return $this->dataArray[$class]['superTypes']; | ||
} | ||
|
||
public function getInstantiator($class) | ||
{ | ||
if (!isset($this->dataArray[$class])) { | ||
return null; | ||
} | ||
|
||
if (!isset($this->dataArray[$class]['instantiator'])) { | ||
return '__construct'; | ||
} | ||
|
||
return $this->dataArray[$class]['instantiator']; | ||
} | ||
|
||
public function hasInjectionMethods($class) | ||
{ | ||
if (!isset($this->dataArray[$class])) { | ||
return array(); | ||
} | ||
|
||
if (!isset($this->dataArray[$class]['injectionMethods'])) { | ||
return array(); | ||
} | ||
|
||
return (count($this->dataArray[$class]['injectionMethods']) > 0); | ||
} | ||
|
||
public function hasInjectionMethod($class, $method) | ||
{ | ||
if (!isset($this->dataArray[$class])) { | ||
return array(); | ||
} | ||
|
||
if (!isset($this->dataArray[$class]['injectionMethods'])) { | ||
return array(); | ||
} | ||
|
||
if (!isset($this->dataArray[$class]['injectionMethods'][$method])) { | ||
return array(); | ||
} | ||
|
||
return array_key_exists($method, $this->dataArray[$class]['injectionMethods']); | ||
} | ||
|
||
public function getInjectionMethods($class) | ||
{ | ||
if (!isset($this->dataArray[$class])) { | ||
return array(); | ||
} | ||
|
||
if (!isset($this->dataArray[$class]['injectionMethods'])) { | ||
return array(); | ||
} | ||
|
||
return array_keys($this->dataArray[$class]['injectionMethods']); | ||
} | ||
|
||
public function getInjectionMethodParameters($class, $method) | ||
{ | ||
if (!isset($this->dataArray[$class])) { | ||
return array(); | ||
} | ||
|
||
if (!isset($this->dataArray[$class]['injectionMethods'])) { | ||
return array(); | ||
} | ||
|
||
if (!isset($this->dataArray[$class]['injectionMethods'][$method])) { | ||
return array(); | ||
} | ||
|
||
return $this->dataArray[$class]['injectionMethods'][$method]; | ||
} | ||
|
||
public function toArray() | ||
{ | ||
return $this->dataArray; | ||
} | ||
|
||
} |
Oops, something went wrong.