-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Container::set() is useless if the entry is an alias #16461
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
Comments
|
Yes but:
$container->set('event_dispatcher', $dispatcher); // if debug=false
$container->set('debug.event_dispatcher', $dispatcher); // if debug=true No great… At all. I don't see a reason why |
I think the proper option should be the be able to erase that when building the container ( |
Why? |
Once the container has been built, it shouldn't be changed so easily IMO (I may be wrong though ; if it is the case, the option of replacing the service is still a good one IMO) |
@Taluu |
@mnapoli Did you find or build a work arround ? I run into the same problems. I suggest the option to set and overwrite the existing alias makes most sense. Let me know if I can be at any help. |
…existing aliases `Container::set()` now overrides any previously alias defined with the same name.
@wpeereboom I just opened a pull request to fix this: #17742 |
@mnapoli Great, thanks |
…g aliases `Container::set()` now overrides any previously alias defined with the same name.
…aliases (mnapoli) This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes #17742). Discussion ---------- [DependencyInjection] Fix #16461 Container::set() replace aliases | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #16461 | License | MIT | Doc PR | - `Container::set()` now overrides any previously alias defined with the same name. Please see #16461 for the background. Example: - given `event_dispatcher` is an alias to `debug.event_dispatcher` - when I run: `$container->set('event_dispatcher', new FakeEventDispatcher)` - *before this patch*: nothing happens - *after this patch*: the `event_dispatcher` is now my fake event dispatcher Commits ------- be85d16 [DependencyInjection] Fix #16461 Let Container::set() replace existing aliases
* 2.3: [DomCrawler] Clarify the value returned by getPhpFiles() [DependencyInjection] Fix #16461 Let Container::set() replace existing aliases Added more exceptions to singularify method
* 2.7: [VarDumper] Fix tests on PHP 7 [DomCrawler] Clarify the value returned by getPhpFiles() [DependencyInjection] Fix #16461 Let Container::set() replace existing aliases avoid (string) catchable fatal error for instances of __PHP_Incomplete_Class remove unnecessary retrieval and setting of data avoid (string) catchable fatal error for __PHP_Incomplete_Class instances sendContent return as parent. [FrameworkBundle] Fix a typo Added more exceptions to singularify method
* 2.8: [VarDumper] Fix tests on PHP 7 [DomCrawler] Clarify the value returned by getPhpFiles() [DependencyInjection] Fix #16461 Let Container::set() replace existing aliases avoid (string) catchable fatal error for instances of __PHP_Incomplete_Class remove unnecessary retrieval and setting of data Update validators.fr.xlf avoid (string) catchable fatal error for __PHP_Incomplete_Class instances sendContent return as parent. [DomCrawler] Remove the overridden getHash() method to prevent problems when cloning the crawler [FrameworkBundle] Fix a typo Added more exceptions to singularify method Add width attribute on SVG [FrameworkBundle] Support autowiring for TranslationInterface [WebProfiler] Fixed styles for search block and menu profiler for IE Edge Conflicts: src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php src/Symfony/Component/DomCrawler/Crawler.php
* 3.0: [VarDumper] Fix tests on PHP 7 [DomCrawler] Clarify the value returned by getPhpFiles() [DependencyInjection] Fix #16461 Let Container::set() replace existing aliases avoid (string) catchable fatal error for instances of __PHP_Incomplete_Class remove unnecessary retrieval and setting of data Update validators.fr.xlf avoid (string) catchable fatal error for __PHP_Incomplete_Class instances sendContent return as parent. [DomCrawler] Remove the overridden getHash() method to prevent problems when cloning the crawler [FrameworkBundle] Fix a typo Added more exceptions to singularify method Add width attribute on SVG [FrameworkBundle] Support autowiring for TranslationInterface [Validator] remove obsolete context and PropertyAccess code [WebProfiler] Fixed styles for search block and menu profiler for IE Edge
In Symfony 2.7.0 - 2.7.9, this line did nothing, since `ezpublish_legacy.kernel` is an alias and `Container::set` never did anything with aliases. In Symfony 2.7.10, setting an alias to a new value basically converts the alias definition to service definition, meaning that `ezpublish_legacy.kernel` will not point to `ezpublish_legacy.kernel.lazy` any more, thus making the alias invalid next time `ezpublish_legacy.kernel.lazy` is rebuilt. See symfony/symfony#16461 for details
In Symfony 2.7.0 - 2.7.9, this line did nothing, since `ezpublish_legacy.kernel` is an alias and `Container::set` never did anything with aliases. In Symfony 2.7.10, setting an alias to a new value basically converts the alias definition to service definition, meaning that `ezpublish_legacy.kernel` will not point to `ezpublish_legacy.kernel.lazy` any more, thus making the alias invalid next time `ezpublish_legacy.kernel.lazy` is rebuilt. See symfony/symfony#16461 for details
This introduced an error in my case. When you have an alias of an alias, it overwrites just the first alias, without cascading. Example: Doctrine default EM
|
* 2.8: [VarDumper] Fix tests on PHP 7 [DomCrawler] Clarify the value returned by getPhpFiles() [DependencyInjection] Fix symfony#16461 Let Container::set() replace existing aliases avoid (string) catchable fatal error for instances of __PHP_Incomplete_Class remove unnecessary retrieval and setting of data Update validators.fr.xlf avoid (string) catchable fatal error for __PHP_Incomplete_Class instances sendContent return as parent. [DomCrawler] Remove the overridden getHash() method to prevent problems when cloning the crawler [FrameworkBundle] Fix a typo Added more exceptions to singularify method Add width attribute on SVG [FrameworkBundle] Support autowiring for TranslationInterface [WebProfiler] Fixed styles for search block and menu profiler for IE Edge Conflicts: src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php src/Symfony/Component/DomCrawler/Crawler.php
* 3.0: [VarDumper] Fix tests on PHP 7 [DomCrawler] Clarify the value returned by getPhpFiles() [DependencyInjection] Fix symfony#16461 Let Container::set() replace existing aliases avoid (string) catchable fatal error for instances of __PHP_Incomplete_Class remove unnecessary retrieval and setting of data Update validators.fr.xlf avoid (string) catchable fatal error for __PHP_Incomplete_Class instances sendContent return as parent. [DomCrawler] Remove the overridden getHash() method to prevent problems when cloning the crawler [FrameworkBundle] Fix a typo Added more exceptions to singularify method Add width attribute on SVG [FrameworkBundle] Support autowiring for TranslationInterface [Validator] remove obsolete context and PropertyAccess code [WebProfiler] Fixed styles for search block and menu profiler for IE Edge
Taken from ezsystems/LegacyBridge@c5bf6c2 This fixes the broken content preview as described in https://jira.ez.no/browse/EZP-25551 Original comment: In Symfony 2.7.0 - 2.7.9, this line did nothing, since `ezpublish_legacy.kernel` is an alias and `Container::set` never did anything with aliases. In Symfony 2.7.10, setting an alias to a new value basically converts the alias definition to service definition, meaning that `ezpublish_legacy.kernel` will not point to `ezpublish_legacy.kernel.lazy` any more, thus making the alias invalid next time `ezpublish_legacy.kernel.lazy` is rebuilt. See symfony/symfony#16461 for details
In my integration tests I want to replace a service by a fake implementation. For example I want to temporarily replace the
event_dispatcher
by a mock that will store all triggered events:The problem is that
event_dispatcher
is an alias, andContainer::get()
first resolves aliases before trying to find the entry.That makes
Container::set()
silently useless when setting an entry that is an alias.IMO this is a bug, and I see 2 solutions (by order of preference), feel free to suggest alternatives:
set()
should erase any existing alias because it should just workset()
should throw an exception if setting an aliasI can implement the solution if one is chosen.
The text was updated successfully, but these errors were encountered: