Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 910b87d

Browse files
committed
[DependencyInjection] Invokable Factory Services
1 parent 98693be commit 910b87d

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/Symfony/Component/DependencyInjection/Definition.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function setChanges(array $changes)
9595
/**
9696
* Sets a factory.
9797
*
98-
* @param string|array $factory A PHP function or an array containing a class/Reference and a method to call
98+
* @param string|array|Reference $factory A PHP function or an array containing a class/Reference and a method to call
9999
*
100100
* @return $this
101101
*/
@@ -107,6 +107,10 @@ public function setFactory($factory)
107107
$factory = explode('::', $factory, 2);
108108
}
109109

110+
if ($factory instanceof Reference) {
111+
$factory = [$factory, '__invoke'];
112+
}
113+
110114
$this->factory = $factory;
111115

112116
return $this;
@@ -782,7 +786,7 @@ public function getDeprecationMessage($id)
782786
/**
783787
* Sets a configurator to call after the service is fully initialized.
784788
*
785-
* @param string|array $configurator A PHP callable
789+
* @param string|array|Reference $configurator A PHP function or an array containing a class/Reference and a method to call
786790
*
787791
* @return $this
788792
*/
@@ -794,6 +798,10 @@ public function setConfigurator($configurator)
794798
$configurator = explode('::', $configurator, 2);
795799
}
796800

801+
if ($configurator instanceof Reference) {
802+
$configurator = [$configurator, '__invoke'];
803+
}
804+
797805
$this->configurator = $configurator;
798806

799807
return $this;

src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ private function parseDefinition(\DOMElement $service, $file, array $defaults)
317317
$class = $factory->hasAttribute('class') ? $factory->getAttribute('class') : null;
318318
}
319319

320-
$definition->setFactory([$class, $factory->getAttribute('method')]);
320+
$definition->setFactory([$class, $factory->hasAttribute('method') ? $factory->getAttribute('method') : '__invoke']);
321321
}
322322
}
323323

@@ -332,7 +332,7 @@ private function parseDefinition(\DOMElement $service, $file, array $defaults)
332332
$class = $configurator->getAttribute('class');
333333
}
334334

335-
$definition->setConfigurator([$class, $configurator->getAttribute('method')]);
335+
$definition->setConfigurator([$class, $configurator->hasAttribute('method') ? $configurator->getAttribute('method') : '__invoke']);
336336
}
337337
}
338338

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,12 +571,15 @@ private function parseDefinition($id, $service, $file, array $defaults)
571571
*
572572
* @throws InvalidArgumentException When errors occur
573573
*
574-
* @return string|array A parsed callable
574+
* @return string|array|Reference A parsed callable
575575
*/
576576
private function parseCallable($callable, $parameter, $id, $file)
577577
{
578578
if (\is_string($callable)) {
579579
if ('' !== $callable && '@' === $callable[0]) {
580+
if (false === strpos($callable, ':')) {
581+
return [$this->resolveServices($callable, $file), '__invoke'];
582+
}
580583
throw new InvalidArgumentException(sprintf('The value of the "%s" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s").', $parameter, $id, $callable, substr($callable, 1)));
581584
}
582585

0 commit comments

Comments
 (0)