Skip to content

Commit

Permalink
Merge pull request #140 from mnapoli/DefinitionSourceRefactoring
Browse files Browse the repository at this point in the history
Refactoring of the definition sources
  • Loading branch information
mnapoli committed Dec 12, 2013
2 parents 781dfde + 8bd26fa commit 0edef76
Show file tree
Hide file tree
Showing 46 changed files with 709 additions and 891 deletions.
15 changes: 4 additions & 11 deletions src/DI/Container.php
Expand Up @@ -96,7 +96,10 @@ public function __construct(
public function get($name)
{
if (! is_string($name)) {
throw new InvalidArgumentException("The name parameter must be of type string");
throw new InvalidArgumentException(sprintf(
'The name parameter must be of type string, %s given',
is_object($name) ? get_class($name) : gettype($name)
));
}

// Try to find the entry in the map
Expand Down Expand Up @@ -196,16 +199,6 @@ public function set($name, $value)
$this->definitionManager->addDefinition($definition);
}

/**
* Add definitions from an array
*
* @param array $definitions
*/
public function addDefinitions(array $definitions)
{
$this->definitionManager->addArrayDefinitions($definitions);
}

/**
* @return DefinitionManager
*/
Expand Down
52 changes: 37 additions & 15 deletions src/DI/ContainerBuilder.php
Expand Up @@ -10,7 +10,10 @@
namespace DI;

use DI\Definition\DefinitionManager;
use DI\Definition\Source\DefinitionSource;
use DI\Definition\Source\AnnotationDefinitionSource;
use DI\Definition\Source\ChainableDefinitionSource;
use DI\Definition\Source\PHPFileDefinitionSource;
use DI\Definition\Source\ReflectionDefinitionSource;
use Doctrine\Common\Cache\Cache;
use InvalidArgumentException;
use ProxyManager\Configuration;
Expand Down Expand Up @@ -67,10 +70,10 @@ class ContainerBuilder
private $wrapperContainer;

/**
* Source of definitions for the container.
* @var DefinitionSource[]
* Files of definitions for the container.
* @var string[]
*/
private $definitionSources = array();
private $files = array();

/**
* Build a container configured for the dev environment.
Expand All @@ -96,14 +99,35 @@ public function __construct($containerClass = 'DI\Container')
*/
public function build()
{
// Definition sources
$source = null;
foreach ($this->files as $file) {
$newSource = new PHPFileDefinitionSource($file);
// Chain file sources
if ($source) {
$newSource->chain($source);
}
$source = $newSource;
}
if ($this->useAnnotations) {
if ($source) {
$source->chain(new AnnotationDefinitionSource());
} else {
$source = new AnnotationDefinitionSource();
}
} elseif ($this->useReflection) {
if ($source) {
$source->chain(new ReflectionDefinitionSource());
} else {
$source = new ReflectionDefinitionSource();
}
}

// Definition manager
$definitionManager = new DefinitionManager($this->useReflection, $this->useAnnotations);
$definitionManager = new DefinitionManager($source);
if ($this->cache) {
$definitionManager->setCache($this->cache);
}
foreach ($this->definitionSources as $definitionSource) {
$definitionManager->addDefinitionSource($definitionSource);
}

// Proxy factory
$proxyFactory = $this->buildProxyFactory();
Expand Down Expand Up @@ -192,16 +216,13 @@ public function wrapContainer(ContainerInterface $otherContainer)
}

/**
* Add definitions to the container by adding a source of definitions.
*
* Do not add ReflectionDefinitionSource or AnnotationDefinitionSource manually, they should be
* handled with useReflection() and useAnnotations().
* Add a file containing definitions to the container.
*
* @param DefinitionSource $definitionSource
* @param string $file
*/
public function addDefinitions(DefinitionSource $definitionSource)
public function addDefinitions($file)
{
$this->definitionSources[] = $definitionSource;
$this->files[] = $file;
}

/**
Expand All @@ -210,6 +231,7 @@ public function addDefinitions(DefinitionSource $definitionSource)
private function buildProxyFactory()
{
$config = new Configuration();
// TODO use non-deprecated method
$config->setAutoGenerateProxies(true);

if ($this->writeProxiesToFile) {
Expand Down
18 changes: 1 addition & 17 deletions src/DI/Definition/AliasDefinition.php
Expand Up @@ -12,7 +12,7 @@
use DI\Scope;

/**
* Defines an alias from an entry to another
* Defines an alias from an entry to another.
*
* @author Matthieu Napoli <matthieu@mnapoli.fr>
*/
Expand Down Expand Up @@ -64,27 +64,11 @@ public function getTargetEntryName()
return $this->targetEntryName;
}

/**
* {@inheritdoc}
*/
public function merge(Definition $definition)
{
throw new \BadMethodCallException("Impossible to merge an AliasDefinition with another definition");
}

/**
* {@inheritdoc}
*/
public function isCacheable()
{
return true;
}

/**
* {@inheritdoc}
*/
public static function isMergeable()
{
return false;
}
}
16 changes: 0 additions & 16 deletions src/DI/Definition/CallableDefinition.php
Expand Up @@ -73,27 +73,11 @@ public function getCallable()
return $this->callable;
}

