Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #24468 [DI] Turn private defs to non-public ones before removing …
…passes (nicolas-grekas) This PR was merged into the 3.4 branch. Discussion ---------- [DI] Turn private defs to non-public ones before removing passes | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #24465 | License | MIT | Doc PR | - As spotted by @stof in #24465 (comment). Commits ------- e5d0934 [DI] Turn private defs to non-public ones before removing passes
- Loading branch information
Showing
5 changed files
with
85 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/Symfony/Component/DependencyInjection/Compiler/ResolvePrivatesPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
class ResolvePrivatesPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
foreach ($container->getDefinitions() as $id => $definition) { | ||
if ($definition->isPrivate()) { | ||
$definition->setPublic(false); | ||
$definition->setPrivate(true); | ||
} | ||
} | ||
|
||
foreach ($container->getAliases() as $id => $alias) { | ||
if ($alias->isPrivate()) { | ||
$alias->setPublic(false); | ||
$alias->setPrivate(true); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolvePrivatesPassTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Compiler; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\Compiler\ResolvePrivatesPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
class ResolvePrivatesPassTest extends TestCase | ||
{ | ||
public function testPrivateHasHigherPrecedenceThanPublic() | ||
{ | ||
$container = new ContainerBuilder(); | ||
|
||
$container->register('foo', 'stdClass') | ||
->setPublic(true) | ||
->setPrivate(true) | ||
; | ||
|
||
$container->setAlias('bar', 'foo') | ||
->setPublic(false) | ||
->setPrivate(false) | ||
; | ||
|
||
(new ResolvePrivatesPass())->process($container); | ||
|
||
$this->assertFalse($container->getDefinition('foo')->isPublic()); | ||
$this->assertFalse($container->getAlias('bar')->isPublic()); | ||
} | ||
} |