-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[DependencyInjection] resolve parameters in inlined factories on compile #13455
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
Closed
Closed
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
31 changes: 31 additions & 0 deletions
31
src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container21.php
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
require_once __DIR__.'/../includes/classes.php'; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
$container = new ContainerBuilder(); | ||
|
||
$container->setParameter('factory.service', 'foo_factory'); | ||
$container->setParameter('factory.class', 'FooFactory'); | ||
$container->setParameter('factory.method', 'createFoo'); | ||
|
||
$container | ||
->register('foo', 'Foo') | ||
->setFactory(array('%factory.class%', '%factory.method%')) | ||
->setPublic(false) | ||
; | ||
$container | ||
->register('bar', 'Bar') | ||
->setFactory(array(new Definition('%factory.class%'), '%factory.method%')) | ||
->setPublic(false) | ||
; | ||
$container | ||
->register('foobar', 'Foobar') | ||
->addMethodCall('setFoo', array(new Reference('foo'))) | ||
->addMethodCall('setBar', array(new Reference('bar'))) | ||
; | ||
|
||
return $container; |
127 changes: 127 additions & 0 deletions
127
src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services21.php
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 |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException; | ||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; | ||
use Symfony\Component\DependencyInjection\Exception\LogicException; | ||
use Symfony\Component\DependencyInjection\Exception\RuntimeException; | ||
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag; | ||
|
||
/** | ||
* ProjectServiceContainer | ||
* | ||
* This class has been auto-generated | ||
* by the Symfony Dependency Injection Component. | ||
*/ | ||
class ProjectServiceContainer extends Container | ||
{ | ||
private $parameters; | ||
private $targetDirs = array(); | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->parameters = $this->getDefaultParameters(); | ||
|
||
$this->services = | ||
$this->scopedServices = | ||
$this->scopeStacks = array(); | ||
|
||
$this->set('service_container', $this); | ||
|
||
$this->scopes = array(); | ||
$this->scopeChildren = array(); | ||
$this->methodMap = array( | ||
'foobar' => 'getFoobarService', | ||
); | ||
|
||
$this->aliases = array(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function compile() | ||
{ | ||
throw new LogicException('You cannot compile a dumped frozen container.'); | ||
} | ||
|
||
/** | ||
* Gets the 'foobar' service. | ||
* | ||
* This service is shared. | ||
* This method always returns the same instance of the service. | ||
* | ||
* @return \Foobar A Foobar instance. | ||
*/ | ||
protected function getFoobarService() | ||
{ | ||
$this->services['foobar'] = $instance = new \Foobar(); | ||
|
||
$instance->setFoo(\FooFactory::createFoo()); | ||
$instance->setBar(call_user_func(array(new \FooFactory(), 'createFoo'))); | ||
|
||
return $instance; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getParameter($name) | ||
{ | ||
$name = strtolower($name); | ||
|
||
if (!(isset($this->parameters[$name]) || array_key_exists($name, $this->parameters))) { | ||
throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name)); | ||
} | ||
|
||
return $this->parameters[$name]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasParameter($name) | ||
{ | ||
$name = strtolower($name); | ||
|
||
return isset($this->parameters[$name]) || array_key_exists($name, $this->parameters); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setParameter($name, $value) | ||
{ | ||
throw new LogicException('Impossible to call set() on a frozen ParameterBag.'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getParameterBag() | ||
{ | ||
if (null === $this->parameterBag) { | ||
$this->parameterBag = new FrozenParameterBag($this->parameters); | ||
} | ||
|
||
return $this->parameterBag; | ||
} | ||
|
||
/** | ||
* Gets the default parameters. | ||
* | ||
* @return array An array of the default parameters | ||
*/ | ||
protected function getDefaultParameters() | ||
{ | ||
return array( | ||
'factory.service' => 'foo_factory', | ||
'factory.class' => 'FooFactory', | ||
'factory.method' => 'createFoo', | ||
); | ||
} | ||
} |
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.
Parameters should already have been resolved by the compiler pass at this point actually. You should not need to resolve them here
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.
Are you sure? I was wondering why my config was failing when migrating to the new syntax. And something similar is already done when using the old syntax: https://github.com/symfony/symfony/blob/2.6/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php#L1331-1339
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.
hmm, the factory method is indeed not resolved by the ResolveParameterPlaceHoldersPass.
and btw, the ContainerBuilder does not resolve the factoryMethod when instantiating, so using a parameter for the factoryMethod was not working consistently. It was a side effect of the dumping logic
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.
Hm, would it make more sense to completely move the logic to the compiler pass? And is it actually possible to move it in a BC way?