-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Add a mechanism to deprecate public services to private #36470
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
nicolas-grekas
merged 1 commit into
symfony:master
from
fancyweb:di-deprecate-public-service
May 4, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
72 changes: 72 additions & 0 deletions
72
src/Symfony/Component/DependencyInjection/Compiler/AliasDeprecatedPublicServicesPass.php
This file contains hidden or 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,72 @@ | ||
<?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; | ||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
final class AliasDeprecatedPublicServicesPass extends AbstractRecursivePass | ||
{ | ||
private $tagName; | ||
|
||
private $aliases = []; | ||
|
||
public function __construct(string $tagName = 'container.private') | ||
{ | ||
$this->tagName = $tagName; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function processValue($value, bool $isRoot = false) | ||
{ | ||
if ($value instanceof Reference && isset($this->aliases[$id = (string) $value])) { | ||
return new Reference($this->aliases[$id], $value->getInvalidBehavior()); | ||
} | ||
|
||
return parent::processValue($value, $isRoot); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
foreach ($container->findTaggedServiceIds($this->tagName) as $id => $tags) { | ||
if (null === $package = $tags[0]['package'] ?? null) { | ||
throw new InvalidArgumentException(sprintf('The "package" attribute is mandatory for the "%s" tag on the "%s" service.', $this->tagName, $id)); | ||
} | ||
|
||
if (null === $version = $tags[0]['version'] ?? null) { | ||
throw new InvalidArgumentException(sprintf('The "version" attribute is mandatory for the "%s" tag on the "%s" service.', $this->tagName, $id)); | ||
} | ||
|
||
$definition = $container->getDefinition($id); | ||
if (!$definition->isPublic() || $definition->isPrivate()) { | ||
throw new InvalidArgumentException(sprintf('The "%s" service is private: it cannot have the "%s" tag.', $id, $this->tagName)); | ||
} | ||
|
||
$container | ||
->setAlias($id, $aliasId = '.'.$this->tagName.'.'.$id) | ||
->setPublic(true) | ||
->setDeprecated($package, $version, 'Accessing the "%alias_id%" service directly from the container is deprecated, use dependency injection instead.'); | ||
|
||
$container->setDefinition($aliasId, $definition); | ||
|
||
$this->aliases[$id] = $aliasId; | ||
} | ||
|
||
parent::process($container); | ||
} | ||
} |
This file contains hidden or 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
73 changes: 73 additions & 0 deletions
73
...ny/Component/DependencyInjection/Tests/Compiler/AliasDeprecatedPublicServicesPassTest.php
This file contains hidden or 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,73 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Compiler; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\Compiler\AliasDeprecatedPublicServicesPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; | ||
|
||
final class AliasDeprecatedPublicServicesPassTest extends TestCase | ||
{ | ||
public function testProcess() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container | ||
->register('foo') | ||
->setPublic(true) | ||
->addTag('container.private', ['package' => 'foo/bar', 'version' => '1.2']); | ||
|
||
(new AliasDeprecatedPublicServicesPass())->process($container); | ||
|
||
$this->assertTrue($container->hasAlias('foo')); | ||
|
||
$alias = $container->getAlias('foo'); | ||
|
||
$this->assertSame('.container.private.foo', (string) $alias); | ||
$this->assertTrue($alias->isPublic()); | ||
$this->assertFalse($alias->isPrivate()); | ||
$this->assertSame([ | ||
'package' => 'foo/bar', | ||
'version' => '1.2', | ||
'message' => 'Accessing the "foo" service directly from the container is deprecated, use dependency injection instead.', | ||
], $alias->getDeprecation('foo')); | ||
} | ||
|
||
/** | ||
* @dataProvider processWithMissingAttributeProvider | ||
*/ | ||
public function testProcessWithMissingAttribute(string $attribute, array $attributes) | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage(sprintf('The "%s" attribute is mandatory for the "container.private" tag on the "foo" service.', $attribute)); | ||
|
||
$container = new ContainerBuilder(); | ||
$container | ||
->register('foo') | ||
->addTag('container.private', $attributes); | ||
|
||
(new AliasDeprecatedPublicServicesPass())->process($container); | ||
} | ||
|
||
public function processWithMissingAttributeProvider() | ||
{ | ||
return [ | ||
['package', ['version' => '1.2']], | ||
['version', ['package' => 'foo/bar']], | ||
]; | ||
} | ||
|
||
public function testProcessWithNonPublicService() | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('The "foo" service is private: it cannot have the "container.private" tag.'); | ||
|
||
$container = new ContainerBuilder(); | ||
$container | ||
->register('foo') | ||
->setPublic(false) | ||
->addTag('container.private', ['package' => 'foo/bar', 'version' => '1.2']); | ||
|
||
(new AliasDeprecatedPublicServicesPass())->process($container); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.