Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
[DependencyInjection] Fix service() as invokable factory in array-b…
…ased PHP config

`ContainerConfigurator::processValue()` turns `ReferenceConfigurator`
instances into `Reference` objects when forwarding array-based PHP
config to `YamlFileLoader::parseCallable()`, which only accepted
strings and arrays as input and threw on a bare `Reference`. Since
`Reference` is already part of `parseCallable()`'s return type and
mirrors the post-processed form of `'@id'` strings, accept it as input
and treat it as an invokable, like the `'@id'` branch does.
  • Loading branch information
nicolas-grekas committed May 20, 2026
commit f959ccdb426f8890ef45a079c6c8a85e5e419cae
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,10 @@ private function parseDefinition(string $id, array|string|null $service, string
*/
private function parseCallable(mixed $callable, string $parameter, string $id, string $file): string|array|Reference
{
if ($callable instanceof Reference) {
return [$callable, '__invoke'];
}

if (\is_string($callable)) {
if (str_starts_with($callable, '@=')) {
if ('factory' !== $parameter) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\DependencyInjection\Tests\Fixtures\Bar;
use Symfony\Component\DependencyInjection\Tests\Fixtures\BarFactory;

return App::config([
'services' => [
'_defaults' => [
'public' => true,
],
BarFactory::class => [
'arguments' => [[]],
],
'invokable_factory' => [
'class' => Bar::class,
'factory' => service(BarFactory::class),
],
'array_factory' => [
'class' => Bar::class,
'factory' => [service(BarFactory::class), 'getDefaultBar'],
],
'invokable_configurator' => [
'class' => Bar::class,
'configurator' => service(BarFactory::class),
],
],
]);
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,20 @@ public function testAutoConfigureAndChildDefinition()
$this->assertTrue($container->getDefinition('child_service')->isAutoconfigured());
}

public function testArrayConfigInvokableFactoryAndConfigurator()
{
$fixtures = realpath(__DIR__.'/../Fixtures');
$container = new ContainerBuilder();
$loader = new PhpFileLoader($container, new FileLocator());
$loader->load($fixtures.'/config/array_config_factory.php');

$barFactoryRef = new Reference('Symfony\Component\DependencyInjection\Tests\Fixtures\BarFactory');

$this->assertEquals([$barFactoryRef, '__invoke'], $container->getDefinition('invokable_factory')->getFactory());
$this->assertEquals([$barFactoryRef, 'getDefaultBar'], $container->getDefinition('array_factory')->getFactory());
$this->assertEquals([$barFactoryRef, '__invoke'], $container->getDefinition('invokable_configurator')->getConfigurator());
}

public function testFactoryShortNotationNotAllowed()
{
$this->expectException(InvalidArgumentException::class);
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.