Skip to content

Commit 5e9c7bb

Browse files
committed
feature #22098 [*Bundle] Add autowiring aliases for common services (nicolas-grekas)
This PR was merged into the 3.3-dev branch. Discussion ---------- [*Bundle] Add autowiring aliases for common services | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - As spotted while working on #22060, we're missing many aliases to prevent any autowiring ambiguities. I also removed the "Symfony\Component\EventDispatcher\EventDispatcher" and "Symfony\Component\DependencyInjection\Container" aliases: we'd better encourage using the corresponding interfaces instead. On ControllerTrait, we need to type hint against SessionInterface, because otherwise, when session support is disabled, autowiring still auto-registers an "autowired.Session" service, which defeats the purpose of being able to enable/disable it. Commits ------- 08c2ee32f1 [*Bundle] Add autowiring aliases for common services
2 parents 4475d7b + 01229d5 commit 5e9c7bb

14 files changed

+39
-10
lines changed

Controller/ControllerTrait.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\HttpFoundation\RedirectResponse;
2424
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
2525
use Symfony\Component\HttpFoundation\Session\Session;
26+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
2627
use Symfony\Component\HttpFoundation\StreamedResponse;
2728
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2829
use Symfony\Component\HttpKernel\HttpKernelInterface;
@@ -76,12 +77,9 @@ protected function getSerializer(): SerializerInterface
7677
}
7778

7879
/**
79-
* An instance of the Session implementation (and not the interface) is returned because getFlashBag is not part of
80-
* the interface.
81-
*
8280
* @required
8381
*/
84-
protected function getSession(): Session
82+
protected function getSession(): SessionInterface
8583
{
8684
}
8785

