-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DI] Add getter injection #20973
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
Merged
Merged
[DI] Add getter injection #20973
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
use Symfony\Component\Config\Resource\ResourceInterface; | ||
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface; | ||
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\RealServiceInstantiator; | ||
use Symfony\Component\DependencyInjection\LazyProxy\GetterProxyInterface; | ||
use Symfony\Component\ExpressionLanguage\Expression; | ||
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface; | ||
|
||
|
@@ -890,6 +891,9 @@ private function createService(Definition $definition, $id, $tryProxy = true) | |
$arguments = $this->resolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getArguments()))); | ||
|
||
if (null !== $factory = $definition->getFactory()) { | ||
if ($definition->getOverriddenGetters()) { | ||
throw new RuntimeException(sprintf('Cannot create service "%s": factories and overridden getters are incompatible with each other.', $id)); | ||
} | ||
if (is_array($factory)) { | ||
$factory = array($this->resolveServices($parameterBag->resolveValue($factory[0])), $factory[1]); | ||
} elseif (!is_string($factory)) { | ||
|
@@ -908,11 +912,31 @@ private function createService(Definition $definition, $id, $tryProxy = true) | |
} else { | ||
$r = new \ReflectionClass($parameterBag->resolveValue($definition->getClass())); | ||
|
||
$service = null === $r->getConstructor() ? $r->newInstance() : $r->newInstanceArgs($arguments); | ||
|
||
if (!$definition->isDeprecated() && 0 < strpos($r->getDocComment(), "\n * @deprecated ")) { | ||
@trigger_error(sprintf('The "%s" service relies on the deprecated "%s" class. It should either be deprecated or its implementation upgraded.', $id, $r->name), E_USER_DEPRECATED); | ||
} | ||
if ($definition->getOverriddenGetters()) { | ||
static $salt; | ||
if (null === $salt) { | ||
$salt = str_replace('.', '', uniqid('', true)); | ||
} | ||
$service = sprintf('%s implements \\%s { private $container%4$s; private $values%4$s; %s }', $r->name, GetterProxyInterface::class, $this->generateOverriddenGetters($id, $definition, $r, $salt), $salt); | ||
if (!class_exists($proxyClass = 'SymfonyProxy_'.md5($service), false)) { | ||
eval(sprintf('class %s extends %s', $proxyClass, $service)); | ||
} | ||
$r = new \ReflectionClass($proxyClass); | ||
$constructor = $r->getConstructor(); | ||
if ($constructor && !defined('HHVM_VERSION') && $constructor->getDeclaringClass()->isInternal()) { | ||
$constructor = null; | ||
} | ||
$service = $constructor ? $r->newInstanceWithoutConstructor() : $r->newInstanceArgs($arguments); | ||
call_user_func(\Closure::bind(function ($c, $v, $s) { $this->{'container'.$s} = $c; $this->{'values'.$s} = $v; }, $service, $service), $this, $definition->getOverriddenGetters(), $salt); | ||
if ($constructor) { | ||
$constructor->invokeArgs($service, $arguments); | ||
} | ||
} else { | ||
$service = null === $r->getConstructor() ? $r->newInstance() : $r->newInstanceArgs($arguments); | ||
} | ||
} | ||
|
||
if ($tryProxy || !$definition->isLazy()) { | ||
|
@@ -1155,6 +1179,102 @@ public function getEnvCounters() | |
return $this->envCounters; | ||
} | ||
|
||
private function generateOverriddenGetters($id, Definition $definition, \ReflectionClass $class, $salt) | ||
{ | ||
if ($class->isFinal()) { | ||
throw new RuntimeException(sprintf('Unable to configure getter injection for service "%s": class "%s" cannot be marked as final.', $id, $class->name)); | ||
} | ||
$getters = ''; | ||
foreach ($definition->getOverriddenGetters() as $name => $returnValue) { | ||
$r = self::getGetterReflector($class, $name, $id, $type); | ||
$visibility = $r->isProtected() ? 'protected' : 'public'; | ||
$name = var_export($name, true); | ||
$getters .= <<<EOF | ||
|
||
{$visibility} function {$r->name}(){$type} { | ||
\$c = \$this->container{$salt}; | ||
\$b = \$c->getParameterBag(); | ||
\$v = \$this->values{$salt}[{$name}]; | ||
|
||
foreach (\$c->getServiceConditionals(\$v) as \$s) { | ||
if (!\$c->has(\$s)) { | ||
return parent::{$r->name}(); | ||
} | ||
} | ||
|
||
return \$c->resolveServices(\$b->unescapeValue(\$b->resolveValue(\$v))); | ||
} | ||
EOF; | ||
} | ||
|
||
return $getters; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public static function getGetterReflector(\ReflectionClass $class, $name, $id, &$type) | ||
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. and please mark this method as |
||
{ | ||
if (!$class->hasMethod($name)) { | ||
throw new RuntimeException(sprintf('Unable to configure getter injection for service "%s": method "%s::%s" does not exist.', $id, $class->name, $name)); | ||
} | ||
$r = $class->getMethod($name); | ||
if ($r->isPrivate()) { | ||
throw new RuntimeException(sprintf('Unable to configure getter injection for service "%s": method "%s::%s" must be public or protected.', $id, $class->name, $r->name)); | ||
} | ||
if ($r->isStatic()) { | ||
throw new RuntimeException(sprintf('Unable to configure getter injection for service "%s": method "%s::%s" cannot be static.', $id, $class->name, $r->name)); | ||
} | ||
if ($r->isFinal()) { | ||
throw new RuntimeException(sprintf('Unable to configure getter injection for service "%s": method "%s::%s" cannot be marked as final.', $id, $class->name, $r->name)); | ||
} | ||
if ($r->returnsReference()) { | ||
throw new RuntimeException(sprintf('Unable to configure getter injection for service "%s": method "%s::%s" cannot return by reference.', $id, $class->name, $r->name)); | ||
} | ||
if (0 < $r->getNumberOfParameters()) { | ||
throw new RuntimeException(sprintf('Unable to configure getter injection for service "%s": method "%s::%s" cannot have any arguments.', $id, $class->name, $r->name)); | ||
} | ||
if ($type = method_exists($r, 'getReturnType') ? $r->getReturnType() : null) { | ||
$type = ': '.($type->allowsNull() ? '?' : '').self::generateTypeHint($type, $r); | ||
} | ||
|
||
return $r; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public static function generateTypeHint($type, \ReflectionFunctionAbstract $r) | ||
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. should be internal |
||
{ | ||
if (is_string($type)) { | ||
$name = $type; | ||
|
||
if ('callable' === $name || 'array' === $name) { | ||
return $name; | ||
} | ||
} else { | ||
$name = $type instanceof \ReflectionNamedType ? $type->getName() : $type->__toString(); | ||
|
||
if ($type->isBuiltin()) { | ||
return $name; | ||
} | ||
} | ||
$lcName = strtolower($name); | ||
|
||
if ('self' !== $lcName && 'parent' !== $lcName) { | ||
return '\\'.$name; | ||
} | ||
if (!$r instanceof \ReflectionMethod) { | ||
return; | ||
} | ||
if ('self' === $lcName) { | ||
return '\\'.$r->getDeclaringClass()->name; | ||
} | ||
if ($parent = $r->getDeclaringClass()->getParentClass()) { | ||
return '\\'.$parent->name; | ||
} | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
|
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
rather than returning the type by reference, it would be more readable to handle it outside
getGetterReflector
IMO (it only depend on the returned value anywayThere 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.
getGetterReflector
is used in two places (ContainerBuilder & PhpDumper), so I'd prefer sharing the implementation. Even short, it's not trivial.