Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[2.6] cleanup deprecated uses
  • Loading branch information
nicolas-grekas committed Jan 5, 2015
1 parent a054bed commit 2856cae
Show file tree
Hide file tree
Showing 32 changed files with 148 additions and 110 deletions.
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
Expand Down Expand Up @@ -340,6 +341,23 @@ private function getContainerDefinitionDocument(Definition $definition, $id = nu
$serviceXML->setAttribute('factory-method', $definition->getFactoryMethod());
}

if ($factory = $definition->getFactory()) {
$serviceXML->appendChild($factoryXML = $dom->createElement('factory'));

if (is_array($factory)) {
if ($factory[0] instanceof Reference) {
$factoryXML->setAttribute('service', (string) $factory[0]);
} elseif ($factory[0] instanceof Definition) {
throw new \InvalidArgumentException('Factory is not describable.');
} else {
$factoryXML->setAttribute('class', $factory[0]);
}
$factoryXML->setAttribute('method', $factory[1]);
} else {
$factoryXML->setAttribute('function', $factory);
}
}

$serviceXML->setAttribute('scope', $definition->getScope());
$serviceXML->setAttribute('public', $definition->isPublic() ? 'true' : 'false');
$serviceXML->setAttribute('synthetic', $definition->isSynthetic() ? 'true' : 'false');
Expand Down
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
Expand Down Expand Up @@ -98,8 +99,7 @@ public static function getContainerDefinitions()
->setLazy(true)
->setSynchronized(true)
->setAbstract(true)
->setFactoryClass('Full\\Qualified\\FactoryClass')
->setFactoryMethod('get'),
->setFactory(array('Full\\Qualified\\FactoryClass', 'get')),
'definition_2' => $definition2
->setPublic(false)
->setSynthetic(true)
Expand All @@ -110,8 +110,7 @@ public static function getContainerDefinitions()
->addTag('tag1', array('attr1' => 'val1', 'attr2' => 'val2'))
->addTag('tag1', array('attr3' => 'val3'))
->addTag('tag2')
->setFactoryService('factory.service')
->setFactoryMethod('get'),
->setFactory(array(new Reference('factory.service'), 'get')),
);
}