@@ -235,7 +233,11 @@ protected function file($file, string $fileName = null, string $disposition = Re
235233
*/
236234
protected function addFlash(string $type, string $message)
237235
{
238-
$this->getSession()->getFlashBag()->add($type, $message);
236+
$session = $this->getSession();
237+
if (!$session instanceof Session) {
238+
throw new \LogicException(sprintf('You can not use the addFlash method: "%s" is not an instance of "%s".', get_class($session), Session::class));
239+
}
240+
$session->getFlashBag()->add($type, $message);
239241
}
240242

241243
/**
@@ -245,8 +247,6 @@ protected function addFlash(string $type, string $message)
245247
* @param mixed $object The object
246248
*
247249
* @return bool
248-
*
249-
* @throws \LogicException
250250
*/
251251
protected function isGranted($attributes, $object = null): bool
252252
{
@@ -396,8 +396,6 @@ protected function createFormBuilder($data = null, array $options = array()): Fo
396396
*
397397
* @return mixed
398398
*
399-
* @throws \LogicException If SecurityBundle is not available
400-
*
401399
* @see TokenInterface::getUser()
402400
*/
403401
protected function getUser()

Resources/config/assets.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<argument type="service" id="assets.empty_package" /> <!-- default package -->
1111
<argument type="collection" /> <!-- named packages -->
1212
</service>
13+
<service id="Symfony\Component\Asset\Packages" alias="assets.packages" public="false" />
1314

1415
<service id="assets.empty_package" class="Symfony\Component\Asset\Package" public="false">
1516
<argument type="service" id="assets.empty_version_strategy" />

Resources/config/debug_prod.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@
2626
<service id="debug.file_link_formatter" class="Symfony\Component\HttpKernel\Debug\FileLinkFormatter" public="false">
2727
<argument>%debug.file_link_format%</argument>
2828
</service>
29+
<service id="Symfony\Component\HttpKernel\Debug\FileLinkFormatter" alias="debug.file_link_formatter" public="false" />
2930
</services>
3031
</container>

Resources/config/form.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<services>
88
<!-- ResolvedFormTypeFactory -->
99
<service id="form.resolved_type_factory" class="Symfony\Component\Form\ResolvedFormTypeFactory" />
10+
<service id="Symfony\Component\Form\ResolvedFormTypeFactoryInterface" alias="form.resolved_type_factory" public="false" />
1011

1112
<!-- FormRegistry -->
1213
<service id="form.registry" class="Symfony\Component\Form\FormRegistry">
@@ -21,12 +22,14 @@
2122
</argument>
2223
<argument type="service" id="form.resolved_type_factory" />
2324
</service>
25+
<service id="Symfony\Component\Form\FormRegistryInterface" alias="form.registry" public="false" />
2426

2527
<!-- FormFactory -->
2628
<service id="form.factory" class="Symfony\Component\Form\FormFactory">
2729
<argument type="service" id="form.registry" />
2830
<argument type="service" id="form.resolved_type_factory" />
2931
</service>
32+
<service id="Symfony\Component\Form\FormFactoryInterface" alias="form.factory" public="false" />
3033

3134
<!-- DependencyInjectionExtension -->
3235
<service id="form.extension" class="Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension" public="false">

Resources/config/identity_translator.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<service id="translator" class="Symfony\Component\Translation\IdentityTranslator">
88
<argument type="service" id="translator.selector" />
99
</service>
10+
<service id="Symfony\Component\Translation\TranslatorInterface" alias="translator" public="false" />
1011

1112
<service id="translator.selector" class="Symfony\Component\Translation\MessageSelector" public="false" />
1213
</services>

Resources/config/property_access.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
<argument /> <!-- throwExceptionOnInvalidIndex, set by the extension -->
1111
<argument type="service" id="cache.property_access" on-invalid="ignore" />
1212
</service>
13+
<service id="Symfony\Component\PropertyAccess\PropertyAccessorInterface" alias="property_accessor" public="false" />
1314
</services>
1415
</container>

Resources/config/property_info.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<argument type="collection" />
1212
<argument type="collection" />
1313
</service>
14+
<service id="Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface" alias="property_info" public="false" />
1415

1516
<!-- Extractor -->
1617
<service id="property_info.reflection_extractor" class="Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor" public="false">

Resources/config/routing.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@
8080
</service>
8181

8282
<service id="router" alias="router.default" />
83+
<service id="Symfony\Component\Routing\RouterInterface" alias="router" public="false" />
84+
<service id="Symfony\Component\Routing\Generator\UrlGeneratorInterface" alias="router" public="false" />
85+
<service id="Symfony\Component\Routing\Matcher\UrlMatcherInterface" alias="router" public="false" />
8386

8487
<service id="router.request_context" class="Symfony\Component\Routing\RequestContext" public="false">
8588
<argument>%router.request_context.base_url%</argument>
@@ -89,6 +92,7 @@
8992
<argument>%request_listener.http_port%</argument>
9093
<argument>%request_listener.https_port%</argument>
9194
</service>
95+
<service id="Symfony\Component\Routing\RequestContext" alias="router.request_context" public="false" />
9296

9397
<service id="router.cache_warmer" class="Symfony\Bundle\FrameworkBundle\CacheWarmer\RouterCacheWarmer" public="false">
9498
<tag name="kernel.cache_warmer" />

Resources/config/security_csrf.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66

77
<services>
88
<service id="security.csrf.token_generator" class="Symfony\Component\Security\Csrf\TokenGenerator\UriSafeTokenGenerator" public="false" />
9+
<service id="Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface" alias="security.csrf.token_generator" public="false" />
910

1011
<service id="security.csrf.token_storage" class="Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage" public="false">
1112
<argument type="service" id="session" />
1213
</service>
14+
<service id="Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface" alias="security.csrf.token_storage" public="false" />
1315

1416
<service id="security.csrf.token_manager" class="Symfony\Component\Security\Csrf\CsrfTokenManager">
1517
<argument type="service" id="security.csrf.token_generator" />
1618
<argument type="service" id="security.csrf.token_storage" />
1719
</service>
20+
<service id="Symfony\Component\Security\Csrf\CsrfTokenManagerInterface" alias="security.csrf.token_manager" public="false" />
1821
</services>
1922
</container>

Resources/config/serializer.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
<argument type="collection" />
1515
<argument type="collection" />
1616
</service>
17+
<service id="Symfony\Component\Serializer\SerializerInterface" alias="serializer" public="false" />
18+
<service id="Symfony\Component\Serializer\NormalizerInterface" alias="serializer" public="false" />
19+
<service id="Symfony\Component\Serializer\DenormalizerInterface" alias="serializer" public="false" />
20+
<service id="Symfony\Component\Serializer\EncoderInterface" alias="serializer" public="false" />
21+
<service id="Symfony\Component\Serializer\DecoderInterface" alias="serializer" public="false" />
1722

1823
<service id="serializer.property_accessor" alias="property_accessor" public="false" />
1924

@@ -27,6 +32,7 @@
2732
<!-- Run after all custom normalizers -->
2833
<tag name="serializer.normalizer" priority="-1000" />
2934
</service>
35+
<service id="Symfony\Component\Serializer\Normalizer\ObjectNormalizer" alias="serializer.normalizer.object" public="false" />
3036

3137
<service id="serializer.denormalizer.array" class="Symfony\Component\Serializer\Normalizer\ArrayDenormalizer" public="false">
3238
<!-- Run before the object normalizer -->

Resources/config/services.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
<argument type="service" id="service_container" />
1010
</service>
1111
<service id="Symfony\Component\EventDispatcher\EventDispatcherInterface" alias="event_dispatcher" public="false" />
12-
<service id="Symfony\Component\EventDispatcher\EventDispatcher" alias="event_dispatcher" public="false" />
1312

1413
<service id="http_kernel" class="Symfony\Component\HttpKernel\HttpKernel">
1514
<argument type="service" id="event_dispatcher" />
1615
<argument type="service" id="controller_resolver" />
1716
<argument type="service" id="request_stack" />
1817
<argument type="service" id="argument_resolver" />
1918
</service>
19+
<service id="Symfony\Component\HttpKernel\HttpKernelInterface" alias="http_kernel" public="false" />
2020

2121
<service id="request_stack" class="Symfony\Component\HttpFoundation\RequestStack" />
22+
<service id="Symfony\Component\HttpFoundation\RequestStack" alias="request_stack" public="false" />
2223

2324
<service id="cache_warmer" class="Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate">
2425
<argument type="collection" />
@@ -41,8 +42,10 @@
4142
</service>
4243

4344
<service id="kernel" synthetic="true" />
45+
<service id="Symfony\Component\HttpKernel\KernelInterface" alias="kernel" public="false" />
4446

4547
<service id="filesystem" class="Symfony\Component\Filesystem\Filesystem" />
48+
<service id="Symfony\Component\Filesystem\Filesystem" alias="filesystem" public="false" />
4649

4750
<service id="file_locator" class="Symfony\Component\HttpKernel\Config\FileLocator">
4851
<argument type="service" id="kernel" />
@@ -51,6 +54,7 @@
5154
<argument>%kernel.root_dir%</argument>
5255
</argument>
5356
</service>
57+
<service id="Symfony\Component\HttpKernel\Config\FileLocator" alias="file_locator" public="false" />
5458

5559
<service id="uri_signer" class="Symfony\Component\HttpKernel\UriSigner">
5660
<argument>%kernel.secret%</argument>

Resources/config/session.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
<argument type="service" id="session.flash_bag" />
1616
</service>
1717

18+
<service id="Symfony\Component\HttpFoundation\Session\SessionInterface" alias="session" public="false" />
19+
<service id="Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface" alias="session.storage" public="false" />
20+
<service id="SessionHandlerInterface" alias="session.handler" public="false" />
21+
1822
<service id="session.storage.metadata_bag" class="Symfony\Component\HttpFoundation\Session\Storage\MetadataBag" public="false">
1923
<argument>%session.metadata.storage_key%</argument>
2024
<argument>%session.metadata.update_threshold%</argument>

Resources/config/validator.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<service id="validator" class="Symfony\Component\Validator\Validator\ValidatorInterface">
1414
<factory service="validator.builder" method="getValidator" />
1515
</service>
16+
<service id="Symfony\Component\Validator\Validator\ValidatorInterface" alias="validator" public="false" />
1617

1718
<service id="validator.builder" class="Symfony\Component\Validator\ValidatorBuilderInterface">
1819
<factory class="Symfony\Component\Validator\Validation" method="createValidatorBuilder" />

Resources/config/workflow.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<service id="workflow.marking_store.single_state" class="Symfony\Component\Workflow\MarkingStore\SingleStateMarkingStore" abstract="true" />
2323

2424
<service id="workflow.registry" class="Symfony\Component\Workflow\Registry" />
25+
<service id="Symfony\Component\Workflow\Registry" alias="workflow.registry" public="false" />
2526

2627
<service id="workflow.twig_extension" class="Symfony\Bridge\Twig\Extension\WorkflowExtension">
2728
<argument type="service" id="workflow.registry" />

0 commit comments

Comments
 (0)