Skip to content

[DependencyInjection] deprecated synchronized services #13289

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
merged 1 commit into from
Jan 9, 2015
Merged
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
4 changes: 4 additions & 0 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ UPGRADE FROM 2.x to 3.0
been removed in favor of `Definition::setFactory()`. Services defined using
YAML or XML use the same syntax as configurators.

* Synchronized services are deprecated and the following methods have been
removed: `ContainerBuilder::synchronize()`, `Definition::isSynchronized()`,
and `Definition::setSynchronized()`.

### EventDispatcher

* The interface `Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcherInterface`
Expand Down
6 changes: 1 addition & 5 deletions autoload.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ class DeprecationErrorHandler
$class = isset($trace[$i]['object']) ? get_class($trace[$i]['object']) : $trace[$i]['class'];
$method = $trace[$i]['function'];

$type =
0 === strpos($method, 'testLegacy')
|| 0 === strpos($method, 'provideLegacy')
|| strpos($class, '\Legacy')
? 'legacy' : 'remaining';
$type = 0 === strpos($method, 'testLegacy') || 0 === strpos($method, 'provideLegacy') || 0 === strpos($method, 'getLegacy') || strpos($class, '\Legacy') ? 'legacy' : 'remaining';

if ('legacy' === $type && 0 === (error_reporting() & E_USER_DEPRECATED)) {
@++$deprecations[$type]['Silenced']['count'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,13 @@ private function getContainerDefinitionData(Definition $definition, $omitTags =
'public' => $definition->isPublic(),
'synthetic' => $definition->isSynthetic(),
'lazy' => $definition->isLazy(),
'synchronized' => $definition->isSynchronized(),
'abstract' => $definition->isAbstract(),
'file' => $definition->getFile(),
);
if (method_exists($definition, 'isSynchronized')) {
$data['synchronized'] = $definition->isSynchronized();
}

$data['abstract'] = $definition->isAbstract();
$data['file'] = $definition->getFile();

if ($definition->getFactoryClass()) {
$data['factory_class'] = $definition->getFactoryClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,13 @@ protected function describeContainerDefinition(Definition $definition, array $op
."\n".'- Public: '.($definition->isPublic() ? 'yes' : 'no')
."\n".'- Synthetic: '.($definition->isSynthetic() ? 'yes' : 'no')
."\n".'- Lazy: '.($definition->isLazy() ? 'yes' : 'no')
."\n".'- Synchronized: '.($definition->isSynchronized() ? 'yes' : 'no')
."\n".'- Abstract: '.($definition->isAbstract() ? 'yes' : 'no');
;

if (method_exists($definition, 'isSynchronized')) {
$output .= "\n".'- Synchronized: '.($definition->isSynchronized() ? 'yes' : 'no');
}

$output .= "\n".'- Abstract: '.($definition->isAbstract() ? 'yes' : 'no');

if ($definition->getFile()) {
$output .= "\n".'- File: `'.$definition->getFile().'`';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ protected function describeContainerDefinition(Definition $definition, array $op
$description[] = sprintf('<comment>Public</comment> %s', $definition->isPublic() ? 'yes' : 'no');
$description[] = sprintf('<comment>Synthetic</comment> %s', $definition->isSynthetic() ? 'yes' : 'no');
$description[] = sprintf('<comment>Lazy</comment> %s', $definition->isLazy() ? 'yes' : 'no');
$description[] = sprintf('<comment>Synchronized</comment> %s', $definition->isSynchronized() ? 'yes' : 'no');
if (method_exists($definition, 'isSynchronized')) {
$description[] = sprintf('<comment>Synchronized</comment> %s', $definition->isSynchronized() ? 'yes' : 'no');
}
$description[] = sprintf('<comment>Abstract</comment> %s', $definition->isAbstract() ? 'yes' : 'no');

if ($definition->getFile()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,9 @@ private function getContainerDefinitionDocument(Definition $definition, $id = nu
$serviceXML->setAttribute('public', $definition->isPublic() ? 'true' : 'false');
$serviceXML->setAttribute('synthetic', $definition->isSynthetic() ? 'true' : 'false');
$serviceXML->setAttribute('lazy', $definition->isLazy() ? 'true' : 'false');
$serviceXML->setAttribute('synchronized', $definition->isSynchronized() ? 'true' : 'false');
if (method_exists($definition, 'isSynchronized')) {
$serviceXML->setAttribute('synchronized', $definition->isSynchronized(false) ? 'true' : 'false');
}
$serviceXML->setAttribute('abstract', $definition->isAbstract() ? 'true' : 'false');
$serviceXML->setAttribute('file', $definition->getFile());

Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

2.7.0
-----

* deprecated synchronized services

2.6.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ public function set($id, $service, $scope = self::SCOPE_CONTAINER)

parent::set($id, $service, $scope);

if (isset($this->obsoleteDefinitions[$id]) && $this->obsoleteDefinitions[$id]->isSynchronized()) {
if (isset($this->obsoleteDefinitions[$id]) && $this->obsoleteDefinitions[$id]->isSynchronized(false)) {
$this->synchronize($id);
}
}
Expand Down Expand Up @@ -1121,9 +1121,15 @@ private function getProxyInstantiator()
* service by calling all methods referencing it.
*
* @param string $id A service id
*
* @deprecated since version 2.7, will be removed in 3.0.
*/
private function synchronize($id)
{
if ('request' !== $id) {
trigger_error('The '.__METHOD__.' method is deprecated in version 2.7 and will be removed in version 3.0.', E_USER_DEPRECATED);
}

foreach ($this->definitions as $definitionId => $definition) {
// only check initialized services
if (!$this->initialized($definitionId)) {
Expand Down
16 changes: 14 additions & 2 deletions src/Symfony/Component/DependencyInjection/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -661,9 +661,15 @@ public function isPublic()
* @return Definition The current instance
*
* @api
*
* @deprecated since version 2.7, will be removed in 3.0.
*/
public function setSynchronized($boolean)
public function setSynchronized($boolean, $triggerDeprecationError = true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it a BC break to add an extra argument to a public method signature? The method is flagged with @api...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not on interface so if you overwrite class you don't need to copy params, yet this argument is optional, so I guess it's not real BC break.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thus, the Definition class is not really meant to be a class extended by inheritance. I don't see any use case where this would bring any benefit actually.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

{
if ($triggerDeprecationError) {
trigger_error('The '.__METHOD__.' method is deprecated in version 2.7 and will be removed in version 3.0.', E_USER_DEPRECATED);
}

$this->synchronized = (bool) $boolean;

return $this;
Expand All @@ -675,9 +681,15 @@ public function setSynchronized($boolean)
* @return bool
*
* @api
*
* @deprecated since version 2.7, will be removed in 3.0.
*/
public function isSynchronized()
public function isSynchronized($triggerDeprecationError = true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

{
if ($triggerDeprecationError) {
trigger_error('The '.__METHOD__.' method is deprecated in version 2.7 and will be removed in version 3.0.', E_USER_DEPRECATED);
}

return $this->synchronized;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,13 +695,19 @@ private function addServices()
* @param Definition $definition A Definition instance
*
* @return string|null
*
* @deprecated since version 2.7, will be removed in 3.0.
*/
private function addServiceSynchronizer($id, Definition $definition)
{
if (!$definition->isSynchronized()) {
if (!$definition->isSynchronized(false)) {
return;
}

if ('request' !== $id) {
trigger_error('Synchronized services were deprecated in version 2.7 and won\'t work anymore in 3.0.', E_USER_DEPRECATED);
}

$code = '';
foreach ($this->container->getDefinitions() as $definitionId => $definition) {
foreach ($definition->getMethodCalls() as $call) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private function addService($definition, $id, \DOMElement $parent)
if ($definition->isSynthetic()) {
$service->setAttribute('synthetic', 'true');
}
if ($definition->isSynchronized()) {
if ($definition->isSynchronized(false)) {
$service->setAttribute('synchronized', 'true');
}
if ($definition->isLazy()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private function addService($id, $definition)
$code .= sprintf(" synthetic: true\n");
}

if ($definition->isSynchronized()) {
if ($definition->isSynchronized(false)) {
$code .= sprintf(" synchronized: true\n");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,17 @@ private function parseDefinition($id, \DOMElement $service, $file)
$definition = new Definition();
}

foreach (array('class', 'scope', 'public', 'factory-class', 'factory-method', 'factory-service', 'synthetic', 'synchronized', 'lazy', 'abstract') as $key) {
foreach (array('class', 'scope', 'public', 'factory-class', 'factory-method', 'factory-service', 'synthetic', 'lazy', 'abstract') as $key) {
if ($value = $service->getAttribute($key)) {
$method = 'set'.str_replace('-', '', $key);
$definition->$method(XmlUtils::phpize($value));
}
}

if ($value = $service->getAttribute('synchronized')) {
$definition->setSynchronized(XmlUtils::phpize($value), 'request' !== $id);
}

if ($files = $this->getChildren($service, 'file')) {
$definition->setFile($files[0]->nodeValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private function parseDefinition($id, $service, $file)
}

if (isset($service['synchronized'])) {
$definition->setSynchronized($service['synchronized']);
$definition->setSynchronized($service['synchronized'], 'request' !== $id);
}

if (isset($service['lazy'])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -683,8 +683,10 @@ public function testNoExceptionWhenSetSyntheticServiceOnAFrozenContainer()
$this->assertEquals($a, $container->get('a'));
}

public function testSetOnSynchronizedService()
public function testLegacySetOnSynchronizedService()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

$container = new ContainerBuilder();
$container->register('baz', 'BazClass')
->setSynchronized(true)
Expand All @@ -700,8 +702,10 @@ public function testSetOnSynchronizedService()
$this->assertSame($baz, $container->get('bar')->getBaz());
}

public function testSynchronizedServiceWithScopes()
public function testLegacySynchronizedServiceWithScopes()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

$container = new ContainerBuilder();
$container->addScope(new Scope('foo'));
$container->register('baz', 'BazClass')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,10 @@ public function testSetIsSynthetic()
* @covers Symfony\Component\DependencyInjection\Definition::setSynchronized
* @covers Symfony\Component\DependencyInjection\Definition::isSynchronized
*/
public function testSetIsSynchronized()
public function testLegacySetIsSynchronized()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

$def = new Definition('stdClass');
$this->assertFalse($def->isSynchronized(), '->isSynchronized() returns false by default');
$this->assertSame($def, $def->setSynchronized(true), '->setSynchronized() implements a fluent interface');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ public function testAddService()
}
}

public function testLegacySynchronizedServices()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

$container = include self::$fixturesPath.'/containers/container20.php';
$dumper = new PhpDumper($container);
$this->assertEquals(str_replace('%path%', str_replace('\\', '\\\\', self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR), file_get_contents(self::$fixturesPath.'/php/services20.php')), $dumper->dump(), '->dump() dumps services');
}

public function testServicesWithAnonymousFactories()
{
$container = include self::$fixturesPath.'/containers/container19.php';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

require_once __DIR__.'/../includes/classes.php';

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

$container = new ContainerBuilder();
$container
->register('request', 'Request')
->setSynchronized(true)
;
$container
->register('depends_on_request', 'stdClass')
->addMethodCall('setRequest', array(new Reference('request', ContainerInterface::NULL_ON_INVALID_REFERENCE, false)))
;

return $container;
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@
$container
->register('request', 'Request')
->setSynthetic(true)
->setSynchronized(true)
;
$container
->register('depends_on_request', 'stdClass')
->addMethodCall('setRequest', array(new Reference('request', ContainerInterface::NULL_ON_INVALID_REFERENCE, false)))
;
$container
->register('configurator_service', 'ConfClass')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ digraph sc {
node_inlined [label="inlined\nBar\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_baz [label="baz\nBaz\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_request [label="request\nRequest\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_depends_on_request [label="depends_on_request\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_configurator_service [label="configurator_service\nConfClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_configured_service [label="configured_service\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_decorated [label="decorated\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
Expand All @@ -38,6 +37,5 @@ digraph sc {
node_foo_with_inline -> node_inlined [label="setBar()" style="dashed"];
node_inlined -> node_baz [label="setBaz()" style="dashed"];
node_baz -> node_foo_with_inline [label="setFoo()" style="dashed"];
node_depends_on_request -> node_request [label="setRequest()" style="dashed"];
node_configurator_service -> node_baz [label="setFoo()" style="dashed"];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;

/**
* ProjectServiceContainer
*
* This class has been auto-generated
* by the Symfony Dependency Injection Component.
*/
class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();

/**
* Constructor.
*/
public function __construct()
{
parent::__construct();
$this->methodMap = array(
'depends_on_request' => 'getDependsOnRequestService',
'request' => 'getRequestService',
);
}

/**
* Gets the 'depends_on_request' service.
*
* This service is shared.
* This method always returns the same instance of the service.
*
* @return \stdClass A stdClass instance.
*/
protected function getDependsOnRequestService()
{
$this->services['depends_on_request'] = $instance = new \stdClass();

$instance->setRequest($this->get('request', ContainerInterface::NULL_ON_INVALID_REFERENCE));

return $instance;
}

/**
* Gets the 'request' service.
*
* This service is shared.
* This method always returns the same instance of the service.
*
* @return \Request A Request instance.
*/
protected function getRequestService()
{
return $this->services['request'] = new \Request();
}

/**
* Updates the 'request' service.
*/
protected function synchronizeRequestService()
{
if ($this->initialized('depends_on_request')) {
$this->get('depends_on_request')->setRequest($this->get('request', ContainerInterface::NULL_ON_INVALID_REFERENCE));
}
}
}
Loading