8000 Make as many services private as possible by ro0NL · Pull Request #23867 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Make as many services private as possible #23867

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

Closed
wants to merge 1 commit into from
Closed
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
Make as many services private as possible
  • Loading branch information
ro0NL committed Aug 18, 2017
commit 5ffa53b5180d5ffd8938cfa6f742bcf02e78e98f
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\LogicException;
use Symfony\Component\DependencyInjection\Reference;

/**
* @author Roland Franssen <franssen.roland@gmail.com>
*
* @internal to be removed in 4.0
*/
class DeprecatePublicServicesPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
$services = array();
foreach ($container->findTaggedServiceIds('deprecated.public') as $id => $tags) {
$definition = $container->getDefinition($id);
if ($definition->isPublic()) {
throw new LogicException(sprintf('Mark the service "%s" private before deprecating public access to it.', $id));
}

$definition->clearTag('deprecated.public');
$services[$id] = new Reference($id);
}

if (!$services) {
return;
}

$container->getDefinition(ServiceLocatorTagPass::register($container, $services))
->setPublic(true)
->setDeprecated('The service "%service_id%" is internal and deprecated since Symfony 3.4 and will be removed in Symfony 4.0');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function __construct()
new CheckCircularReferencesPass(),
new CheckReferenceValidityPass(),
new CheckArgumentsValidityPass(),
new DeprecatePublicServicesPass(),
));

$this->removingPasses = array(array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,23 @@ public function testPrivateServiceTriggersDeprecation()

$container->get('bar');
}

public function testDeprecatePublicService()
{
$container = new ContainerBuilder();
$container->register('was_public', 'stdClass')
->setPublic(false)
->addTag('deprecated.public');
$container->register('ref_was_public', 'stdClass')
->setPublic(true)
->setProperty('foo', new Reference('was_public'));

$container->compile();

$this->assertTrue($container->has('was_public'));
$this->assertInstanceOf('stdClass', $service = $container->get('was_public'));
$this->assertSame($service, $container->get('ref_was_public')->foo);
}
}

class FooClass
Expand Down
0