Skip to content

Commit

Permalink
feature #22098 [*Bundle] Add autowiring aliases for common services (…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
fabpot committed Mar 21, 2017
2 parents b048296 + 08c2ee3 commit 7f7b897
Show file tree
Hide file tree
Showing 54 changed files with 54 additions and 63 deletions.
Expand Up @@ -23,6 +23,7 @@
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;
Expand Down Expand Up @@ -76,12 +77,9 @@ protected function getSerializer(): SerializerInterface
}

/**
* An instance of the Session implementation (and not the interface) is returned because getFlashBag is not part of
* the interface.
*
* @required
*/
protected function getSession(): Session
protected function getSession(): SessionInterface
{
}

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

/**
Expand All @@ -245,8 +247,6 @@ protected function addFlash(string $type, string $message)
* @param mixed $object The object
*
* @return bool
*
* @throws \LogicException
*/
protected function isGranted($attributes, $object = null): bool
{
Expand Down Expand Up @@ -396,8 +396,6 @@ protected function createFormBuilder($data = null, array $options = array()): Fo
*
* @return mixed
*
* @throws \LogicException If SecurityBundle is not available
*
* @see TokenInterface::getUser()
*/
protected function getUser()
Expand Down
Expand Up @@ -10,6 +10,7 @@
<argument type="service" id="assets.empty_package" /> <!-- default package -->
<argument type="collection" /> <!-- named packages -->
</service>
<service id="Symfony\Component\Asset\Packages" alias="assets.packages" public="false" />

<service id="assets.empty_package" class="Symfony\Component\Asset\Package" public="false">
<argument type="service" id="assets.empty_version_strategy" />
Expand Down
Expand Up @@ -26,5 +26,6 @@
<service id="debug.file_link_formatter" class="Symfony\Component\HttpKernel\Debug\FileLinkFormatter" public="false">
<argument>%debug.file_link_format%</argument>
</service>
<service id="Symfony\Component\HttpKernel\Debug\FileLinkFormatter" alias="debug.file_link_formatter" public="false" />
</services>
</container>
3 changes: 3 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml
Expand Up @@ -7,6 +7,7 @@
<services>
<!-- ResolvedFormTypeFactory -->
<service id="form.resolved_type_factory" class="Symfony\Component\Form\ResolvedFormTypeFactory" />
<service id="Symfony\Component\Form\ResolvedFormTypeFactoryInterface" alias="form.resolved_type_factory" public="false" />

<!-- FormRegistry -->
<service id="form.registry" class="Symfony\Component\Form\FormRegistry">
Expand All @@ -21,12 +22,14 @@
</argument>
<argument type="service" id="form.resolved_type_factory" />
</service>
<service id="Symfony\Component\Form\FormRegistryInterface" alias="form.registry" public="false" />

<!-- FormFactory -->
<service id="form.factory" class="Symfony\Component\Form\FormFactory">
<argument type="service" id="form.registry" />
<argument type="service" id="form.resolved_type_factory" />
</service>
<service id="Symfony\Component\Form\FormFactoryInterface" alias="form.factory" public="false" />

<!-- DependencyInjectionExtension -->
<service id="form.extension" class="Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension" public="false">
Expand Down
Expand Up @@ -7,6 +7,7 @@
<service id="translator" class="Symfony\Component\Translation\IdentityTranslator">
<argument type="service" id="translator.selector" />
</service>
<service id="Symfony\Component\Translation\TranslatorInterface" alias="translator" public="false" />

<service id="translator.selector" class="Symfony\Component\Translation\MessageSelector" public="false" />
</services>
Expand Down
Expand Up @@ -10,5 +10,6 @@
<argument /> <!-- throwExceptionOnInvalidIndex, set by the extension -->
<argument type="service" id="cache.property_access" on-invalid="ignore" />
</service>
<service id="Symfony\Component\PropertyAccess\PropertyAccessorInterface" alias="property_accessor" public="false" />
</services>
</container>
Expand Up @@ -11,6 +11,7 @@
<argument type="collection" />
<argument type="collection" />
</service>
<service id="Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface" alias="property_info" public="false" />

<!-- Extractor -->
<service id="property_info.reflection_extractor" class="Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor" public="false">
Expand Down
Expand Up @@ -80,6 +80,9 @@
</service>

<service id="router" alias="router.default" />
<service id="Symfony\Component\Routing\RouterInterface" alias="router" public="false" />
<service id="Symfony\Component\Routing\Generator\UrlGeneratorInterface" alias="router" public="false" />
<service id="Symfony\Component\Routing\Matcher\UrlMatcherInterface" alias="router" public="false" />

<service id="router.request_context" class="Symfony\Component\Routing\RequestContext" public="false">
<argument>%router.request_context.base_url%</argument>
Expand All @@ -89,6 +92,7 @@
<argument>%request_listener.http_port%</argument>
<argument>%request_listener.https_port%</argument>
</service>
<service id="Symfony\Component\Routing\RequestContext" alias="router.request_context" public="false" />

<service id="router.cache_warmer" class="Symfony\Bundle\FrameworkBundle\CacheWarmer\RouterCacheWarmer" public="false">
<tag name="kernel.cache_warmer" />
Expand Down
Expand Up @@ -6,14 +6,17 @@

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

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

<service id="security.csrf.token_manager" class="Symfony\Component\Security\Csrf\CsrfTokenManager">
<argument type="service" id="security.csrf.token_generator" />
<argument type="service" id="security.csrf.token_storage" />
</service>
<service id="Symfony\Component\Security\Csrf\CsrfTokenManagerInterface" alias="security.csrf.token_manager" public="false" />
</services>
</container>
Expand Up @@ -14,6 +14,11 @@
<argument type="collection" />
<argument type="collection" />
</service>
<service id="Symfony\Component\Serializer\SerializerInterface" alias="serializer" public="false" />
<service id="Symfony\Component\Serializer\NormalizerInterface" alias="serializer" public="false" />
<service id="Symfony\Component\Serializer\DenormalizerInterface" alias="serializer" public="false" />
<service id="Symfony\Component\Serializer\EncoderInterface" alias="serializer" public="false" />
<service id="Symfony\Component\Serializer\DecoderInterface" alias="serializer" public="false" />

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

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

<service id="serializer.denormalizer.array" class="Symfony\Component\Serializer\Normalizer\ArrayDenormalizer" public="false">
<!-- Run before the object normalizer -->
Expand Down
Expand Up @@ -9,16 +9,17 @@
<argument type="service" id="service_container" />
</service>
<service id="Symfony\Component\EventDispatcher\EventDispatcherInterface" alias="event_dispatcher" public="false" />
<service id="Symfony\Component\EventDispatcher\EventDispatcher" alias="event_dispatcher" public="false" />

<service id="http_kernel" class="Symfony\Component\HttpKernel\HttpKernel">
<argument type="service" id="event_dispatcher" />
<argument type="service" id="controller_resolver" />
<argument type="service" id="request_stack" />
<argument type="service" id="argument_resolver" />
</service>
<service id="Symfony\Component\HttpKernel\HttpKernelInterface" alias="http_kernel" public="false" />

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

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

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

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

<service id="file_locator" class="Symfony\Component\HttpKernel\Config\FileLocator">
<argument type="service" id="kernel" />
Expand All @@ -51,6 +54,7 @@
<argument>%kernel.root_dir%</argument>
</argument>
</service>
<service id="Symfony\Component\HttpKernel\Config\FileLocator" alias="file_locator" public="false" />

<service id="uri_signer" class="Symfony\Component\HttpKernel\UriSigner">
<argument>%kernel.secret%</argument>
Expand Down
Expand Up @@ -15,6 +15,10 @@
<argument type="service" id="session.flash_bag" />
</service>

<service id="Symfony\Component\HttpFoundation\Session\SessionInterface" alias="session" public="false" />
<service id="Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface" alias="session.storage" public="false" />
<service id="SessionHandlerInterface" alias="session.handler" public="false" />

<service id="session.storage.metadata_bag" class="Symfony\Component\HttpFoundation\Session\Storage\MetadataBag" public="false">
<argument>%session.metadata.storage_key%</argument>
<argument>%session.metadata.update_threshold%</argument>
Expand Down
Expand Up @@ -13,6 +13,7 @@
<service id="validator" class="Symfony\Component\Validator\Validator\ValidatorInterface">
<factory service="validator.builder" method="getValidator" />
</service>
<service id="Symfony\Component\Validator\Validator\ValidatorInterface" alias="validator" public="false" />

<service id="validator.builder" class="Symfony\Component\Validator\ValidatorBuilderInterface">
<factory class="Symfony\Component\Validator\Validation" method="createValidatorBuilder" />
Expand Down
Expand Up @@ -22,6 +22,7 @@
<service id="workflow.marking_store.single_state" class="Symfony\Component\Workflow\MarkingStore\SingleStateMarkingStore" abstract="true" />

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

<service id="workflow.twig_extension" class="Symfony\Bridge\Twig\Extension\WorkflowExtension">
<argument type="service" id="workflow.registry" />
Expand Down
Expand Up @@ -17,8 +17,10 @@
<argument type="service" id="security.access.decision_manager" />
<argument>%security.access.always_authenticate_before_granting%</argument>
</service>
<service id="Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface" alias="security.authorization_checker" />

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

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

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

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

<service id="security.password_encoder" alias="security.user_password_encoder.generic"></service>
<service id="security.password_encoder" alias="security.user_password_encoder.generic" />
<service id="Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface" alias="security.password_encoder" />

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

Expand Down Expand Up @@ -103,6 +107,7 @@
<argument type="service" id="security.firewall.map" />
<argument type="service" id="event_dispatcher" />
</service>
<service id="Symfony\Component\Security\Http\Firewall" alias="security.firewall" public="false" />

<service id="security.firewall.map" class="Symfony\Bundle\SecurityBundle\Security\FirewallMap" public="false">
<argument /> <!-- Firewall context locator -->
Expand Down
Expand Up @@ -35,6 +35,7 @@
</service>

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

<service id="security.acl.cache.doctrine" class="Symfony\Component\Security\Acl\Domain\DoctrineAclCache" public="false">
<argument type="service" id="security.acl.cache.doctrine.cache_impl" />
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Expand Up @@ -17,6 +17,7 @@
</call>
<configurator service="twig.configurator.environment" method="configure" />
</service>
<service id="Twig_Environment" alias="twig" public="false" />

<service id="twig.app_variable" class="Symfony\Bridge\Twig\AppVariable" public="false">
<call method="setEnvironment"><argument>%kernel.environment%</argument></call>
Expand Down
Expand Up @@ -126,7 +126,6 @@ public function __construct(ParameterBagInterface $parameterBag = null)
$this->setDefinition('service_container', (new Definition(ContainerInterface::class))->setSynthetic(true));
$this->setAlias(PsrContainerInterface::class, new Alias('service_container', false));
$this->setAlias(ContainerInterface::class, new Alias('service_container', false));
$this->setAlias(Container::class, new Alias('service_container', false));
}

/**
Expand Down
Expand Up @@ -26,7 +26,6 @@
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
Expand Down Expand Up @@ -58,7 +57,6 @@ public function testDefaultRegisteredDefinitions()
$this->assertSame(ContainerInterface::class, $definition->getClass());
$this->assertTrue($builder->hasAlias(PsrContainerInterface::class));
$this->assertTrue($builder->hasAlias(ContainerInterface::class));
$this->assertTrue($builder->hasAlias(Container::class));
}

public function testDefinitions()
Expand Down Expand Up @@ -229,7 +227,6 @@ public function testGetServiceIds()
'bar',
'Psr\Container\ContainerInterface',
'Symfony\Component\DependencyInjection\ContainerInterface',
'Symfony\Component\DependencyInjection\Container',
),
$builder->getServiceIds(),
'->getServiceIds() returns all defined service ids'
Expand Down Expand Up @@ -281,7 +278,7 @@ public function testGetAliases()

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

public function testSetAliases()
Expand Down
Expand Up @@ -82,7 +82,6 @@ public function testDumpAnonymousServices()
</service>
<service id="Psr\Container\ContainerInterface" alias="service_container" public="false"/>
<service id="Symfony\Component\DependencyInjection\ContainerInterface" alias="service_container" public="false"/>
<service id="Symfony\Component\DependencyInjection\Container" alias="service_container" public="false"/>
</services>
</container>
', $dumper->dump());
Expand All @@ -102,7 +101,6 @@ public function testDumpEntities()
</service>
<service id=\"Psr\Container\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
<service id=\"Symfony\Component\DependencyInjection\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
<service id=\"Symfony\Component\DependencyInjection\Container\" alias=\"service_container\" public=\"false\"/>
</services>
</container>
", $dumper->dump());
Expand All @@ -129,7 +127,6 @@ public function provideDecoratedServicesData()
<service id=\"foo\" class=\"FooClass\Foo\" decorates=\"bar\" decoration-inner-name=\"bar.woozy\"/>
<service id=\"Psr\Container\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
<service id=\"Symfony\Component\DependencyInjection\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
<service id=\"Symfony\Component\DependencyInjection\Container\" alias=\"service_container\" public=\"false\"/>
</services>
</container>
", include $fixturesPath.'/containers/container15.php'),
Expand All @@ -140,7 +137,6 @@ public function provideDecoratedServicesData()
<service id=\"foo\" class=\"FooClass\Foo\" decorates=\"bar\"/>
<service id=\"Psr\Container\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
<service id=\"Symfony\Component\DependencyInjection\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
<service id=\"Symfony\Component\DependencyInjection\Container\" alias=\"service_container\" public=\"false\"/>
</services>
</container>
", include $fixturesPath.'/containers/container16.php'),
Expand Down

0 comments on commit 7f7b897

Please sign in to comment.