8000 [DependencyInjection] Add a new Syntax to define factories as callables by fabpot · Pull Request #12008 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Add a new Syntax to define factories as callables #12008

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 2 commits into from
Sep 24, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fixed CS
  • Loading branch information
fabpot committed Sep 24, 2014
commit 187aeeeaf790f16ec0344acf7d432f781994e58d
6 changes: 4 additions & 2 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ UPGRADE FROM 2.x to 3.0

### DependencyInjection

* The methods `setFactoryClass()`, `setFactoryMethod()` and `setFactoryService()` have been removed in favor of `setFactory()`.
Services defined using YAML or XML use the same syntax as configurators.
* The methods `Definition::setFactoryClass()`,
`Definition::setFactoryMethod()`, and `Definition::setFactoryService()` have
been removed in favor of `Definition::setFactory()`. Services defined using
YAML or XML use the same syntax as configurators.

### EventDispatcher

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,22 @@ public function process(ContainerBuilder $container)
foreach ($container->getDefinitions() as $id => $definition) {
// synthetic service is public
if ($definition->isSynthetic() && !$definition->isPublic()) {
throw new RuntimeException(sprintf(
'A synthetic service ("%s") must be public.',
$id
));
throw new RuntimeException(sprintf('A synthetic service ("%s") must be public.', $id));
}

// synthetic service has non-prototype scope
if ($definition->isSynthetic() && ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope()) {
throw new RuntimeException(sprintf(
'A synthetic service ("%s") cannot be of scope "prototype".',
$id
));
throw new RuntimeException(sprintf('A synthetic service ("%s") cannot be of scope "prototype".', $id));
}

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

throw new RuntimeException(sprintf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,10 @@ public function process(ContainerBuilder $container)
);

$configurator = $this->inlineArguments($container, array($definition->getConfigurator()));
$definition->setConfigurator(
$configurator[0]
);
$definition->setConfigurator($configurator[0]);

$factory = $this->inlineArguments($container, array($definition->getFactory()));
$definition->setFactory(
$factory[0]
);
$definition->setFactory($factory[0]);
}
}

Expand Down
24 changes: 10 additions & 14 deletions src/Symfony/Component/DependencyInjection/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,11 @@ public function __construct($class = null, array $arguments = array())
}

/**
* Sets a factory
* Sets a factory.
*
* @param callable $factory The PHP callable to call or an array containing a Reference and a method to call
* @param string|array $factory A PHP function or an array containing a class/Reference and a method to call
*
* @return Definition The current instance
*
* @api
*/
public function setFactory($factory)
{
Expand All @@ -74,11 +72,9 @@ public function setFactory($factory)
}

/**
* Gets the factory .
* Gets the factory.
*
* @return callable|array The PHP callable to call or an array containing a Reference and a method to call
*
* @api
* @return string|array The PHP function or an array containing a class/Reference and a method to call
*/
public function getFactory()
{
Expand All @@ -94,7 +90,7 @@ public function getFactory()
* @return Definition The current instance
*
* @api
* @deprecated Deprecated since version 2.5, to be removed in 3.0.
* @deprecated Deprecated since version 2.6, to be removed in 3.0.
*/
public function setFactoryClass($factoryClass)
{
Expand All @@ -109,7 +105,7 @@ public function setFactoryClass($factoryClass)
* @return string|null The factory class name
*
* @api
* @deprecated Deprecated since version 2.5, to be removed in 3.0.
* @deprecated Deprecated since version 2.6, to be removed in 3.0.
*/
public function getFactoryClass()
{
Expand All @@ -124,7 +120,7 @@ public function getFactoryClass()
* @return Definition The current instance
*
* @api
* @deprecated Deprecated since version 2.5, to be removed in 3.0.
* @deprecated Deprecated since version 2.6, to be removed in 3.0.
*/
public function setFactoryMethod($factoryMethod)
{
Expand Down Expand Up @@ -174,7 +170,7 @@ public function getDecoratedService()
* @return string|null The factory method name
*
* @api
* @deprecated Deprecated since version 2.5, to be removed in 3.0.
* @deprecated Deprecated since version 2.6, to be removed in 3.0.
*/
public function getFactoryMethod()
{
Expand All @@ -189,7 +185,7 @@ public function getFactoryMethod()
* @return Definition The current instance
*
* @api
* @deprecated Deprecated since version 2.5, to be removed in 3.0.
* @deprecated Deprecated since version 2.6, to be removed in 3.0.
*/
public function setFactoryService($factoryService)
{
Expand All @@ -204,7 +200,7 @@ public function setFactoryService($factoryService)
* @return string|null The factory service id
*
* @api
* @deprecated Deprecated since version 2.5, to be removed in 3.0.
* @deprecated Deprecated since version 2.6, to be removed in 3.0.
*/
public function getFactoryService()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ public function setClass($class)

/**
* {@inheritdoc}
*
* @api
*/
public function setFactory($callable)
{
Expand All @@ -89,7 +87,7 @@ public function setFactory($callable)
}

/**
* {@inheritDoc}
* {@inheritdoc}
*
* @api
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ private function addService($id, $definition)
if (is_string($factory)) {
$return[] = sprintf('@return object An instance returned by %s().', $factory);
} elseif (is_array($factory) && (is_string($factory[0]) || $factory[0] instanceof Definition || $factory[0] instanceof Reference)) {
if (is_string($factory[0] || $factory[0] instanceof Reference)) {
if (is_string($factory[0]) || $factory[0] instanceof Reference) {
$return[] = sprintf('@return object An instance returned by %s::%s().', (string) $factory[0], $factory[1]);
} elseif ($factory[0] instanceof Definition) {
$return[] = sprintf('@return object An instance returned by %s::%s().', $factory[0]->getClass(), $factory[1]);
Expand Down Expand Up @@ -731,7 +731,6 @@ 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private function parseDefinition($id, $service, $file)

if (isset($service['factory'])) {
if (is_string($service['factory'])) {
if (strpos($service['factory'], ':')) {
if (strpos($service['factory'], ':')) {
$parts = explode(':', $service['factory']);
$definition->setFactory(array($this->resolveServices('@'.$parts[0]), $parts[1]));
} else {
Expand Down
0