Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7f7b897

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 ------- 08c2ee3 [*Bundle] Add autowiring aliases for common services
2 parents b048296 + 08c2ee3 commit 7f7b897

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+54
-63
lines changed

src/Symfony/Bundle/FrameworkBundle/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()

src/Symfony/Bundle/FrameworkBundle/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" />

src/Symfony/Bundle/FrameworkBundle/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>

src/Symfony/Bundle/FrameworkBundle/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">

src/Symfony/Bundle/FrameworkBundle/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>

src/Symfony/Bundle/FrameworkBundle/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>

src/Symfony/Bundle/FrameworkBundle/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">

src/Symfony/Bundle/FrameworkBundle/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" />

src/Symfony/Bundle/FrameworkBundle/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>

src/Symfony/Bundle/FrameworkBundle/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 -->

src/Symfony/Bundle/FrameworkBundle/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>

src/Symfony/Bundle/FrameworkBundle/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>

src/Symfony/Bundle/FrameworkBundle/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" />

src/Symfony/Bundle/FrameworkBundle/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" />

src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
<argument type="service" id="security.access.decision_manager" />
1818
<argument>%security.access.always_authenticate_before_granting%</argument>
1919
</service>
20+
<service id="Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface" alias="security.authorization_checker" />
2021

2122
<service id="security.token_storage" class="Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage" />
23+
<service id="Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface" alias="security.token_storage" public="false" />
2224

2325
<service id="security.user_value_resolver" class="Symfony\Bundle\SecurityBundle\SecurityUserValueResolver" public="false">
2426
<argument type="service" id="security.token_storage" />
@@ -48,12 +50,14 @@
4850
</service>
4951

5052
<service id="security.encoder_factory" alias="security.encoder_factory.generic" />
53+
<service id="Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface" alias="security.encoder_factory" public="false" />
5154

5255
<service id="security.user_password_encoder.generic" class="Symfony\Component\Security\Core\Encoder\UserPasswordEncoder" public="false">
5356
<argument type="service" id="security.encoder_factory"></argument>
5457
</service>
5558

56-
<service id="security.password_encoder" alias="security.user_password_encoder.generic"></service>
59+
<service id="security.password_encoder" alias="security.user_password_encoder.generic" />
60+
<service id="Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface" alias="security.password_encoder" />
5761

5862
<service id="security.user_checker" class="Symfony\Component\Security\Core\User\UserChecker" public="false" />
5963

@@ -103,6 +107,7 @@
103107
<argument type="service" id="security.firewall.map" />
104108
<argument type="service" id="event_dispatcher" />
105109
</service>
110+
<service id="Symfony\Component\Security\Http\Firewall" alias="security.firewall" public="false" />
106111

107112
<service id="security.firewall.map" class="Symfony\Bundle\SecurityBundle\Security\FirewallMap" public="false">
108113
<argument /> <!-- Firewall context locator -->

src/Symfony/Bundle/SecurityBundle/Resources/config/security_acl_dbal.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
</service>
3636

3737
<service id="security.acl.provider" alias="security.acl.dbal.provider" />
38+
<service id="Symfony\Component\Security\Acl\Model\AclProviderInterface" alias="security.acl.provider" public="false" />
3839

3940
<service id="security.acl.cache.doctrine" class="Symfony\Component\Security\Acl\Domain\DoctrineAclCache" public="false">
4041
<argument type="service" id="security.acl.cache.doctrine.cache_impl" />

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
</call>
1818
<configurator service="twig.configurator.environment" method="configure" />
1919
</service>
20+
<service id="Twig_Environment" alias="twig" public="false" />
2021

2122
<service id="twig.app_variable" class="Symfony\Bridge\Twig\AppVariable" public="false">
2223
<call method="setEnvironment"><argument>%kernel.environment%</argument></call>

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ public function __construct(ParameterBagInterface $parameterBag = null)
126126
$this->setDefinition('service_container', (new Definition(ContainerInterface::class))->setSynthetic(true));
127127
$this->setAlias(PsrContainerInterface::class, new Alias('service_container', false));
128128
$this->setAlias(ContainerInterface::class, new Alias('service_container', false));
129-
$this->setAlias(Container::class, new Alias('service_container', false));
130129
}
131130