/**
* {@inheritdoc}
*/
public function merge(Definition $definition)
{
throw new \BadMethodCallException("Impossible to merge a CallableDefinition with another definition");
}

/**
* {@inheritdoc}
*/
public function isCacheable()
{
return false;
}

/**
* {@inheritdoc}
*/
public static function isMergeable()
{
return false;
}
}
49 changes: 29 additions & 20 deletions src/DI/Definition/ClassDefinition.php
Expand Up @@ -19,7 +19,7 @@
*
* @author Matthieu Napoli <matthieu@mnapoli.fr>
*/
class ClassDefinition implements Definition
class ClassDefinition implements MergeableDefinition
{
/**
* Entry name (most of the time, same as $classname)
Expand Down Expand Up @@ -130,6 +130,15 @@ public function getPropertyInjections()
return $this->propertyInjections;
}

/**
* @param string $propertyName
* @return PropertyInjection
*/
public function getPropertyInjection($propertyName)
{
return isset($this->propertyInjections[$propertyName]) ? $this->propertyInjections[$propertyName] : null;
}

/**
* @param PropertyInjection $propertyInjection
*/
Expand All @@ -146,6 +155,15 @@ public function getMethodInjections()
return $this->methodInjections;
}

/**
* @param string $methodName
* @return MethodInjection|null
*/
public function getMethodInjection($methodName)
{
return isset($this->methodInjections[$methodName]) ? $this->methodInjections[$methodName] : null;
}

/**
* @param MethodInjection $methodInjection
*/
Expand Down Expand Up @@ -194,21 +212,23 @@ public function isLazy()
/**
* {@inheritdoc}
*/
public function merge(Definition $definition)
public function merge(MergeableDefinition $definition)
{
if (!$definition instanceof ClassDefinition) {
throw new DefinitionException("DI definition conflict: there are 2 different definitions for '"
. $definition->getName() . "' that are incompatible, they are not of the same type");
throw new DefinitionException(
"DI definition conflict: there are 2 different definitions for '" . $this->getName()
. "' that are incompatible, they are not of the same type"
);
}

// The latter prevails
if ($definition->className !== null) {
// The current prevails
if ($this->className === null) {
$this->className = $definition->className;
}
if ($definition->scope !== null) {
if ($this->scope === null) {
$this->scope = $definition->scope;
}
if ($definition->lazy !== null) {
if ($this->lazy === null) {
$this->lazy = $definition->lazy;
}

Expand All @@ -225,10 +245,7 @@ public function merge(Definition $definition)

// Merge property injections
foreach ($definition->getPropertyInjections() as $propertyName => $propertyInjection) {
if (array_key_exists($propertyName, $this->propertyInjections)) {
// Merge
$this->propertyInjections[$propertyName]->merge($propertyInjection);
} else {
if (! array_key_exists($propertyName, $this->propertyInjections)) {
// Add
$this->propertyInjections[$propertyName] = $propertyInjection;
}
Expand All @@ -253,12 +270,4 @@ public function isCacheable()
{
return true;
}

/**
* {@inheritdoc}
*/
public static function isMergeable()
{
return true;
}
}
17 changes: 15 additions & 2 deletions src/DI/Definition/ClassInjection/MethodInjection.php
Expand Up @@ -52,15 +52,28 @@ public function getParameters()
return $this->parameters;
}

/**
* @param int $index Position of the parameter (starting at 0)
* @return mixed|null Value to inject, or null if no injection defined.
*/
public function getParameter($index)
{
if (! isset($this->parameters[$index])) {
return null;
}

return $this->parameters[$index];
}

/**
* Merge another definition into the current definition.
*
* In case of conflicts, the latter prevails (i.e. the other definition)
* In case of conflicts, the current definition prevails.
*
* @param MethodInjection $methodInjection
*/
public function merge(MethodInjection $methodInjection)
{
$this->parameters = $methodInjection->parameters + $this->parameters;
$this->parameters = $this->parameters + $methodInjection->parameters;
}
}
14 changes: 0 additions & 14 deletions src/DI/Definition/ClassInjection/PropertyInjection.php
Expand Up @@ -53,18 +53,4 @@ public function getValue()
{
return $this->value;
}

/**
* Merge another definition into the current definition
*
* In case of conflicts, the latter prevails (i.e. the other definition)
*
* @param PropertyInjection $propertyInjection
*/
public function merge(PropertyInjection $propertyInjection)
{
if ($propertyInjection->value !== null) {
$this->value = $propertyInjection->value;
}
}
}
16 changes: 0 additions & 16 deletions src/DI/Definition/Definition.php
Expand Up @@ -32,26 +32,10 @@ public function getName();
*/
public function getScope();

/**
* Merge another definition into the current definition
*
* In case of conflicts, the latter prevails (i.e. the other definition)
*
* @param Definition $definition
*/
public function merge(Definition $definition);

/**
* Returns true if the definition can be cached, false otherwise
*
* @return bool
*/
public function isCacheable();

/**
* Returns true if the definition is mergeable with other definitions
*
* @return bool
*/
public static function isMergeable();
}

0 comments on commit 0edef76

Please sign in to comment.