8000 [DependencyInjection] Do not trigger deprecation when injecting private services by wouterj · Pull Request #19682 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Do not trigger deprecation when injecting private services #19682

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
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
7 changes: 6 additions & 1 deletion src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ public function has($id)
*/
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE)
{
$triggerPrivateDeprecations = true;
if (func_num_a 8000 rgs() > 2) {
$triggerPrivateDeprecations = func_get_arg(2);
}

// Attempt to retrieve the service by checking first aliases then
// available services. Service IDs are case insensitive, however since
// this method can be called thousands of times during a request, avoid
Expand Down Expand Up @@ -281,7 +286,7 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE

return;
}
if (isset($this->privates[$id])) {
if ($triggerPrivateDeprecations && isset($this->privates[$id])) {
@trigger_error(sprintf('Requesting the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1377,13 +1377,13 @@ private function getServiceCall($id, Reference $reference = null)
}

if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
return sprintf('$this->get(\'%s\', ContainerInterface::NULL_ON_INVALID_REFERENCE)', $id);
return sprintf('$this->get(\'%s\', ContainerInterface::NULL_ON_INVALID_REFERENCE, false)', $id);
} else {
if ($this->container->hasAlias($id)) {
$id = (string) $this->container->getAlias($id);
}

return sprintf('$this->get(\'%s\')', $id);
return sprintf('$this->get(\'%s\', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)', $id);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public function getFunctions()
{
return array(
new ExpressionFunction('service', function ($arg) {
return sprintf('$this->get(%s)', $arg);
return sprintf('$this->get(%s, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)', $arg);
}, function (array $variables, $value) {
return $variables['container']->get($value);
return $variables['container']->get($value, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false);
}),

new ExpressionFunction('parameter', function ($arg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,34 @@ public function testServicesWithAnonymousFactories()
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services19.php', $dumper->dump(), '->dump() dumps services with anonymous factories');
}

public function testPrivateServices()
{
$fooDefinition = new Definition('stdClass');
$fooDefinition->setProperty('value', 'something');
$fooDefinition->setPublic(false);

$fooUserDefinition = new Definition('stdClass');
$fooUserDefinition->setProperty('foo', new Reference('foo'));

$builder = new ContainerBuilder();
$builder->setResourceTracking(false);

$builder->addDefinitions(array(
'foo' => $fooDefinition,
'foo_user' => $fooUserDefinition,
));

$dumper = new PhpDumper($builder);
$class = 'Symfony_DI_PhpDumper_Test_Private_Service';
eval('?>'.$dumper->dump(array('class' => $class)));

$container = new $class();

$expected = new \stdClass();
$expected->value = 'something';
$this->assertEquals($expected, $container->get('foo_user')->foo);
}

public function testAddServiceIdWithUnsupportedCharacters()
{
$class = 'Symfony_DI_PhpDumper_Test_Unsupported_Characters';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function __construct()
*/
protected function getBarService()
{
$a = $this->get('foo.baz');
$a = $this->get('foo.baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false);

$this->services['bar'] = $instance = new \Bar\FooClass('foo', $a, $this->getParameter('foo_bar'));

Expand All @@ -93,7 +93,7 @@ protected function getBazService()
{
$this->services['baz'] = $instance = new \Baz();

$instance->setFoo($this->get('foo_with_inline'));
$instance->setFoo($this->get('foo_with_inline', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false));

return $instance;
}
Expand All @@ -110,7 +110,7 @@ protected function getConfiguredServiceService()
{
$this->services['configured_service'] = $instance = new \stdClass();

$this->get('configurator_service')->configureStdClass($instance);
$this->get('configurator_service', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)->configureStdClass($instance);

return $instance;
}
Expand All @@ -127,7 +127,7 @@ protected function getConfiguredServiceSimpleService()
{
$this->services['configured_service_simple'] = $instance = new \stdClass();

$this->get('configurator_service_simple')->configureStdClass($instance);
$this->get('configurator_service_simple', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)->configureStdClass($instance);

return $instance;
}
Expand Down Expand Up @@ -198,7 +198,7 @@ protected function getDeprecatedServiceService()
*/
protected function getFactoryServiceService()
{
return $this->services['factory_service'] = $this->get('foo.baz')->getInstance();
return $this->services['factory_service'] = $this->get('foo.baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)->getInstance();
}

/**
Expand All @@ -211,7 +211,7 @@ protected function getFactoryServiceService()
*/
protected function getFactoryServiceSimpleService()
{
return $this->services['factory_service_simple'] = $this->get('factory_simple')->getInstance();
return $this->services['factory_service_simple'] = $this->get('factory_simple', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)->getInstance();
}

/**
Expand All @@ -224,11 +224,11 @@ protected function getFactoryServiceSimpleService()
*/
protected function getFooService()
{
$a = $this->get('foo.baz');
$a = $this->get('foo.baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false);

$this->services['foo'] = $instance = \Bar\FooClass::getInstance('foo', $a, array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo').'', 'foobar' => $this->getParameter('foo')), true, $this);

$instance->setBar($this->get('bar'));
$instance->setBar($this->get('bar', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false));
$instance->initialize();
$instance->foo = 'bar';
$instance->moo = $a;
Expand Down Expand Up @@ -279,7 +279,7 @@ protected function getFooWithInlineService()
{
$this->services['foo_with_inline'] = $instance = new \Foo();

$instance->setBar($this->get('inlined'));
$instance->setBar($this->get('inlined', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false));

return $instance;
}
Expand All @@ -298,15 +298,15 @@ protected function getMethodCall1Service()

$this->services['method_call1'] = $instance = new \Bar\FooClass();

$instance->setBar($this->get('foo'));
$instance->setBar($this->get('foo2', ContainerInterface::NULL_ON_INVALID_REFERENCE));
$instance->setBar($this->get('foo', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false));
$instance->setBar($this->get('foo2', ContainerInterface::NULL_ON_INVALID_REFERENCE, false));
if ($this->has('foo3')) {
$instance->setBar($this->get('foo3', ContainerInterface::NULL_ON_INVALID_REFERENCE));
$instance->setBar($this->get('foo3', ContainerInterface::NULL_ON_INVALID_REFERENCE, false));
}
if ($this->has('foobaz')) {
$instance->setBar($this->get('foobaz', ContainerInterface::NULL_ON_INVALID_REFERENCE));
$instance->setBar($this->get('foobaz', ContainerInterface::NULL_ON_INVALID_REFERENCE, false));
}
$instance->setBar(($this->get("foo")->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default"))));
$instance->setBar(($this->get("foo", ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default"))));

return $instance;
}
Expand All @@ -321,7 +321,7 @@ protected function getMethodCall1Service()
*/
protected function getNewFactoryServiceService()
{
$this->services['new_factory_service'] = $instance = $this->get('new_factory')->getInstance();
$this->services['new_factory_service'] = $instance = $this->get('new_factory', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)->getInstance();

$instance->foo = 'bar';

Expand Down Expand Up @@ -370,7 +370,7 @@ protected function getConfiguratorServiceService()
{
$this->services['configurator_service'] = $instance = new \ConfClass();

$instance->setFoo($this->get('baz'));
$instance->setFoo($this->get('baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false));

return $instance;
}
Expand Down Expand Up @@ -425,7 +425,7 @@ protected function getInlinedService()
{
$this->services['inlined'] = $instance = new \Bar();

$instance->setBaz($this->get('baz'));
$instance->setBaz($this->get('baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false));
$instance->pub = 'pub';

return $instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function compile()
*/
protected function getBarService()
{
$a = $this->get('foo.baz');
$a = $this->get('foo.baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false);

$this->services['bar'] = $instance = new \Bar\FooClass('foo', $a, $this->getParameter('foo_bar'));

Expand All @@ -91,7 +91,7 @@ protected function getBazService()
{
$this->services['baz'] = $instance = new \Baz();

$instance->setFoo($this->get('foo_with_inline'));
$instance->setFoo($this->get('foo_with_inline', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false));

return $instance;
}
Expand All @@ -107,7 +107,7 @@ protected function getBazService()
protected function getConfiguredServiceService()
{
$a = new \ConfClass();
$a->setFoo($this->get('baz'));
$a->setFoo($this->get('baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false));

$this->services['configured_service'] = $instance = new \stdClass();

Expand Down Expand Up @@ -186,7 +186,7 @@ protected function getDeprecatedServiceService()
*/
protected function getFactoryServiceService()
{
return $this->services['factory_service'] = $this->get('foo.baz')->getInstance();
return $this->services['factory_service'] = $this->get('foo.baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)->getInstance();
}

/**
Expand All @@ -212,11 +212,11 @@ protected function getFactoryServiceSimpleService()
*/
protected function getFooService()
{
$a = $this->get('foo.baz');
$a = $this->get('foo.baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false);

$this->services['foo'] = $instance = \Bar\FooClass::getInstance('foo', $a, array('bar' => 'foo is bar', 'foobar' => 'bar'), true, $this);

$instance->setBar($this->get('bar'));
$instance->setBar($this->get('bar', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false));
$instance->initialize();
$instance->foo = 'bar';
$instance->moo = $a;
Expand Down Expand Up @@ -267,7 +267,7 @@ protected function getFooWithInlineService()

$this->services['foo_with_inline'] = $instance = new \Foo();

$a->setBaz($this->get('baz'));
$a->setBaz($this->get('baz', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false));
$a->pub = 'pub';

$instance->setBar($a);
Expand All @@ -289,9 +289,9 @@ protected function getMethodCall1Service()

$this->services['method_call1'] = $instance = n 582C ew \Bar\FooClass();

$instance->setBar($this->get('foo'));
$instance->setBar($this->get('foo', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false));
$instance->setBar(NULL);
$instance->setBar(($this->get("foo")->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default"))));
$instance->setBar(($this->get("foo", ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false)->foo() . (($this->hasParameter("foo")) ? ($this->getParameter("foo")) : ("default"))));

return $instance;
}
Expand Down
0