Skip to content

[2.7] silence deprecations for getFactory*() BC layer #13292

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 7, 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
Original file line number Diff line number Diff line change
Expand Up @@ -329,16 +329,18 @@ private function getContainerDefinitionDocument(Definition $definition, $id = nu

$serviceXML->setAttribute('class', $definition->getClass());

if ($definition->getFactoryClass()) {
$serviceXML->setAttribute('factory-class', $definition->getFactoryClass());
}
if (method_exists($definition, 'getFactoryMethod')) {
Copy link
Member

Choose a reason for hiding this comment

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

why a methodExists check ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Forward compatibility layer with 3.0, when we'll remove getFactoryMethod there

if ($definition->getFactoryClass(false)) {
$serviceXML->setAttribute('factory-class', $definition->getFactoryClass(false));
}

if ($definition->getFactoryService()) {
$serviceXML->setAttribute('factory-service', $definition->getFactoryService());
}
if ($definition->getFactoryService(false)) {
$serviceXML->setAttribute('factory-service', $definition->getFactoryService(false));
}

if ($definition->getFactoryMethod()) {
$serviceXML->setAttribute('factory-method', $definition->getFactoryMethod());
if ($definition->getFactoryMethod(false)) {
$serviceXML->setAttribute('factory-method', $definition->getFactoryMethod(false));
}
}

if ($factory = $definition->getFactory()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public function process(ContainerBuilder $container)
$this->currentDefinition = $definition;

$this->processArguments($definition->getArguments());
if ($definition->getFactoryService()) {
$this->processArguments(array(new Reference($definition->getFactoryService())));
if ($definition->getFactoryService(false)) {
$this->processArguments(array(new Reference($definition->getFactoryService(false))));
}
if (is_array($definition->getFactory())) {
$this->processArguments($definition->getFactory());
Expand Down Expand Up @@ -118,8 +118,8 @@ private function processArguments(array $arguments)
if (is_array($argument->getFactory())) {
$this->processArguments($argument->getFactory());
}
if ($argument->getFactoryService()) {
$this->processArguments(array(new Reference($argument->getFactoryService())));
if ($argument->getFactoryService(false)) {
$this->processArguments(array(new Reference($argument->getFactoryService(false))));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public function process(ContainerBuilder $container)
throw new RuntimeException(sprintf('A synthetic service ("%s") cannot be of scope "prototype".', $id));
}

if ($definition->getFactory() && ($definition->getFactoryClass() || $definition->getFactoryService() || $definition->getFactoryMethod())) {
if ($definition->getFactory() && ($definition->getFactoryClass(false) || $definition->getFactoryService(false) || $definition->getFactoryMethod(false))) {
throw new RuntimeException(sprintf('A service ("%s") can use either the old or the new factory syntax, not both.', $id));
}

// non-synthetic, non-abstract service has class
if (!$definition->isAbstract() && !$definition->isSynthetic() && !$definition->getClass()) {
if ($definition->getFactory() || $definition->getFactoryClass() || $definition->getFactoryService()) {
if ($definition->getFactory() || $definition->getFactoryClass(false) || $definition->getFactoryService(false)) {
throw new RuntimeException(sprintf('Please add the class to service "%s" even if it is constructed by a factory since we might need to add method calls based on compile-time checks.', $id));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private function isInlineableDefinition(ContainerBuilder $container, $id, Defini
return false;
}

if (count($ids) > 1 && $definition->getFactoryService()) {
if (count($ids) > 1 && $definition->getFactoryService(false)) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,15 @@ private function resolveDefinition($id, DefinitionDecorator $definition)
$def->setArguments($parentDef->getArguments());
$def->setMethodCalls($parentDef->getMethodCalls());
$def->setProperties($parentDef->getProperties());
$def->setFactoryClass($parentDef->getFactoryClass());
$def->setFactoryMethod($parentDef->getFactoryMethod());
$def->setFactoryService($parentDef->getFactoryService());
if ($parentDef->getFactoryClass(false)) {
$def->setFactoryClass($parentDef->getFactoryClass(false));
}
if ($parentDef->getFactoryMethod(false)) {
$def->setFactoryMethod($parentDef->getFactoryMethod(false));
}
if ($parentDef->getFactoryService(false)) {
$def->setFactoryService($parentDef->getFactoryService(false));
}
$def->setFactory($parentDef->getFactory());
$def->setConfigurator($parentDef->getConfigurator());
$def->setFile($parentDef->getFile());
Expand All @@ -96,13 +102,13 @@ private function resolveDefinition($id, DefinitionDecorator $definition)
$def->setClass($definition->getClass());
}
if (isset($changes['factory_class'])) {
$def->setFactoryClass($definition->getFactoryClass());
$def->setFactoryClass($definition->getFactoryClass(false));
}
if (isset($changes['factory_method'])) {
$def->setFactoryMethod($definition->getFactoryMethod());
$def->setFactoryMethod($definition->getFactoryMethod(false));
}
if (isset($changes['factory_service'])) {
$def->setFactoryService($definition->getFactoryService());
$def->setFactoryService($definition->getFactoryService(false));
}
if (isset($changes['factory'])) {
$def->setFactory($definition->getFactory());
Expand Down
12 changes: 6 additions & 6 deletions src/Symfony/Component/DependencyInjection/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -948,16 +948,16 @@ public function createService(Definition $definition, $id, $tryProxy = true)
}

$service = call_user_func_array($factory, $arguments);
} elseif (null !== $definition->getFactoryMethod()) {
if (null !== $definition->getFactoryClass()) {
$factory = $parameterBag->resolveValue($definition->getFactoryClass());
} elseif (null !== $definition->getFactoryService()) {
$factory = $this->get($parameterBag->resolveValue($definition->getFactoryService()));
} elseif (null !== $definition->getFactoryMethod(false)) {
if (null !== $definition->getFactoryClass(false)) {
$factory = $parameterBag->resolveValue($definition->getFactoryClass(false));
} elseif (null !== $definition->getFactoryService(false)) {
$factory = $this->get($parameterBag->resolveValue($definition->getFactoryService(false)));
} else {
throw new RuntimeException(sprintf('Cannot create service "%s" from factory method without a factory service or factory class.', $id));
}

$service = call_user_func_array(array($factory, $definition->getFactoryMethod()), $arguments);
$service = call_user_func_array(array($factory, $definition->getFactoryMethod(false)), $arguments);
} else {
$r = new \ReflectionClass($parameterBag->resolveValue($definition->getClass()));

Expand Down
18 changes: 12 additions & 6 deletions src/Symfony/Component/DependencyInjection/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ public function setFactoryClass($factoryClass)
* @api
* @deprecated since version 2.6, to be removed in 3.0.
*/
public function getFactoryClass()
public function getFactoryClass($triggerDeprecationError = true)
{
trigger_error('The '.__METHOD__.' is deprecated since version 2.6 and will be removed in 3.0.', E_USER_DEPRECATED);
if ($triggerDeprecationError) {
trigger_error('The '.__METHOD__.' is deprecated since version 2.6 and will be removed in 3.0.', E_USER_DEPRECATED);
}

return $this->factoryClass;
}
Expand Down Expand Up @@ -182,9 +184,11 @@ public function getDecoratedService()
* @api
* @deprecated since version 2.6, to be removed in 3.0.
*/
public function getFactoryMethod()
public function getFactoryMethod($triggerDeprecationError = true)
{
trigger_error('The '.__METHOD__.' is deprecated since version 2.6 and will be removed in 3.0.', E_USER_DEPRECATED);
if ($triggerDeprecationError) {
trigger_error('The '.__METHOD__.' is deprecated since version 2.6 and will be removed in 3.0.', E_USER_DEPRECATED);
}

return $this->factoryMethod;
}
Expand Down Expand Up @@ -216,9 +220,11 @@ public function setFactoryService($factoryService)
* @api
* @deprecated since version 2.6, to be removed in 3.0.
*/
public function getFactoryService()
public function getFactoryService($triggerDeprecationError = true)
{
trigger_error('The '.__METHOD__.' is deprecated since version 2.6 and will be removed in 3.0.', E_USER_DEPRECATED);
if ($triggerDeprecationError) {
trigger_error('The '.__METHOD__.' is deprecated since version 2.6 and will be removed in 3.0.', E_USER_DEPRECATED);
}

return $this->factoryService;
}
Expand Down
32 changes: 16 additions & 16 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,10 +572,10 @@ private function addService($id, $definition)
$return[] = sprintf('@return object An instance returned by %s::%s().', $factory[0]->getClass(), $factory[1]);
}
}
} elseif ($definition->getFactoryClass()) {
$return[] = sprintf('@return object An instance returned by %s::%s().', $definition->getFactoryClass(), $definition->getFactoryMethod());
} elseif ($definition->getFactoryService()) {
$return[] = sprintf('@return object An instance returned by %s::%s().', $definition->getFactoryService(), $definition->getFactoryMethod());
} elseif ($definition->getFactoryClass(false)) {
$return[] = sprintf('@return object An instance returned by %s::%s().', $definition->getFactoryClass(false), $definition->getFactoryMethod(false));
} elseif ($definition->getFactoryService(false)) {
$return[] = sprintf('@return object An instance returned by %s::%s().', $definition->getFactoryService(false), $definition->getFactoryMethod(false));
}

$scope = $definition->getScope();
Expand Down Expand Up @@ -768,20 +768,20 @@ private function addNewInstance($id, Definition $definition, $return, $instantia
}

return sprintf(" $return{$instantiation}\\%s(%s);\n", $callable, $arguments ? implode(', ', $arguments) : '');
} elseif (null !== $definition->getFactoryMethod()) {
if (null !== $definition->getFactoryClass()) {
$class = $this->dumpValue($definition->getFactoryClass());
} elseif (null !== $definition->getFactoryMethod(false)) {
if (null !== $definition->getFactoryClass(false)) {
$class = $this->dumpValue($definition->getFactoryClass(false));

// If the class is a string we can optimize call_user_func away
if (strpos($class, "'") === 0) {
return sprintf(" $return{$instantiation}%s::%s(%s);\n", $this->dumpLiteralClass($class), $definition->getFactoryMethod(), $arguments ? implode(', ', $arguments) : '');
return sprintf(" $return{$instantiation}%s::%s(%s);\n", $this->dumpLiteralClass($class), $definition->getFactoryMethod(false), $arguments ? implode(', ', $arguments) : '');
}

return sprintf(" $return{$instantiation}call_user_func(array(%s, '%s')%s);\n", $this->dumpValue($definition->getFactoryClass()), $definition->getFactoryMethod(), $arguments ? ', '.implode(', ', $arguments) : '');
return sprintf(" $return{$instantiation}call_user_func(array(%s, '%s')%s);\n", $this->dumpValue($definition->getFactoryClass(false)), $definition->getFactoryMethod(false), $arguments ? ', '.implode(', ', $arguments) : '');
}

if (null !== $definition->getFactoryService()) {
return sprintf(" $return{$instantiation}%s->%s(%s);\n", $this->getServiceCall($definition->getFactoryService()), $definition->getFactoryMethod(), implode(', ', $arguments));
if (null !== $definition->getFactoryService(false)) {
return sprintf(" $return{$instantiation}%s->%s(%s);\n", $this->getServiceCall($definition->getFactoryService(false)), $definition->getFactoryMethod(false), implode(', ', $arguments));
}

throw new RuntimeException(sprintf('Factory method requires a factory service or factory class in service definition for %s', $id));
Expand Down Expand Up @@ -1328,11 +1328,11 @@ private function dumpValue($value, $interpolate = true)
throw new RuntimeException('Cannot dump definition because of invalid factory');
}

if (null !== $value->getFactoryMethod()) {
if (null !== $value->getFactoryClass()) {
return sprintf("call_user_func(array(%s, '%s')%s)", $this->dumpValue($value->getFactoryClass()), $value->getFactoryMethod(), count($arguments) > 0 ? ', '.implode(', ', $arguments) : '');
} elseif (null !== $value->getFactoryService()) {
return sprintf("%s->%s(%s)", $this->getServiceCall($value->getFactoryService()), $value->getFactoryMethod(), implode(', ', $arguments));
if (null !== $value->getFactoryMethod(false)) {
if (null !== $value->getFactoryClass(false)) {
return sprintf("call_user_func(array(%s, '%s')%s)", $this->dumpValue($value->getFactoryClass(false)), $value->getFactoryMethod(false), count($arguments) > 0 ? ', '.implode(', ', $arguments) : '');
} elseif (null !== $value->getFactoryService(false)) {
return sprintf("%s->%s(%s)", $this->getServiceCall($value->getFactoryService(false)), $value->getFactoryMethod(false), implode(', ', $arguments));
} else {
throw new RuntimeException('Cannot dump definitions which have factory method without factory service or factory class.');
}
Expand Down
12 changes: 6 additions & 6 deletions src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ private function addService($definition, $id, \DOMElement $parent)
if ($definition->getClass()) {
$service->setAttribute('class', $definition->getClass());
}
if ($definition->getFactoryMethod()) {
$service->setAttribute('factory-method', $definition->getFactoryMethod());
if ($definition->getFactoryMethod(false)) {
$service->setAttribute('factory-method', $definition->getFactoryMethod(false));
}
if ($definition->getFactoryClass()) {
$service->setAttribute('factory-class', $definition->getFactoryClass());
if ($definition->getFactoryClass(false)) {
$service->setAttribute('factory-class', $definition->getFactoryClass(false));
}
if ($definition->getFactoryService()) {
$service->setAttribute('factory-service', $definition->getFactoryService());
if ($definition->getFactoryService(false)) {
$service->setAttribute('factory-service', $definition->getFactoryService(false));
}
if (ContainerInterface::SCOPE_CONTAINER !== $scope = $definition->getScope()) {
$service->setAttribute('scope', $scope);
Expand Down
12 changes: 6 additions & 6 deletions src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,20 @@ private function addService($id, $definition)
$code .= sprintf(" synchronized: true\n");
}

if ($definition->getFactoryClass()) {
$code .= sprintf(" factory_class: %s\n", $definition->getFactoryClass());
if ($definition->getFactoryClass(false)) {
$code .= sprintf(" factory_class: %s\n", $definition->getFactoryClass(false));
}

if ($definition->isLazy()) {
$code .= sprintf(" lazy: true\n");
}

if ($definition->getFactoryMethod()) {
$code .= sprintf(" factory_method: %s\n", $definition->getFactoryMethod());
if ($definition->getFactoryMethod(false)) {
$code .= sprintf(" factory_method: %s\n", $definition->getFactoryMethod(false));
}

if ($definition->getFactoryService()) {
$code .= sprintf(" factory_service: %s\n", $definition->getFactoryService());
if ($definition->getFactoryService(false)) {
$code .= sprintf(" factory_service: %s\n", $definition->getFactoryService(false));
}

if ($definition->getArguments()) {
Expand Down