132131
/**

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
2727
use Symfony\Component\DependencyInjection\ChildDefinition;
2828
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
29-
use Symfony\Component\DependencyInjection\Container;
3029
use Symfony\Component\DependencyInjection\ContainerBuilder;
3130
use Symfony\Component\DependencyInjection\ContainerInterface;
3231
use Symfony\Component\DependencyInjection\Definition;
@@ -58,7 +57,6 @@ public function testDefaultRegisteredDefinitions()
5857
$this->assertSame(ContainerInterface::class, $definition->getClass());
5958
$this->assertTrue($builder->hasAlias(PsrContainerInterface::class));
6059
$this->assertTrue($builder->hasAlias(ContainerInterface::class));
61-
$this->assertTrue($builder->hasAlias(Container::class));
6260
}
6361

6462
public function testDefinitions()
@@ -229,7 +227,6 @@ public function testGetServiceIds()
229227
'bar',
230228
'Psr\Container\ContainerInterface',
231229
'Symfony\Component\DependencyInjection\ContainerInterface',
232-
'Symfony\Component\DependencyInjection\Container',
233230
),
234231
$builder->getServiceIds(),
235232
'->getServiceIds() returns all defined service ids'
@@ -281,7 +278,7 @@ public function testGetAliases()
281278

282279
$builder->set('foobar', 'stdClass');
283280
$builder->set('moo', 'stdClass');
284-
$this->assertCount(3, $builder->getAliases(), '->getAliases() does not return aliased services that have been overridden');
281+
$this->assertCount(2, $builder->getAliases(), '->getAliases() does not return aliased services that have been overridden');
285282
}
286283

287284
public function testSetAliases()

src/Symfony/Component/DependencyInjection/Tests/Dumper/XmlDumperTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ public function testDumpAnonymousServices()
8282
</service>
8383
<service id="Psr\Container\ContainerInterface" alias="service_container" public="false"/>
8484
<service id="Symfony\Component\DependencyInjection\ContainerInterface" alias="service_container" public="false"/>
85-
<service id="Symfony\Component\DependencyInjection\Container" alias="service_container" public="false"/>
8685
</services>
8786
</container>
8887
', $dumper->dump());
@@ -102,7 +101,6 @@ public function testDumpEntities()
102101
</service>
103102
<service id=\"Psr\Container\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
104103
<service id=\"Symfony\Component\DependencyInjection\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
105-
<service id=\"Symfony\Component\DependencyInjection\Container\" alias=\"service_container\" public=\"false\"/>
106104
</services>
107105
</container>
108106
", $dumper->dump());
@@ -129,7 +127,6 @@ public function provideDecoratedServicesData()
129127
<service id=\"foo\" class=\"FooClass\Foo\" decorates=\"bar\" decoration-inner-name=\"bar.woozy\"/>
130128
<service id=\"Psr\Container\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
131129
<service id=\"Symfony\Component\DependencyInjection\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
132-
<service id=\"Symfony\Component\DependencyInjection\Container\" alias=\"service_container\" public=\"false\"/>
133130
</services>
134131
</container>
135132
", include $fixturesPath.'/containers/container15.php'),
@@ -140,7 +137,6 @@ public function provideDecoratedServicesData()
140137
<service id=\"foo\" class=\"FooClass\Foo\" decorates=\"bar\"/>
141138
<service id=\"Psr\Container\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
142139
<service id=\"Symfony\Component\DependencyInjection\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
143-
<service id=\"Symfony\Component\DependencyInjection\Container\" alias=\"service_container\" public=\"false\"/>
144140
</services>
145141
</container>
146142
", include $fixturesPath.'/containers/container16.php'),

0 commit comments

Comments
 (0)