New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rework how nested definitions are handled (fix #501 & #487) #540
Changes from all commits
2b390bd
32fb5b3
d93e035
759207d
e771b2e
fd7bde0
ca69945
b6ff39d
c0c467b
0616139
e3d002a
1940ca1
baf7de1
02231ae
b4ede14
79712dc
7cc2ca2
f902438
f568561
73ba9f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,6 @@ | |
|
||
namespace DI\Definition; | ||
|
||
use DI\Definition\Helper\DefinitionHelper; | ||
|
||
/** | ||
* Definition of an array containing values or references. | ||
* | ||
|
@@ -18,19 +16,15 @@ class ArrayDefinition implements Definition | |
* Entry name. | ||
* @var string | ||
*/ | ||
private $name; | ||
private $name = ''; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $values; | ||
|
||
/** | ||
* @param string $name Entry name | ||
*/ | ||
public function __construct(string $name, array $values) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There will be the same pattern on several definition classes: by making the name optional that allows me to get rid of several It's a tiny bit less clean, but it removes a lot of code and classes, it simplifies everything and also it makes a little sense because nested definitions don't have names, so… the name is not really mandatory after all. |
||
public function __construct(array $values) | ||
{ | ||
$this->name = $name; | ||
$this->values = $values; | ||
} | ||
|
||
|
@@ -39,11 +33,21 @@ public function getName() : string | |
return $this->name; | ||
} | ||
|
||
public function setName(string $name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function getValues() : array | ||
{ | ||
return $this->values; | ||
} | ||
|
||
public function replaceNestedDefinitions(callable $replacer) | ||
{ | ||
$this->values = array_map($replacer, $this->values); | ||
} | ||
|
||
public function __toString() | ||
{ | ||
$str = '[' . PHP_EOL; | ||
|
@@ -55,8 +59,8 @@ public function __toString() | |
|
||
$str .= ' ' . $key . ' => '; | ||
|
||
if ($value instanceof DefinitionHelper) { | ||
$str .= str_replace(PHP_EOL, PHP_EOL . ' ', $value->getDefinition('')); | ||
if ($value instanceof Definition) { | ||
$str .= str_replace(PHP_EOL, PHP_EOL . ' ', $value); | ||
} else { | ||
$str .= var_export($value, true); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,16 @@ interface Definition extends RequestedEntry | |
*/ | ||
public function getName() : string; | ||
|
||
/** | ||
* Set the name of the entry in the container. | ||
*/ | ||
public function setName(string $name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the new method that allows to get rid of several |
||
|
||
/** | ||
* Apply a callable that replaces the definitions nested in this definition. | ||
*/ | ||
public function replaceNestedDefinitions(callable $replacer); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the method that allows the new |
||
|
||
/** | ||
* Definitions can be cast to string for debugging information. | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
|
||
namespace DI\Definition\Dumper; | ||
|
||
use DI\Definition\EntryReference; | ||
use DI\Definition\Definition; | ||
use DI\Definition\ObjectDefinition; | ||
use DI\Definition\ObjectDefinition\MethodInjection; | ||
use ReflectionException; | ||
|
@@ -72,8 +72,8 @@ private function dumpProperties(ObjectDefinition $definition) : string | |
|
||
foreach ($definition->getPropertyInjections() as $propertyInjection) { | ||
$value = $propertyInjection->getValue(); | ||
if ($value instanceof EntryReference) { | ||
$valueStr = sprintf('get(%s)', $value->getName()); | ||
if ($value instanceof Definition) { | ||
$valueStr = (string) $value; | ||
} else { | ||
$valueStr = var_export($value, true); | ||
} | ||
|
@@ -109,11 +109,13 @@ private function dumpMethodParameters(string $className, MethodInjection $method | |
if (array_key_exists($index, $definitionParameters)) { | ||
$value = $definitionParameters[$index]; | ||
|
||
if ($value instanceof EntryReference) { | ||
$args[] = sprintf('$%s = get(%s)', $parameter->getName(), $value->getName()); | ||
if ($value instanceof Definition) { | ||
$valueStr = (string) $value; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All these little changes also mean that nested definitions are better supported when debugging and in exception messages. |
||
} else { | ||
$args[] = sprintf('$%s = %s', $parameter->getName(), var_export($value, true)); | ||
$valueStr = var_export($value, true); | ||
} | ||
$args[] = sprintf('$%s = %s', $parameter->getName(), $valueStr); | ||
|
||
continue; | ||
} | ||
|
||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cf. the description of the PR but there are no
DefinitionHelper
anymore once a definition is returned by theDefinitionSource
(the definition is 100% prepared by the source, meaning less work for the compiler and definition resolvers).