Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e5d0934

Browse files
[DI] Turn private defs to non-public ones before removing passes
1 parent b43bdf3 commit e5d0934

File tree

5 files changed

+85
-43
lines changed

5 files changed

+85
-43
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ public function __construct()
7171
new CheckArgumentsValidityPass(false),
7272
));
7373

74+
$this->beforeRemovingPasses = array(
75+
-100 => array(
76+
new ResolvePrivatesPass(),
77+
),
78+
);
79+
7480
$this->removingPasses = array(array(
7581
new RemovePrivateAliasesPass(),
7682
new ReplaceAliasByActualDefinitionPass(),

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

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\DependencyInjection\Compiler;
1313

1414
use Symfony\Component\DependencyInjection\ChildDefinition;
15-
use Symfony\Component\DependencyInjection\ContainerBuilder;
1615
use Symfony\Component\DependencyInjection\Definition;
1716
use Symfony\Component\DependencyInjection\Exception\ExceptionInterface;
1817
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
@@ -26,28 +25,6 @@
2625
*/
2726
class ResolveChildDefinitionsPass extends AbstractRecursivePass
2827
{
29-
/**
30-
* {@inheritdoc}
31-
*/
32-
public function process(ContainerBuilder $container)
33-
{
34-
parent::process($container);
35-
36-
foreach ($container->getDefinitions() as $definition) {
37-
if ($definition->isPrivate()) {
38-
$definition->setPublic(false);
39-
$definition->setPrivate(true);
40-
}
41-
}
42-
43-
foreach ($container->getAliases() as $alias) {
44-
if ($alias->isPrivate()) {
45-
$alias->setPublic(false);
46-
$alias->setPrivate(true);
47-
}
48-
}
49-
}
50-
5128
protected function processValue($value, $isRoot = false)
5229
{
5330
if (!$value instanceof Definition) {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\ContainerBuilder;
15+
16+
/**
17+
* @author Nicolas Grekas <[email protected]>
18+
*/
19+
class ResolvePrivatesPass implements CompilerPassInterface
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function process(ContainerBuilder $container)
25+
{
26+
foreach ($container->getDefinitions() as $id => $definition) {
27+
if ($definition->isPrivate()) {
28+
$definition->setPublic(false);
29+
$definition->setPrivate(true);
30+
}
31+
}
32+
33+
foreach ($container->getAliases() as $id => $alias) {
34+
if ($alias->isPrivate()) {
35+
$alias->setPublic(false);
36+
$alias->setPrivate(true);
37+
}
38+
}
39+
}
40+
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveChildDefinitionsPassTest.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -397,26 +397,6 @@ public function testSetAutoconfiguredOnServiceIsParent()
397397
$this->assertFalse($container->getDefinition('child1')->isAutoconfigured());
398398
}
399399

400-
public function testPrivateHasHigherPrecedenceThanPublic()
401-
{
402-
$container = new ContainerBuilder();
403-
404-
$container->register('foo', 'stdClass')
405-
->setPublic(true)
406-
->setPrivate(true)
407-
;
408-
409-
$container->setAlias('bar', 'foo')
410-
->setPublic(false)
411-
->setPrivate(false)
412-
;
413-
414-
$this->process($container);
415-
416-
$this->assertFalse($container->getDefinition('foo')->isPublic());
417-
$this->assertFalse($container->getAlias('bar')->isPublic());
418-
}
419-
420400
/**
421401
* @group legacy
422402
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\DependencyInjection\Compiler\ResolvePrivatesPass;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
18+
class ResolvePrivatesPassTest extends TestCase
19+
{
20+
public function testPrivateHasHigherPrecedenceThanPublic()
21+
{
22+
$container = new ContainerBuilder();
23+
24+
$container->register('foo', 'stdClass')
25+
->setPublic(true)
26+
->setPrivate(true)
27+
;
28+
29+
$container->setAlias('bar', 'foo')
30+
->setPublic(false)
31+
->setPrivate(false)
32+
;
33+
34+
(new ResolvePrivatesPass())->process($container);
35+
36+
$this->assertFalse($container->getDefinition('foo')->isPublic());
37+
$this->assertFalse($container->getAlias('bar')->isPublic());
38+
}
39+
}

0 commit comments

Comments
 (0)