Skip to content

Commit ee70b26

Browse files
[DependencyInjection] Add constructor option to #[Autoconfigure]
1 parent cb39a7f commit ee70b26

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+555
-10
lines changed

src/Symfony/Component/DependencyInjection/Attribute/Autoconfigure.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function __construct(
2929
public ?bool $autowire = null,
3030
public ?array $properties = null,
3131
public array|string|null $configurator = null,
32+
public string|null $constructor = null,
3233
) {
3334
}
3435
}

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ CHANGELOG
1818
* Add support for autowiring services as closures using `#[AutowireCallable]` or `#[AutowireServiceClosure]`
1919
* Deprecate `#[MapDecorated]`, use `#[AutowireDecorated]` instead
2020
* Deprecate the `@required` annotation, use the `Symfony\Contracts\Service\Attribute\Required` attribute instead
21+
* Add `constructor` option to `#[Autoconfigure]`
2122

2223
6.2
2324
---

src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function __construct()
5959
new RegisterServiceSubscribersPass(),
6060
new ResolveParameterPlaceHoldersPass(false, false),
6161
new ResolveFactoryClassPass(),
62+
new ResolveStaticConstructorPass(),
6263
new ResolveNamedArgumentsPass(),
6364
new AutowireRequiredMethodsPass(),
6465
new AutowireRequiredPropertiesPass(),

src/Symfony/Component/DependencyInjection/Compiler/ResolveChildDefinitionsPass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ private function doResolveDefinition(ChildDefinition $definition): Definition
105105
$def->setDeprecated($deprecation['package'], $deprecation['version'], $deprecation['message']);
106106
}
107107
$def->setFactory($parentDef->getFactory());
108+
$def->setConstructor($parentDef->getConstructor());
108109
$def->setConfigurator($parentDef->getConfigurator());
109110
$def->setFile($parentDef->getFile());
110111
$def->setPublic($parentDef->isPublic());
@@ -124,6 +125,9 @@ private function doResolveDefinition(ChildDefinition $definition): Definition
124125
if (isset($changes['factory'])) {
125126
$def->setFactory($definition->getFactory());
126127
}
128+
if (isset($changes['constructor'])) {
129+
$def->setConstructor($definition->getConstructor());
130+
}
127131
if (isset($changes['configurator'])) {
128132
$def->setConfigurator($definition->getConfigurator());
129133
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Definition;
15+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
16+
17+
/**
18+
* @author Alexandre Daubois <alex.daubois@gmail.com>
19+
*/
20+
class ResolveStaticConstructorPass extends AbstractRecursivePass
21+
{
22+
protected function processValue(mixed $value, bool $isRoot = false): mixed
23+
{
24+
if ($value instanceof Definition && null !== $constructor = $value->getConstructor()) {
25+
if (null !== $value->getFactory()) {
26+
throw new RuntimeException(sprintf('The "%s" service cannot declare a factory as well as a constructor method.', $this->currentId));
27+
}
28+
29+
try {
30+
$r = new \ReflectionMethod($value->getClass(), $constructor);
31+
} catch (\ReflectionException) {
32+
throw new RuntimeException(sprintf('The "%s" service does not define a method named "%s".', $this->currentId, $constructor));
33+
}
34+
35+
if (!$r->isStatic() || !$r->isPublic()) {
36+
throw new RuntimeException(sprintf('To be used as a constructor, the "%s" method of the "%s" service must be defined as public and static.', $constructor, $this->currentId));
37+
}
38+
39+
$value->setFactory([$value->getClass(), $constructor]);
40+
}
41+
42+
return parent::processValue($value, $isRoot);
43+
}
44+
}

src/Symfony/Component/DependencyInjection/Definition.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Definition
2727
private ?string $class = null;
2828
private ?string $file = null;
2929
private string|array|null $factory = null;
30+
private string|null $constructor = null;
3031
private bool $shared = true;
3132
private array $deprecation = [];
3233
private array $properties = [];
@@ -123,6 +124,32 @@ public function getFactory(): string|array|null
123124
return $this->factory;
124125
}
125126

127+
/**
128+
* Sets a constructor.
129+
*
130+
* @param string|null $constructor the name of the public static function of the service to call to create itself
131+
*
132+
* @return $this
133+
*/
134+
public function setConstructor(string|null $constructor): static
135+
{
136+
$this->changes['constructor'] = true;
137+
138+
$this->constructor = $constructor;
139+
140+
return $this;
141+
}
142+
143+
/**
144+
* Gets the constructor.
145+
*
146+
* @return string|null the name of the public static function of the service to call to create itself
147+
*/
148+
public function getConstructor(): ?string
149+
{
150+
return $this->constructor;
151+
}
152+
126153
/**
127154
* Sets the service that this service is decorating.
128155
*

src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,12 @@ private function addService(Definition $definition, ?string $id, \DOMElement $pa
166166

167167
$this->addMethodCalls($definition->getMethodCalls(), $service);
168168

169-
if ($callable = $definition->getFactory()) {
169+
if ($constructorName = $definition->getConstructor()) {
170+
$constructor = $this->document->createElement('constructor');
171+
$constructor->setAttribute('method', $constructorName);
172+
173+
$service->appendChild($constructor);
174+
} elseif ($callable = $definition->getFactory()) {
170175
$factory = $this->document->createElement('factory');
171176

172177
if (\is_array($callable) && $callable[0] instanceof Definition) {

src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ private function addService(string $id, Definition $definition): string
150150
}
151151
}
152152

153-
if ($callable = $definition->getFactory()) {
153+
if ($constructor = $definition->getConstructor()) {
154+
$code .= sprintf(" constructor: %s\n", $this->dumper->dump($constructor, 0));
155+
} elseif ($callable = $definition->getFactory()) {
154156
$code .= sprintf(" factory: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0));
155157
}
156158

src/Symfony/Component/DependencyInjection/Loader/Configurator/InlineServiceConfigurator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class InlineServiceConfigurator extends AbstractConfigurator
2828
use Traits\LazyTrait;
2929
use Traits\ParentTrait;
3030
use Traits\PropertyTrait;
31+
use Traits\StaticConstructorTrait;
3132
use Traits\TagTrait;
3233

3334
public const FACTORY = 'service';

src/Symfony/Component/DependencyInjection/Loader/Configurator/PrototypeConfigurator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class PrototypeConfigurator extends AbstractServiceConfigurator
3333
use Traits\PropertyTrait;
3434
use Traits\PublicTrait;
3535
use Traits\ShareTrait;
36+
use Traits\StaticConstructorTrait;
3637
use Traits\TagTrait;
3738

3839
public const FACTORY = 'load';

0 commit comments

Comments
 (0)