Expand Down
Expand Up @@ -2,6 +2,8 @@
<container>
<alias id="alias_1" service="service_1" public="true"/>
<alias id="alias_2" service="service_2" public="false"/>
<definition id="definition_1" class="Full\Qualified\Class1" factory-class="Full\Qualified\FactoryClass" factory-method="get" scope="container" public="true" synthetic="false" lazy="true" synchronized="true" abstract="true" file=""/>
<definition id="definition_1" class="Full\Qualified\Class1" scope="container" public="true" synthetic="false" lazy="true" synchronized="true" abstract="true" file="">
<factory class="Full\Qualified\FactoryClass" method="get"/>
</definition>
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerBuilder"/>
</container>
Expand Up @@ -2,8 +2,11 @@
<container>
<alias id="alias_1" service="service_1" public="true"/>
<alias id="alias_2" service="service_2" public="false"/>
<definition id="definition_1" class="Full\Qualified\Class1" factory-class="Full\Qualified\FactoryClass" factory-method="get" scope="container" public="true" synthetic="false" lazy="true" synchronized="true" abstract="true" file=""/>
<definition id="definition_2" class="Full\Qualified\Class2" factory-service="factory.service" factory-method="get" scope="container" public="false" synthetic="true" lazy="false" synchronized="false" abstract="false" file="/path/to/file">
<definition id="definition_1" class="Full\Qualified\Class1" scope="container" public="true" synthetic="false" lazy="true" synchronized="true" abstract="true" file="">
<factory class="Full\Qualified\FactoryClass" method="get"/>
</definition>
<definition id="definition_2" class="Full\Qualified\Class2" scope="container" public="false" synthetic="true" lazy="false" synchronized="false" abstract="false" file="/path/to/file">
<factory service="factory.service" method="get"/>
<tags>
<tag name="tag1">
<parameter name="attr1">val1</parameter>
Expand Down
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<container>
<definition id="definition_2" class="Full\Qualified\Class2" factory-service="factory.service" factory-method="get" scope="container" public="false" synthetic="true" lazy="false" synchronized="false" abstract="false" file="/path/to/file">
<definition id="definition_2" class="Full\Qualified\Class2" scope="container" public="false" synthetic="true" lazy="false" synchronized="false" abstract="false" file="/path/to/file">
<factory service="factory.service" method="get"/>
<tags>
<tag name="tag1">
<parameter name="attr1">val1</parameter>
Expand Down
@@ -1,9 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<container>
<tag name="tag1">
<definition id="definition_2" class="Full\Qualified\Class2" factory-service="factory.service" factory-method="get" scope="container" public="false" synthetic="true" lazy="false" synchronized="false" abstract="false" file="/path/to/file"/>
<definition id="definition_2" class="Full\Qualified\Class2" scope="container" public="false" synthetic="true" lazy="false" synchronized="false" abstract="false" file="/path/to/file">
<factory service="factory.service" method="get"/>
</definition>
</tag>
<tag name="tag2">
<definition id="definition_2" class="Full\Qualified\Class2" factory-service="factory.service" factory-method="get" scope="container" public="false" synthetic="true" lazy="false" synchronized="false" abstract="false" file="/path/to/file"/>
<definition id="definition_2" class="Full\Qualified\Class2" scope="container" public="false" synthetic="true" lazy="false" synchronized="false" abstract="false" file="/path/to/file">
<factory service="factory.service" method="get"/>
</definition>
</tag>
</container>
@@ -1,2 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<definition class="Full\Qualified\Class1" factory-class="Full\Qualified\FactoryClass" factory-method="get" scope="container" public="true" synthetic="false" lazy="true" synchronized="true" abstract="true" file=""/>
<definition class="Full\Qualified\Class1" scope="container" public="true" synthetic="false" lazy="true" synchronized="true" abstract="true" file="">
<factory class="Full\Qualified\FactoryClass" method="get"/>
</definition>
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<definition class="Full\Qualified\Class2" factory-service="factory.service" factory-method="get" scope="container" public="false" synthetic="true" lazy="false" synchronized="false" abstract="false" file="/path/to/file">
<definition class="Full\Qualified\Class2" scope="container" public="false" synthetic="true" lazy="false" synchronized="false" abstract="false" file="/path/to/file">
<factory service="factory.service" method="get"/>
<tags>
<tag name="tag1">
<parameter name="attr1">val1</parameter>
Expand Down
Expand Up @@ -26,8 +26,10 @@ public function setUp()
$this->globals = new GlobalVariables($this->container);
}

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

$securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');

$this->assertNull($this->globals->getSecurity());
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Debug/README.md
Expand Up @@ -24,7 +24,7 @@ if ('cli' !== php_sapi_name()) {
} elseif (!ini_get('log_errors') || ini_get('error_log')) {
ini_set('display_errors', 1);
}
ErrorHandler::register($errorReportingLevel);
ErrorHandler::register();
```

Note that the `Debug::enable()` call also registers the debug class loader
Expand Down
Expand Up @@ -21,42 +21,48 @@ class ClassNotFoundFatalErrorHandlerTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider provideClassNotFoundData
*/
public function testHandleClassNotFound($error, $translatedMessage, $autoloader = null)
public function testHandleClassNotFound($error, $translatedMessage)
{
if ($autoloader) {
// Unregister all autoloaders to ensure the custom provided
// autoloader is the only one to be used during the test run.
$autoloaders = spl_autoload_functions();
array_map('spl_autoload_unregister', $autoloaders);
spl_autoload_register($autoloader);
}

$handler = new ClassNotFoundFatalErrorHandler();

$exception = $handler->handleError($error, new FatalErrorException('', 0, $error['type'], $error['file'], $error['line']));

if ($autoloader) {
spl_autoload_unregister($autoloader);
array_map('spl_autoload_register', $autoloaders);
}

$this->assertInstanceof('Symfony\Component\Debug\Exception\ClassNotFoundException', $exception);
$this->assertSame($translatedMessage, $exception->getMessage());
$this->assertSame($error['type'], $exception->getSeverity());
$this->assertSame($error['file'], $exception->getFile());
$this->assertSame($error['line'], $exception->getLine());
}

public function provideClassNotFoundData()
/**
* @dataProvider provideLegacyClassNotFoundData
*/
public function testLegacyHandleClassNotFound($error, $translatedMessage, $autoloader)
{
$prefixes = array('Symfony\Component\Debug\Exception\\' => realpath(__DIR__.'/../../Exception'));
$this->iniSet('error_reporting', -1 & E_USER_DEPRECATED);

$symfonyAutoloader = new SymfonyClassLoader();
$symfonyAutoloader->addPrefixes($prefixes);
// Unregister all autoloaders to ensure the custom provided
// autoloader is the only one to be used during the test run.
$autoloaders = spl_autoload_functions();
array_map('spl_autoload_unregister', $autoloaders);
spl_autoload_register($autoloader);

$symfonyUniversalClassLoader = new SymfonyUniversalClassLoader();
$symfonyUniversalClassLoader->registerPrefixes($prefixes);
$handler = new ClassNotFoundFatalErrorHandler();

$exception = $handler->handleError($error, new FatalErrorException('', 0, $error['type'], $error['file'], $error['line']));

spl_autoload_unregister($autoloader);
array_map('spl_autoload_register', $autoloaders);

$this->assertInstanceof('Symfony\Component\Debug\Exception\ClassNotFoundException', $exception);
$this->assertSame($translatedMessage, $exception->getMessage());
$this->assertSame($error['type'], $exception->getSeverity());
$this->assertSame($error['file'], $exception->getFile());
$this->assertSame($error['line'], $exception->getLine());
}

public function provideClassNotFoundData()
{
return array(
array(
array(
Expand Down Expand Up @@ -103,6 +109,20 @@ public function provideClassNotFoundData()
),
"Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\Bar\".\nDid you forget a \"use\" statement for \"Symfony\Component\Debug\Exception\UndefinedFunctionException\"?",
),
);
}

public function provideLegacyClassNotFoundData()
{
$prefixes = array('Symfony\Component\Debug\Exception\\' => realpath(__DIR__.'/../../Exception'));

$symfonyAutoloader = new SymfonyClassLoader();
$symfonyAutoloader->addPrefixes($prefixes);

$symfonyUniversalClassLoader = new SymfonyUniversalClassLoader();
$symfonyUniversalClassLoader->registerPrefixes($prefixes);

return array(
array(
array(
'type' => 1,
Expand Down
Expand Up @@ -74,16 +74,16 @@ public function process(ContainerBuilder $container)
if ($definition->getFactoryService()) {
$this->processArguments(array(new Reference($definition->getFactoryService())));
}
if (is_array($definition->getFactory())) {
$this->processArguments($definition->getFactory());
}

if (!$this->onlyConstructorArguments) {
$this->processArguments($definition->getMethodCalls());
$this->processArguments($definition->getProperties());
if ($definition->getConfigurator()) {
$this->processArguments(array($definition->getConfigurator()));
}
if ($definition->getFactory()) {
$this->processArguments(array($definition->getFactory()));
}
}
}

Expand Down Expand Up @@ -115,6 +115,9 @@ private function processArguments(array $arguments)
$this->processArguments($argument->getMethodCalls());
$this->processArguments($argument->getProperties());

if (is_array($argument->getFactory())) {
$this->processArguments($argument->getFactory());
}
if ($argument->getFactoryService()) {
$this->processArguments(array(new Reference($argument->getFactoryService())));
}
Expand Down
Expand Up @@ -144,6 +144,10 @@ private function isInlineableDefinition(ContainerBuilder $container, $id, Defini
return false;
}

if (count($ids) > 1 && is_array($factory = $definition->getFactory()) && ($factory[0] instanceof Reference || $factory[0] instanceof Definition)) {
return false;
}

if (count($ids) > 1 && $definition->getFactoryService()) {
return false;
}
Expand Down
Expand Up @@ -81,11 +81,9 @@ private function resolveDefinition($id, DefinitionDecorator $definition)
$def->setArguments($parentDef->getArguments());
$def->setMethodCalls($parentDef->getMethodCalls());
$def->setProperties($parentDef->getProperties());
if (null !== $parentDef->getFactoryMethod()) {
$def->setFactoryClass($parentDef->getFactoryClass());
$def->setFactoryMethod($parentDef->getFactoryMethod());
$def->setFactoryService($parentDef->getFactoryService());
}
$def->setFactoryClass($parentDef->getFactoryClass());
$def->setFactoryMethod($parentDef->getFactoryMethod());
$def->setFactoryService($parentDef->getFactoryService());
$def->setFactory($parentDef->getFactory());
$def->setConfigurator($parentDef->getConfigurator());
$def->setFile($parentDef->getFile());
Expand Down
Expand Up @@ -87,7 +87,7 @@ public function testProcessDetectsReferencesFromInlinedFactoryDefinitions()
;

$factory = new Definition();
$factory->setFactoryService('a');
$factory->setFactory(array(new Reference('a'), 'a'));

$container
->register('b')
Expand Down Expand Up @@ -124,13 +124,11 @@ public function testProcessDetectsFactoryReferences()

$container
->register('foo', 'stdClass')
->setFactoryClass('stdClass')
->setFactoryMethod('getInstance');
->setFactory(array('stdClass', 'getInstance'));

$container
->register('bar', 'stdClass')
->setFactoryService('foo')
->setFactoryMethod('getInstance');
->setFactory(array(new Reference('foo'), 'getInstance'));

$graph = $this->process($container);

Expand Down
Expand Up @@ -53,13 +53,11 @@ public function testProcessWithFactory()

$container
->register('a', 'stdClass')
->setFactoryService('b')
->setFactoryMethod('getInstance');
->setFactory(array(new Reference('b'), 'getInstance'));

$container
->register('b', 'stdClass')
->setFactoryService('a')
->setFactoryMethod('getInstance');
->setFactory(array(new Reference('a'), 'getInstance'));

$this->process($container);
}
Expand Down Expand Up @@ -88,8 +86,7 @@ public function testProcessDetectsIndirectCircularReferenceWithFactory()

$container
->register('b', 'stdClass')
->setFactoryService('c')
->setFactoryMethod('getInstance');
->setFactory(array(new Reference('c'), 'getInstance'));

$container->register('c')->addArgument(new Reference('a'));

Expand Down
Expand Up @@ -118,7 +118,7 @@ public function testProcessInlinesPrivateFactoryReference()
$b = $container
->register('b')
->setPublic(false)
->setFactoryService('a')
->setFactory(array(new Reference('a'), 'a'))
;

$container
Expand All @@ -142,7 +142,7 @@ public function testProcessDoesNotInlinePrivateFactoryIfReferencedMultipleTimesW
$container
->register('b')
->setPublic(false)
->setFactoryService('a')
->setFactory(array(new Reference('a'), 'a'))
;

$container
Expand All @@ -168,12 +168,12 @@ public function testProcessDoesNotInlineReferenceWhenUsedByInlineFactory()
$container
->register('b')
->setPublic(false)
->setFactoryService('a')
->setFactory(array(new Reference('a'), 'a'))
;

$inlineFactory = new Definition();
$inlineFactory->setPublic(false);
$inlineFactory->setFactoryService('b');
$inlineFactory->setFactory(array(new Reference('b'), 'b'));

$container
->register('foo')
Expand Down
Expand Up @@ -86,14 +86,12 @@ public function testProcessWontRemovePrivateFactory()

$container
->register('foo', 'stdClass')
->setFactoryClass('stdClass')
->setFactoryMethod('getInstance')
->setFactory(array('stdClass', 'getInstance'))
->setPublic(false);

$container
->register('bar', 'stdClass')
->setFactoryService('foo')
->setFactoryMethod('getInstance')
->setFactory(array(new Reference('foo'), 'getInstance'))
->setPublic(false);

$container
Expand Down

0 comments on commit 2856cae

Please sign in to comment.