Skip to content

[DI] Turn services and aliases private by default, with BC layer #24238

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
Sep 19, 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
11 changes: 7 additions & 4 deletions UPGRADE-3.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ UPGRADE FROM 3.3 to 3.4
DependencyInjection
-------------------

* Definitions and aliases will be made private by default in 4.0. You should either use service injection
or explicitly define your services as public if you really need to inject the container.

* Relying on service auto-registration while autowiring is deprecated and won't be supported
in Symfony 4.0. Explicitly inject your dependencies or create services
whose ids are their fully-qualified class name.
Expand Down Expand Up @@ -154,7 +157,7 @@ FrameworkBundle
* The `Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader`
class has been deprecated and will be removed in 4.0. Use the
`Symfony\Component\Translation\Reader\TranslationReader` class instead.

* The `translation.loader` service has been deprecated and will be removed in 4.0.
Use the `translation.reader` service instead..

Expand Down Expand Up @@ -269,9 +272,9 @@ SecurityBundle
Translation
-----------

* `Symfony\Component\Translation\Writer\TranslationWriter::writeTranslations` has been deprecated
and will be removed in 4.0, use `Symfony\Component\Translation\Writer\TranslationWriter::write`
instead.
* `Symfony\Component\Translation\Writer\TranslationWriter::writeTranslations` has been deprecated
and will be removed in 4.0, use `Symfony\Component\Translation\Writer\TranslationWriter::write`
instead.

* Passing a `Symfony\Component\Translation\MessageSelector` to `Translator` has been
deprecated. You should pass a message formatter instead
Expand Down
15 changes: 9 additions & 6 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ Debug
DependencyInjection
-------------------

* Definitions and aliases are now private by default in 4.0. You should either use service injection
or explicitly define your services as public if you really need to inject the container.

* Relying on service auto-registration while autowiring is not supported anymore.
Explicitly inject your dependencies or create services whose ids are
their fully-qualified class name.
Expand Down Expand Up @@ -449,14 +452,14 @@ FrameworkBundle
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslatorPass`
class has been removed. Use the
`Symfony\Component\Translation\DependencyInjection\TranslatorPass` class instead.

* The `Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader`
class has been deprecated and will be removed in 4.0. Use the
`Symfony\Component\Translation\Reader\TranslationReader` class instead.

* The `translation.loader` service has been removed.
Use the `translation.reader` service instead.

* `AssetsInstallCommand::__construct()` now requires an instance of
`Symfony\Component\Filesystem\Filesystem` as first argument.

Expand Down Expand Up @@ -673,11 +676,11 @@ Translation
-----------

* Removed the backup feature from the file dumper classes.

* The default value of the `$readerServiceId` argument of `TranslatorPass::__construct()` has been changed to `"translation.reader"`.
* Removed `Symfony\Component\Translation\Writer\TranslationWriter::writeTranslations`,
use `Symfony\Component\Translation\Writer\TranslationWriter::write` instead.

* Removed `Symfony\Component\Translation\Writer\TranslationWriter::writeTranslations`,
use `Symfony\Component\Translation\Writer\TranslationWriter::write` instead.

* Removed support for passing `Symfony\Component\Translation\MessageSelector` as a second argument to the
`Translator::__construct()`. You should pass an instance of `Symfony\Component\Translation\Formatter\MessageFormatterInterface` instead.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private function dumpLazyServiceProjectServiceContainer()
{
$container = new ContainerBuilder();

$container->register('foo', 'stdClass');
$container->register('foo', 'stdClass')->setPublic(true);
$container->getDefinition('foo')->setLazy(true);
$container->compile();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1012,12 +1012,12 @@ private function registerTemplatingConfiguration(array $config, ContainerBuilder

// Use a delegation unless only a single engine was registered
if (1 === count($engines)) {
$container->setAlias('templating', (string) reset($engines));
$container->setAlias('templating', (string) reset($engines))->setPublic(true);
} else {
foreach ($engines as $engine) {
$container->getDefinition('templating.engine.delegating')->addMethodCall('addEngine', array($engine));
}
$container->setAlias('templating', 'templating.engine.delegating');
$container->setAlias('templating', 'templating.engine.delegating')->setPublic(true);
}

$container->getDefinition('fragment.renderer.hinclude')
Expand Down Expand Up @@ -1213,7 +1213,7 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
$container->getDefinition('translation.writer')->setPrivate(true);

// Use the "real" translator instead of the identity default
$container->setAlias('translator', 'translator.default');
$container->setAlias('translator', 'translator.default')->setPublic(true);
$container->setAlias('translator.formatter', new Alias($config['formatter'], false));
$translator = $container->findDefinition('translator.default');
$translator->addMethodCall('setFallbackLocales', array($config['fallbacks']));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function registerContainerConfiguration(LoaderInterface $loader)
if ($this instanceof EventSubscriberInterface) {
$container->register('kernel', static::class)
->setSynthetic(true)
->setPublic(true)
->addTag('kernel.event_subscriber')
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ imports:
- { resource: ../config/default.yml }

services:
_defaults: { public: true }
test.autowiring_types.autowired_services:
class: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\AutowiringTypes\AutowiredServices
autowire: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ imports:
- { resource: ../config/default.yml }

services:
_defaults: { public: true }
public:
class: Symfony\Bundle\FrameworkBundle\Tests\Fixtures\DeclaredClass
private_alias:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ imports:
- { resource: ../config/default.yml }

services:
_defaults: { public: true }
test.property_info: '@property_info'

framework:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
));

$c->setParameter('halloween', 'Have a great day!');
$c->register('halloween', 'stdClass');
$c->register('halloween', 'stdClass')->setPublic(true);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ private function configureDbalAclProvider(array $config, ContainerBuilder $conta
$container->getAlias('security.acl.provider')->setPrivate(true);

if (null !== $config['connection']) {
$container->setAlias('security.acl.dbal.connection', sprintf('doctrine.dbal.%s_connection', $config['connection']));
$container->setAlias('security.acl.dbal.connection', sprintf('doctrine.dbal.%s_connection', $config['connection']))->setPrivate(true);
}

$container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ imports:
- { resource: ./../config/framework.yml }

services:
_defaults: { public: true }
test.security.acl.provider: '@security.acl.provider'

doctrine:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ imports:
- { resource: ../config/framework.yml }

services:
_defaults: { public: true }
test.autowiring_types.autowired_services:
class: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AutowiringBundle\AutowiredServices
autowire: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public function process(ContainerBuilder $container)
if (isset($serviceIds[$commandId]) || $container->hasAlias($commandId)) {
$commandId = $commandId.'_'.$id;
}
if (!$definition->isPublic()) {
$container->setAlias($commandId, $id);
if (!$definition->isPublic() || $definition->isPrivate()) {
$container->setAlias($commandId, $id)->setPublic(true);
$id = $commandId;
}
$serviceIds[$commandId] = $id;
Expand Down Expand Up @@ -97,6 +97,7 @@ public function process(ContainerBuilder $container)

$container
->register($this->commandLoaderServiceId, ContainerCommandLoader::class)
->setPublic(true)
->setArguments(array(ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap));

$container->setParameter('console.command.ids', $serviceIds);
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Console/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"require-dev": {
"symfony/config": "~3.3|~4.0",
"symfony/event-dispatcher": "~2.8|~3.0|~4.0",
"symfony/dependency-injection": "~3.3|~4.0",
"symfony/dependency-injection": "~3.4|~4.0",
"symfony/lock": "~3.4|~4.0",
"symfony/process": "~3.3|~4.0",
"psr/log": "~1.0"
Expand All @@ -35,7 +35,7 @@
"psr/log": "For using the console logger"
},
"conflict": {
"symfony/dependency-injection": "<3.3",
"symfony/dependency-injection": "<3.4",
"symfony/process": "<3.3"
},
"autoload": {
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/DependencyInjection/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Alias
{
private $id;
private $public;
private $private = false;
private $private;

/**
* @param string $id Alias identifier
Expand All @@ -25,6 +25,7 @@ public function __construct($id, $public = true)
{
$this->id = (string) $id;
$this->public = $public;
$this->private = 2 > func_num_args();
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't this be $public && 2 > func_num_args() ?

If I register an alias as private explicitly, it does not need to have the BC layer keeping it from being inlined.

Copy link
Member Author

@nicolas-grekas nicolas-grekas Sep 18, 2017

Choose a reason for hiding this comment

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

when "private" is set to true, the BC layer is enabled and triggers deprecations,
so here, we want it to be "false" when either true or false is passes.

Copy link
Member

Choose a reason for hiding this comment

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

hmm sorry, I misunderstood the condition (reading too fast).

}

/**
Expand All @@ -47,6 +48,7 @@ public function isPublic()
public function setPublic($boolean)
{
$this->public = (bool) $boolean;
$this->private = false;

return $this;
}
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
3.4.0
-----

* deprecated "public-by-default" definitions and aliases, the new default will be "private" in 4.0
* added `EnvVarProcessorInterface` and corresponding "container.env_var_processor" tag for processing env vars
* added support for ignore-on-uninitialized references
* deprecated service auto-registration while autowiring
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function process(ContainerBuilder $container)

$aliasId = $container->getParameterBag()->resolveValue($tag['format']);
if ($container->hasDefinition($aliasId) || $container->hasAlias($aliasId)) {
$container->setAlias($serviceId, new Alias($aliasId));
$container->setAlias($serviceId, new Alias($aliasId, true));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function process(ContainerBuilder $container)
{
foreach ($container->getDefinitions() as $id => $definition) {
// synthetic service is public
if ($definition->isSynthetic() && (!$definition->isPublic() || $definition->isPrivate())) {
if ($definition->isSynthetic() && !($definition->isPublic() || $definition->isPrivate())) {
throw new RuntimeException(sprintf('A synthetic service ("%s") must be public.', $id));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function process(ContainerBuilder $container)
$container->setDefinition($renamedId, $decoratedDefinition);
}

$container->setAlias($inner, $id)->setPublic($public && !$private)->setPrivate($private);
$container->setAlias($inner, $id)->setPublic($public)->setPrivate($private);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function process(ContainerBuilder $container)
$bag->setProvidedTypes($types);
}
$container->register('container.env_var_processors_locator', ServiceLocator::class)
->setPublic(true)
->setArguments(array($processors))
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function process(ContainerBuilder $container)
}
// Check if target needs to be replaces
if (isset($replacements[$targetId])) {
$container->setAlias($definitionId, $replacements[$targetId]);
$container->setAlias($definitionId, $replacements[$targetId])->setPublic($target->isPublic())->setPrivate($target->isPrivate());
}
// No need to process the same target twice
if (isset($seenAliasTargets[$targetId])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ public function process(ContainerBuilder $container)
foreach ($container->getDefinitions() as $definition) {
if ($definition->isPrivate()) {
$definition->setPublic(false);
$definition->setPrivate(true);
}
}

foreach ($container->getAliases() as $alias) {
if ($alias->isPrivate()) {
$alias->setPublic(false);
$alias->setPrivate(true);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function process(ContainerBuilder $container)
foreach ($container->getAliases() as $id => $alias) {
$aliasId = (string) $alias;
if ($aliasId !== $defId = $this->getDefinitionId($aliasId, $container)) {
$container->setAlias($id, $defId)->setPublic($alias->isPublic() && !$alias->isPrivate())->setPrivate($alias->isPrivate());
$container->setAlias($id, $defId)->setPublic($alias->isPublic())->setPrivate($alias->isPrivate());
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/DependencyInjection/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Definition
private $configurator;
private $tags = array();
private $public = true;
private $private = false;
private $private = true;
private $synthetic = false;
private $abstract = false;
private $lazy = false;
Expand Down Expand Up @@ -603,6 +603,7 @@ public function setPublic($boolean)
$this->changes['public'] = true;

$this->public = (bool) $boolean;
$this->private = false;

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ private function addService($definition, $id, \DOMElement $parent)
if (!$definition->isShared()) {
$service->setAttribute('shared', 'false');
}
if (!$definition->isPublic()) {
$service->setAttribute('public', 'false');
if (!$definition->isPrivate()) {
$service->setAttribute('public', $definition->isPublic() ? 'true' : 'false');
}
if ($definition->isSynthetic()) {
$service->setAttribute('synthetic', 'true');
Expand Down Expand Up @@ -242,8 +242,8 @@ private function addServiceAlias($alias, Alias $id, \DOMElement $parent)
$service = $this->document->createElement('service');
$service->setAttribute('id', $alias);
$service->setAttribute('alias', $id);
if (!$id->isPublic()) {
$service->setAttribute('public', 'false');
if (!$id->isPrivate()) {
$service->setAttribute('public', $id->isPublic() ? 'true' : 'false');
}
$parent->appendChild($service);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ private function addService($id, $definition)
$code .= sprintf(" class: %s\n", $this->dumper->dump($class));
}

if (!$definition->isPublic()) {
$code .= " public: false\n";
if (!$definition->isPrivate()) {
$code .= sprintf(" public: %s\n", $definition->isPublic() ? 'true' : 'false');
}

$tagsCode = '';
Expand Down Expand Up @@ -178,11 +178,11 @@ private function addService($id, $definition)
*/
private function addServiceAlias($alias, $id)
{
if ($id->isPublic()) {
if ($id->isPrivate()) {
return sprintf(" %s: '@%s'\n", $alias, $id);
}

return sprintf(" %s:\n alias: %s\n public: false\n", $alias, $id);
return sprintf(" %s:\n alias: %s\n public: %s\n", $alias, $id, $id->isPublic() ? 'true' : 'false');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,12 @@ private function parseDefinition(\DOMElement $service, $file, array $defaults)
if ($alias = $service->getAttribute('alias')) {
$this->validateAlias($service, $file);

$public = true;
$this->container->setAlias((string) $service->getAttribute('id'), $alias = new Alias($alias));
if ($publicAttr = $service->getAttribute('public')) {
$public = XmlUtils::phpize($publicAttr);
$alias->setPublic(XmlUtils::phpize($publicAttr));
} elseif (isset($defaults['public'])) {
$public = $defaults['public'];
$alias->setPublic($defaults['public']);
}
$this->container->setAlias((string) $service->getAttribute('id'), new Alias($alias, $public));

return;
}
Expand Down
Loading