Skip to content

[*Bundle] Add autowiring aliases for common services #22098

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

Merged
merged 1 commit into from
Mar 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change violates the Liskov substitution principle, getFlashBag() is not part of this interface.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See PR description for why we need to do this.
The addFlash() method now throws a LogicException when needed.

{
}

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it actually needed ? I never had a need to inject it directly. This service should probably only be used by the form registry and the form factory, not by userland code

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dunno, but it's public and has an interface, so does no harm at least


<!-- 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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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>
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
<argument type="collection" />
<argument type="collection" />
</service>
<service id="Symfony\Component\Serializer\SerializerInterface" alias="serializer" public="false" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also mark Symfony\Component\Serializer\NormalizerInterface, Symfony\Component\Serializer\DenormalizerInterface, Symfony\Component\Serializer\EncoderInterface and Symfony\Component\Serializer\DecoderInterface as aliases of the serializer.

<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" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it necessary ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @lyrixx @dunglas wdyt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that there is a good use case to inject this normalizer using autowiring. I would not recommend to do it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably a better idea to alias Symfony\Component\Serializer\Normalizer\NormalizerInterface and Symfony\Component\Serializer\Normalizer\DenormalizerInterface to the serializer service in case someone want to autowire a normalizer.


<service id="serializer.denormalizer.array" class="Symfony\Component\Serializer\Normalizer\ArrayDenormalizer" public="false">
<!-- Run before the object normalizer -->
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading