8000 [DI] Fix private-by-default BC layer · symfony/symfony@ff2ab58 · GitHub
[go: up one dir, main page]

Skip to content

Commit ff2ab58

Browse files
[DI] Fix private-by-default BC layer
1 parent ad4bc6b commit ff2ab58

Some content is hidden

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

46 files changed

+91
-98
lines changed

src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/ContainerBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testCreateProxyServiceWithRuntimeInstantiator()
3131

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

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

3737
$builder->compile();

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,7 @@ public function testEventDispatcherService()
953953
$this->loadFromFile($container, 'default_config');
954954
$container
955955
->register('foo', \stdClass::class)
956+
->setPublic(true)
956957
->setProperty('dispatcher', new Reference('event_dispatcher'));
957958
$container->compile();
958959
$this->assertInstanceOf(EventDispatcherInterface::class, $container->get('foo')->dispatcher);

src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/WebProfilerExtensionTest.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ protected function setUp()
5252
$this->kernel = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\KernelInterface')->getMock();
5353

5454
$this->container = new ContainerBuilder();
55-
$this->container->register('event_dispatcher', EventDispatcher::class);
56-
$this->container->register('router', $this->getMockClass('Symfony\\Component\\Routing\\RouterInterface'));
57-
$this->container->register('twig', 'Twig\Environment');
58-
$this->container->register('twig_loader', 'Twig\Loader\ArrayLoader')->addArgument(array());
59-
$this->container->register('twig', 'Twig\Environment')->addArgument(new Reference('twig_loader'));
55+
$this->container->register('event_dispatcher', EventDispatcher::class)->setPublic(true);
56+
$this->container->register('router', $this->getMockClass('Symfony\\Component\\Routing\\RouterInterface'))->setPublic(true);
57+
$this->container->register('twig', 'Twig\Environment')->setPublic(true);
58+
$this->container->register('twig_loader', 'Twig\Loader\ArrayLoader')->addArgument(array())->setPublic(true);
59+
$this->container->register('twig', 'Twig\Environment')->addArgument(new Reference('twig_loader'))->setPublic(true);
6060
$this->container->setParameter('kernel.bundles', array());
6161
$this->container->setParameter('kernel.cache_dir', __DIR__);
6262
$this->container->setParameter('kernel.debug', false);
@@ -65,6 +65,7 @@ protected function setUp()
6565
$this->container->setParameter('debug.file_link_format', null);
6666
$this->container->setParameter('profiler.class', array('Symfony\\Component\\HttpKernel\\Profiler\\Profiler'));
6767
$this->container->register('profiler', $this->getMockClass('Symfony\\Component\\HttpKernel\\Profiler\\Profiler'))
68+
->setPublic(true)
6869
->addArgument(new Definition($this->getMockClass('Symfony\\Component\\HttpKernel\\Profiler\\ProfilerStorageInterface')));
6970
$this->container->setParameter('data_collector.templates', array());
7071
$this->container->set('kernel', $this->kernel);
@@ -123,6 +124,9 @@ public function getDebugModes()
123124

124125
private function getCompiledContainer()
125126
{
127+
if ($this->container->has('web_profiler.debug_toolbar')) {
128+
$this->container->getDefinition('web_profiler.debug_toolbar')->setPublic(true);
129+
}
126130
$this->container->compile();
127131
$this->container->set('kernel', $this->kernel);
128132

src/Symfony/Component/DependencyInjection/ChildDefinition.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class ChildDefinition extends Definition
3030
public function __construct($parent)
3131
{
3232
$this->parent = $parent;
33+
$this->setPrivate(false);
3334
}
3435

3536
/**

src/Symfony/Component/DependencyInjection/Compiler/RemoveUnusedDefinitionsPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public function process(ContainerBuilder $container)
6868

6969
if (1 === count($referencingAliases) && false === $isReferenced) {
7070
$container->setDefinition((string) reset($referencingAliases), $definition);
71-
$definition->setPrivate(reset($referencingAliases)->isPrivate());
7271
$definition->setPublic(!$definition->isPrivate());
72+
$definition->setPrivate(reset($referencingAliases)->isPrivate());
7373
$container->removeDefinition($id);
7474
$container->log($this, sprintf('Removed service "%s"; reason: replaces alias %s.', $id, reset($referencingAliases)));
7575
} elseif (0 === count($referencingAliases) && false === $isReferenced) {

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function __construct(ParameterBagInterface $parameterBag = null)
126126
parent::__construct($parameterBag);
127127

128128
$this->trackResources = interface_exists('Symfony\Component\Config\Resource\ResourceInterface');
129-
$this->setDefinition('service_container', (new Definition(ContainerInterface::class))->setSynthetic(true));
129+
$this->setDefinition('service_container', (new Definition(ContainerInterface::class))->setSynthetic(true)->setPublic(true));
130130
$this->setAlias(PsrContainerInterface::class, new Alias('service_container', false));
131131
$this->setAlias(ContainerInterface::class, new Alias('service_container', false));
132132
}
@@ -562,6 +562,22 @@ public function has($id)
562562
* @see Reference
563563
*/
564564
public function get($id, $invalidBehavior = Contain 10000 erInterface::EXCEPTION_ON_INVALID_REFERENCE)
565+
{
566+
if ($this->isCompiled()) {
567+
$id = $this->normalizeId($id);
568+
569+
if (isset($this->definitions[$id]) && $this->definitions[$id]->isPrivate()) {
570+
@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);
571+
}
572+
if (isset($this->aliasDefinitions[$id]) && $this->aliasDefinitions[$id]->isPrivate()) {
573+
@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);
574+
}
575+
}
576+
577+
return $this->doGet($id, $invalidBehavior);
578+
}
579+
580+
private function doGet($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
565581
{
566582
$id = $this->normalizeId($id);
567583

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

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

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

11241140
if ($callable[0] instanceof Reference) {
1125-
$callable[0] = $this->get((string) $callable[0], $callable[0]->getInvalidBehavior());
1141+
$callable[0] = $this->doGet((string) $callable[0], $callable[0]->getInvalidBehavior());
11261142
} elseif ($callable[0] instanceof Definition) {
11271143
$callable[0] = $this->createService($callable[0], null);
11281144
}
@@ -1166,7 +1182,7 @@ public function resolveServices($value)
11661182
}
11671183
}
11681184
foreach (self::getInitializedConditionals($v) as $s) {
1169-
if (!$this->get($s, ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE)) {
1185+
if (!$this->doGet($s, ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE)) {
11701186
continue 2;
11711187
}
11721188
}
@@ -1182,7 +1198,7 @@ public function resolveServices($value)
11821198
}
11831199
}
11841200
foreach (self::getInitializedConditionals($v) as $s) {
1185-
if (!$this->get($s, ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE)) {
1201+
if (!$this->doGet($s, ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE)) {
11861202
continue 2;
11871203
}
11881204
}
@@ -1193,7 +1209,7 @@ public function resolveServices($value)
11931209
return $count;
11941210
});
11951211
} elseif ($value instanceof Reference) {
1196-
$value = $this->get((string) $value, $value->getInvalidBehavior());
1212+
$value = $this->doGet((string) $value, $value->getInvalidBehavior());
11971213
} elseif ($value instanceof Definition) {
11981214
$value = $this->createService($value, null);
11991215
} elseif ($value instanceof Expression) {
@@ -1518,7 +1534,7 @@ private function callMethod($service, $call)
15181534
}
15191535
}
15201536
foreach (self::getInitializedConditionals($call[1]) as $s) {
1521-
if (!$this->get($s, ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE)) {
1537+
if (!$this->doGet($s, ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE)) {
15221538
return;
15231539
}
15241540
}

src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,7 @@ private function parseDefinition(\DOMElement $service, $file, array $defaults)
255255
$definition->setChanges(array());
256256
}
257257

258-
if ($publicAttr = $service->getAttribute('public')) {
259-
$definition->setPublic(XmlUtils::phpize($publicAttr));
260-
}
261-
262-
foreach (array('class', 'shared', 'synthetic', 'lazy', 'abstract') as $key) {
258+
foreach (array('class', 'public', 'shared', 'synthetic', 'lazy', 'abstract') as $key) {
263259
if ($value = $service->getAttribute($key)) {
264260
$method = 'set'.$key;
265261
$definition->$method(XmlUtils::phpize($value));

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ public function testCompileWithResolveEnv()
630630
$container->setParameter('env(HTTP_DUMMY_VAR)', '123');
631631
$container->register('teatime', 'stdClass')
632632
->setProperty('foo', '%env(DUMMY_ENV_VAR)%')
633+
->setPublic(true)
633634
;
634635
$container->compile(true);
635636

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

690-
$container->register('foo', 'stdClass')->setProperties(array(
691-
'fake' => '%env(int:FAKE)%',
692-
));
691+
$container->register('foo', 'stdClass')
692+
->setPublic(true)
693+
->setProperties(array(
694+
'fake' => '%env(int:FAKE)%',
695+
));
693696

694697
$container->compile(true);
695698

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

704-
$container->register('foo', 'stdClass')->setProperties(array(
707+
$container->register('foo', 'stdClass')
708+
->setPublic(true)
709+
->setProperties(array(
705710
'fake' => '%env(int:FAKE)%',
706711
));
707712

@@ -860,7 +865,7 @@ public function testCompilesClassDefinitionsOfLazyServices()
860865

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

863-
$container->register('foo', 'BarClass');
868+
$container->register('foo', 'BarClass')->setPublic(true);
864869
$container->getDefinition('foo')->setLazy(true);
865870

866871
$container->compile();
@@ -959,7 +964,7 @@ public function testPrivateServiceUser()
959964

960965
$container->addDefinitions(array(
961966
'bar' => $fooDefinition,
962-
'bar_user' => $fooUserDefinition,
967+
'bar_user' => $fooUserDefinition->setPublic(true),
963968
));
964969

965970
$container->compile();
@@ -973,7 +978,7 @@ public function testThrowsExceptionWhenSetServiceOnACompiledContainer()
973978
{
974979
$container = new ContainerBuilder();
975980
$container->setResourceTracking(false);
976-
$container->setDefinition('a', new Definition('stdClass'));
981+
$container->register('a', 'stdClass')->setPublic(true);
977982
$container->compile();
978983
$container->set('a', new \stdClass());
979984
}
@@ -990,7 +995,7 @@ public function testNoExceptionWhenSetSyntheticServiceOnACompiledContainer()
990995
{
991996
$container = new ContainerBuilder();
992997
$def = new Definition('stdClass');
993-
$def->setSynthetic(true);
998+
$def->setSynthetic(true)->setPublic(true);
994999
$container->setDefinition('a', $def);
9951000
$container->compile();
9961001
$container->set('a', $a = new \stdClass());
@@ -1031,10 +1036,10 @@ public function testAbstractAlias()
10311036
$container = new ContainerBuilder();
10321037

10331038
$abstract = new Definition('AbstractClass');
1034-
$abstract->setAbstract(true);
1039+
$abstrac 10000 t->setAbstract(true)->setPublic(true);
10351040

10361041
$container->setDefinition('abstract_service', $abstract);
1037-
$container->setAlias('abstract_alias', 'abstract_service');
1042+
$container->setAlias('abstract_alias', 'abstract_service')->setPublic(true);
10381043

10391044
$container->compile();
10401045

@@ -1048,6 +1053,7 @@ public function testLazyLoadedService()
10481053
$container->set('a', new \BazClass());
10491054
$definition = new Definition('BazClass');
10501055
$definition->setLazy(true);
1056+
$definition->setPublic(true);
10511057
$container->setDefinition('a', $definition);
10521058
});
10531059

@@ -1075,6 +1081,7 @@ public function testInitializePropertiesBeforeMethodCalls()
10751081
$container = new ContainerBuilder();
10761082
$container->register('foo', 'stdClass');
10771083
$container->register('bar', 'MethodCallClass')
1084+
->setPublic(true)
10781085
->setProperty('simple', 'bar')
10791086
->setProperty('complex', new Reference('foo'))
10801087
->addMethodCall('callMe');
@@ -1088,9 +1095,10 @@ public function testAutowiring()
10881095
{
10891096
$container = new ContainerBuilder();
10901097

1091-
$container->register(A::class);
1098+
$container->register(A::class)->setPublic(true);
10921099
$bDefinition = $container->register('b', __NAMESPACE__.'\B');
10931100
$bDefinition->setAutowired(true);
1101+
$bDefinition->setPublic(true);
10941102

10951103
$container->compile();
10961104

@@ -1149,12 +1157,13 @@ public function testServiceLocator()
11491157
{
11501158
$container = new ContainerBuilder();
11511159
$container->register('foo_service', ServiceLocator::class)
1160+
->setPublic(true)
11521161
->addArgument(array(
11531162
'bar' => new ServiceClosureArgument(new Reference('bar_service')),
11541163
'baz' => new ServiceClosureArgument(new TypedReference('baz_service', 'stdClass')),
11551164
))
11561165
;
1157-
$container->register('bar_service', 'stdClass')->setArguments(array(new Reference('baz_service')));
1166+
$container->register('bar_service', 'stdClass')->setArguments(array(new Reference('baz_service')))->setPublic(true);
11581167
$container->register('baz_service', 'stdClass')->setPublic(false);
11591168
$container->compile();
11601169

@@ -1234,6 +1243,7 @@ public function testParameterWithMixedCase()
12341243
{
12351244
$container = new ContainerBuilder(new ParameterBag(array('foo' => 'bar')));
12361245
$container->register('foo', 'stdClass')
1246+
->setPublic(true)
12371247
->setProperty('foo', '%FOO%');
12381248

12391249
$container->compile();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function testDumpAnonymousServices()
7474
$this->assertEquals('<?xml version="1.0" encoding="utf-8"?>
7575
<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">
7676
<services>
77-
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" synthetic="true"/>
77+
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" public="true" synthetic="true"/>
7878
<service id="foo" class="FooClass" public="true">
7979
<argument type="service">
8080
<service class="BarClass">
@@ -98,7 +98,7 @@ public function testDumpEntities()
9898
$this->assertEquals("<?xml version=\"1.0\" encoding=\"utf-8\"?>
9999
<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\">
100100
<services>
101-
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" synthetic=\"true\"/>
101+
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" public=\"true\" synthetic=\"true\"/>
102102
<service id=\"foo\" class=\"FooClass\Foo\" public=\"true\">
103103
<tag name=\"foo&quot;bar\bar\" foo=\"foo&quot;barřž€\"/>
104104
<argument>foo&lt;&gt;&amp;bar</argument>
@@ -127,7 +127,7 @@ public function provideDecoratedServicesData()
127127
array("<?xml version=\"1.0\" encoding=\"utf-8\"?>
128128
<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\">
129129
<services>
130-
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" synthetic=\"true\"/>
130+
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" public=\"true\" synthetic=\"true\"/>
131131
<service id=\"foo\" class=\"FooClass\Foo\" public=\"true\" decorates=\"bar\" decoration-inner-name=\"bar.woozy\"/>
132132
<service id=\"Psr\Container\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
133133
<service id=\"Symfony\Component\DependencyInjection\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
@@ -137,7 +137,7 @@ public function provideDecoratedServicesData()
137137
array("<?xml version=\"1.0\" encoding=\"utf-8\"?>
138138
<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\">
139139
<services>
140-
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" synthetic=\"true\"/>
140+
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" public=\"true\" synthetic=\"true\"/>
141141
<service id=\"foo\" class=\"FooClass\Foo\" public=\"true\" decorates=\"bar\"/>
142142
<service id=\"Psr\Container\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
143143
<service id=\"Symfony\Component\DependencyInjection\ContainerInterface\" alias=\"service_container\" public=\"false\"/>

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ class Container extends AbstractContainer
2929
public function __construct()
3030
{
3131
$this->services = array();
32-
$this->privates = array(
33-
'service_container' => true,
34-
);
3532

3633
$this->aliases = array();
3734
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ class ProjectServiceContainer extends Container
2727
public function __construct()
2828
{
2929
$this->services = array();
30-
$this->privates = array(
31-
'service_container' => true,
32-
);
3330

3431
$this->aliases = array();
3532
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ public function __construct()
3232
$this->methodMap = array(
3333
'test' => 'getTestService',
3434
);
35-
$this->privates = array(
36-
'service_container' => true,
37-
);
3835

3936
$this->aliases = array();
4037
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ public function __construct()
3636
$this->methodMap = array(
3737
'test' => 'getTestService',
3838
);
39-
$this->privates = array(
40-
'service_container' => true,
41-
);
4239

4340
$this->aliases = array();
4441
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services13.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ public function __construct()
3030
$this->methodMap = array(
3131
'bar' => 'getBarService',
3232
);
33-
$this->privates = array(
34-
'service_container' => true,
35-
);
3633

3734
$this->aliases = array();
3835
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services19.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ public function __construct()
3131
'service_from_anonymous_factory' => 'getServiceFromAnonymousFactoryService',
3232
'service_with_method_call_and_factory' => 'getServiceWithMethodCallAndFactoryService',
3333
);
34-
$this->privates = array(
35-
'service_container' => true,
36-
);
3734

3835
$this->aliases = array();
3936
}

0 commit comments

Comments
 (0)
0