Skip to content

[DI] Fix private-by-default BC layer #24266

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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testCreateProxyServiceWithRuntimeInstantiator()

$builder->setProxyInstantiator(new RuntimeInstantiator());

$builder->register('foo1', 'ProxyManagerBridgeFooClass')->setFile(__DIR__.'/Fixtures/includes/foo.php');
$builder->register('foo1', 'ProxyManagerBridgeFooClass')->setFile(__DIR__.'/Fixtures/includes/foo.php')->setPublic(true);
$builder->getDefinition('foo1')->setLazy(true);

$builder->compile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,7 @@ public function testEventDispatcherService()
$this->loadFromFile($container, 'default_config');
$container
->register('foo', \stdClass::class)
->setPublic(true)
->setProperty('dispatcher', new Reference('event_dispatcher'));
$container->compile();
$this->assertInstanceOf(EventDispatcherInterface::class, $container->get('foo')->dispatcher);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ protected function setUp()
$this->kernel = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\KernelInterface')->getMock();

$this->container = new ContainerBuilder();
$this->container->register('event_dispatcher', EventDispatcher::class);
$this->container->register('router', $this->getMockClass('Symfony\\Component\\Routing\\RouterInterface'));
$this->container->register('twig', 'Twig\Environment');
$this->container->register('twig_loader', 'Twig\Loader\ArrayLoader')->addArgument(array());
$this->container->register('twig', 'Twig\Environment')->addArgument(new Reference('twig_loader'));
$this->container->register('event_dispatcher', EventDispatcher::class)->setPublic(true);
$this->container->register('router', $this->getMockClass('Symfony\\Component\\Routing\\RouterInterface'))->setPublic(true);
$this->container->register('twig', 'Twig\Environment')->setPublic(true);
$this->container->register('twig_loader', 'Twig\Loader\ArrayLoader')->addArgument(array())->setPublic(true);
$this->container->register('twig', 'Twig\Environment')->addArgument(new Reference('twig_loader'))->setPublic(true);
$this->container->setParameter('kernel.bundles', array());
$this->container->setParameter('kernel.cache_dir', __DIR__);
$this->container->setParameter('kernel.debug', false);
Expand All @@ -65,6 +65,7 @@ protected function setUp()
$this->container->setParameter('debug.file_link_format', null);
$this->container->setParameter('profiler.class', array('Symfony\\Component\\HttpKernel\\Profiler\\Profiler'));
$this->container->register('profiler', $this->getMockClass('Symfony\\Component\\HttpKernel\\Profiler\\Profiler'))
->setPublic(true)
->addArgument(new Definition($this->getMockClass('Symfony\\Component\\HttpKernel\\Profiler\\ProfilerStorageInterface')));
$this->container->setParameter('data_collector.templates', array());
$this->container->set('kernel', $this->kernel);
Expand Down Expand Up @@ -123,6 +124,9 @@ public function getDebugModes()

private function getCompiledContainer()
{
if ($this->container->has('web_profiler.debug_toolbar')) {
$this->container->getDefinition('web_profiler.debug_toolbar')->setPublic(true);
}
$this->container->compile();
$this->container->set('kernel', $this->kernel);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ChildDefinition extends Definition
public function __construct($parent)
{
$this->parent = $parent;
$this->setPrivate(false);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public function process(ContainerBuilder $container)

if (1 === count($referencingAliases) && false === $isReferenced) {
$container->setDefinition((string) reset($referencingAliases), $definition);
$definition->setPrivate(reset($referencingAliases)->isPrivate());
$definition->setPublic(!$definition->isPrivate());
$definition->setPrivate(reset($referencingAliases)->isPrivate());
$container->removeDefinition($id);
$container->log($this, sprintf('Removed service "%s"; reason: replaces alias %s.', $id, reset($referencingAliases)));
} elseif (0 === count($referencingAliases) && false === $isReferenced) {
Expand Down
30 changes: 23 additions & 7 deletions src/Symfony/Component/DependencyInjection/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function __construct(ParameterBagInterface $parameterBag = null)
parent::__construct($parameterBag);

$this->trackResources = interface_exists('Symfony\Component\Config\Resource\ResourceInterface');
$this->setDefinition('service_container', (new Definition(ContainerInterface::class))->setSynthetic(true));
$this->setDefinition('service_container', (new Definition(ContainerInterface::class))->setSynthetic(true)->setPublic(true));
$this->setAlias(PsrContainerInterface::class, new Alias('service_container', false));
$this->setAlias(ContainerInterface::class, new Alias('service_container', false));
}
Expand Down Expand Up @@ -562,6 +562,22 @@ public function has($id)
* @see Reference
*/
public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
{
if ($this->isCompiled()) {
$id = $this->normalizeId($id);

if (isset($this->definitions[$id]) && $this->definitions[$id]->isPrivate()) {
@trigger_error(sprintf('Fetching the "%s" private service is deprecated and will fail in Symfony 4.0. Make the service public instead.', $id), E_USER_DEPRECATED);
}
if (isset($this->aliasDefinitions[$id]) && $this->aliasDefinitions[$id]->isPrivate()) {
@trigger_error(sprintf('Fetching the "%s" private alias is deprecated and will fail in Symfony 4.0. Make the alias public instead.', $id), E_USER_DEPRECATED);
Copy link
Member Author

Choose a reason for hiding this comment

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

these deprecation are required because in master, these services/aliases are removed by the RemoveUnusedServicesPass.

}
}

return $this->doGet($id, $invalidBehavior);
}

private function doGet($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
{
$id = $this->normalizeId($id);

Expand All @@ -573,7 +589,7 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
}

if (!isset($this->definitions[$id]) && isset($this->aliasDefinitions[$id])) {
return $this->get((string) $this->aliasDefinitions[$id], $invalidBehavior);
return $this->doGet((string) $this->aliasDefinitions[$id], $invalidBehavior);
}

try {
Expand Down Expand Up @@ -1122,7 +1138,7 @@ private function createService(Definition $definition, $id, $tryProxy = true)
$callable[0] = $parameterBag->resolveValue($callable[0]);

if ($callable[0] instanceof Reference) {
$callable[0] = $this->get((string) $callable[0], $callable[0]->getInvalidBehavior());
$callable[0] = $this->doGet((string) $callable[0], $callable[0]->getInvalidBehavior());
} elseif ($callable[0] instanceof Definition) {
$callable[0] = $this->createService($callable[0], null);
}
Expand Down Expand Up @@ -1166,7 +1182,7 @@ public function resolveServices($value)
}
}
foreach (self::getInitializedConditionals($v) as $s) {
if (!$this->get($s, ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE)) {
if (!$this->doGet($s, ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE)) {
continue 2;
}
}
Expand All @@ -1182,7 +1198,7 @@ public function resolveServices($value)
}
}
foreach (self::getInitializedConditionals($v) as $s) {
if (!$this->get($s, ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE)) {
if (!$this->doGet($s, ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE)) {
continue 2;
}
}
Expand All @@ -1193,7 +1209,7 @@ public function resolveServices($value)
return $count;
});
} elseif ($value instanceof Reference) {
$value = $this->get((string) $value, $value->getInvalidBehavior());
$value = $this->doGet((string) $value, $value->getInvalidBehavior());
} elseif ($value instanceof Definition) {
$value = $this->createService($value, null);
} elseif ($value instanceof Expression) {
Expand Down Expand Up @@ -1518,7 +1534,7 @@ private function callMethod($service, $call)
}
}
foreach (self::getInitializedConditionals($call[1]) as $s) {
if (!$this->get($s, ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE)) {
if (!$this->doGet($s, ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE)) {
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,7 @@ private function parseDefinition(\DOMElement $service, $file, array $defaults)
$definition->setChanges(array());
}

if ($publicAttr = $service->getAttribute('public')) {
$definition->setPublic(XmlUtils::phpize($publicAttr));
}

foreach (array('class', 'shared', 'synthetic', 'lazy', 'abstract') as $key) {
foreach (array('class', 'public', 'shared', 'synthetic', 'lazy', 'abstract') as $key) {
if ($value = $service->getAttribute($key)) {
$method = 'set'.$key;
$definition->$method(XmlUtils::phpize($value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ public function testCompileWithResolveEnv()
$container->setParameter('env(HTTP_DUMMY_VAR)', '123');
$container->register('teatime', 'stdClass')
->setProperty('foo', '%env(DUMMY_ENV_VAR)%')
->setPublic(true)
;
$container->compile(true);

Expand Down Expand Up @@ -687,9 +688,11 @@ public function testCastEnv()
$container = new ContainerBuilder();
$container->setParameter('env(FAKE)', '123');

$container->register('foo', 'stdClass')->setProperties(array(
'fake' => '%env(int:FAKE)%',
));
$container->register('foo', 'stdClass')
->setPublic(true)
->setProperties(array(
'fake' => '%env(int:FAKE)%',
));

$container->compile(true);

Expand All @@ -701,7 +704,9 @@ public function testEnvAreNullable()
$container = new ContainerBuilder();
$container->setParameter('env(FAKE)', null);

$container->register('foo', 'stdClass')->setProperties(array(
$container->register('foo', 'stdClass')
->setPublic(true)
->setProperties(array(
'fake' => '%env(int:FAKE)%',
));

Expand Down Expand Up @@ -860,7 +865,7 @@ public function testCompilesClassDefinitionsOfLazyServices()

$this->assertEmpty($container->getResources(), 'No resources get registered without resource tracking');

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

$container->compile();
Expand Down Expand Up @@ -959,7 +964,7 @@ public function testPrivateServiceUser()

$container->addDefinitions(array(
'bar' => $fooDefinition,
'bar_user' => $fooUserDefinition,
'bar_user' => $fooUserDefinition->setPublic(true),
));

$container->compile();
Expand All @@ -973,7 +978,7 @@ public function testThrowsExceptionWhenSetServiceOnACompiledContainer()
{
$container = new ContainerBuilder();
$container->setResourceTracking(false);
$container->setDefinition('a', new Definition('stdClass'));
$container->register('a', 'stdClass')->setPublic(true);
$container->compile();
$container->set('a', new \stdClass());
}
Expand All @@ -990,7 +995,7 @@ public function testNoExceptionWhenSetSyntheticServiceOnACompiledContainer()
{
$container = new ContainerBuilder();
$def = new Definition('stdClass');
$def->setSynthetic(true);
$def->setSynthetic(true)->setPublic(true);
$container->setDefinition('a', $def);
$container->compile();
$container->set('a', $a = new \stdClass());
Expand Down Expand Up @@ -1031,10 +1036,10 @@ public function testAbstractAlias()
$container = new ContainerBuilder();

$abstract = new Definition('AbstractClass');
$abstract->setAbstract(true);
$abstract->setAbstract(true)->setPublic(true);

$container->setDefinition('abstract_service', $abstract);
$container->setAlias('abstract_alias', 'abstract_service');
$container->setAlias('abstract_alias', 'abstract_service')->setPublic(true);

$container->compile();

Expand All @@ -1048,6 +1053,7 @@ public function testLazyLoadedService()
$container->set('a', new \BazClass());
$definition = new Definition('BazClass');
$definition->setLazy(true);
$definition->setPublic(true);
$container->setDefinition('a', $definition);
});

Expand Down Expand Up @@ -1075,6 +1081,7 @@ public function testInitializePropertiesBeforeMethodCalls()
$container = new ContainerBuilder();
$container->register('foo', 'stdClass');
$container->register('bar', 'MethodCallClass')
->setPublic(true)
->setProperty('simple', 'bar')
->setProperty('complex', new Reference('foo'))
->addMethodCall('callMe');
Expand All @@ -1088,9 +1095,10 @@ public function testAutowiring()
{
$container = new ContainerBuilder();

$container->register(A::class);
$container->register(A::class)->setPublic(true);
$bDefinition = $container->register('b', __NAMESPACE__.'\B');
$bDefinition->setAutowired(true);
$bDefinition->setPublic(true);

$container->compile();

Expand Down Expand Up @@ -1149,12 +1157,13 @@ public function testServiceLocator()
{
$container = new ContainerBuilder();
$container->register('foo_service', ServiceLocator::class)
->setPublic(true)
->addArgument(array(
'bar' => new ServiceClosureArgument(new Reference('bar_service')),
'baz' => new ServiceClosureArgument(new TypedReference('baz_service', 'stdClass')),
))
;
$container->register('bar_service', 'stdClass')->setArguments(array(new Reference('baz_service')));
$container->register('bar_service', 'stdClass')->setArguments(array(new Reference('baz_service')))->setPublic(true);
$container->register('baz_service', 'stdClass')->setPublic(false);
$container->compile();

Expand Down Expand Up @@ -1234,6 +1243,7 @@ public function testParameterWithMixedCase()
{
$container = new ContainerBuilder(new ParameterBag(array('foo' => 'bar')));
$container->register('foo', 'stdClass')
->setPublic(true)
->setProperty('foo', '%FOO%');

$container->compile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testDumpAnonymousServices()
$this->assertEquals('<?xml version="1.0" encoding="utf-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" synthetic="true"/>
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" public="true" synthetic="true"/>
<service id="foo" class="FooClass" public="true">
<argument type="service">
<service class="BarClass">
Expand All @@ -98,7 +98,7 @@ public function testDumpEntities()
$this->assertEquals("<?xml version=\"1.0\" encoding=\"utf-8\"?>
<container xmlns=\"http://symfony.com/schema/dic/services\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd\">
<services>
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" synthetic=\"true\"/>
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" public=\"true\" synthetic=\"true\"/>
<service id=\"foo\" class=\"FooClass\Foo\" public=\"true\">
<tag name=\"foo&quot;bar\bar\" foo=\"foo&quot;barřž€\"/>
<argument>foo&lt;&gt;&amp;bar</argument>
Expand Down Expand Up @@ -127,7 +127,7 @@ public function provideDecoratedServicesData()
array("<?xml version=\"1.0\" encoding=\"utf-8\"?>
<container xmlns=\"http://symfony.com/schema/dic/services\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd\">
<services>
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" synthetic=\"true\"/>
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" public=\"true\" synthetic=\"true\"/>
<service id=\"foo\" class=\"FooClass\Foo\" public=\"true\" 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\"/>
Expand All @@ -137,7 +137,7 @@ public function provideDecoratedServicesData()
array("<?xml version=\"1.0\" encoding=\"utf-8\"?>
<container xmlns=\"http://symfony.com/schema/dic/services\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd\">
<services>
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" synthetic=\"true\"/>
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" public=\"true\" synthetic=\"true\"/>
<service id=\"foo\" class=\"FooClass\Foo\" public=\"true\" decorates=\"bar\"/>
<service id=\"Psr\Container\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
<service id=\"Symfony\Component\DependencyInjection\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ class Container extends AbstractContainer
public function __construct()
{
$this->services = array();
$this->privates = array(
'service_container' => true,
);

$this->aliases = array();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ class ProjectServiceContainer extends Container
public function __construct()
{
$this->services = array();
$this->privates = array(
'service_container' => true,
);

$this->aliases = array();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ public function __construct()
$this->methodMap = array(
'test' => 'getTestService',
);
$this->privates = array(
'service_container' => true,
);

$this->aliases = array();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ public function __construct()
$this->methodMap = array(
'test' => 'getTestService',
);
$this->privates = array(
'service_container' => true,
);

$this->aliases = array();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ public function __construct()
$this->methodMap = array(
'bar' => 'getBarService',
);
$this->privates = array(
'service_container' => true,
);

$this->aliases = array();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ public function __construct()
'service_from_anonymous_factory' => 'getServiceFromAnonymousFactoryService',
'service_with_method_call_and_factory' => 'getServiceWithMethodCallAndFactoryService',
);
$this->privates = array(
'service_container' => true,
);

$this->aliases = array();
}
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy