Skip to content

Commit 4c92908

Browse files
[DependencyInjection] Add constructor option to #[Autoconfigure]
1 parent da8e8d0 commit 4c92908

31 files changed

+378
-0
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/Definition.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,28 @@ public function getFactory(): string|array|null
123123
return $this->factory;
124124
}
125125

126+
/**
127+
* Sets a constructor.
128+
*
129+
* @param string|null $constructor the name of the public static function of the service to call to create itself
130+
*
131+
* @return $this
132+
*/
133+
public function setConstructor(string|null $constructor): static
134+
{
135+
return $constructor ? $this->setFactory([null, $constructor]) : $this;
136+
}
137+
138+
/**
139+
* Gets the constructor.
140+
*
141+
* @return string|null the name of the public static function of the service to call to create itself
142+
*/
143+
public function getConstructor(): ?string
144+
{
145+
return \is_array($this->factory) ? $this->factory[1] : $this->factory;
146+
}
147+
126148
/**
127149
* Sets the service that this service is decorating.
128150
*

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';

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class ServiceConfigurator extends AbstractServiceConfigurator
3636
use Traits\PropertyTrait;
3737
use Traits\PublicTrait;
3838
use Traits\ShareTrait;
39+
use Traits\StaticConstructorTrait;
3940
use Traits\SyntheticTrait;
4041
use Traits\TagTrait;
4142

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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\Loader\Configurator\Traits;
13+
14+
trait StaticConstructorTrait
15+
{
16+
/**
17+
* Sets a static constructor.
18+
*
19+
* @return $this
20+
*/
21+
final public function constructor(string $constructor): static
22+
{
23+
$this->definition->setConstructor($constructor);
24+
25+
return $this;
26+
}
27+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Symfony\Component\DependencyInjection\ContainerInterface;
2525
use Symfony\Component\DependencyInjection\Definition;
2626
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
27+
use Symfony\Component\DependencyInjection\Exception\LogicException;
2728
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
2829
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
2930
use Symfony\Component\DependencyInjection\Reference;
@@ -314,6 +315,14 @@ private function parseDefinition(\DOMElement $service, string $file, Definition
314315
}
315316
}
316317

318+
if ($constructor = $this->getChildren($service, 'constructor')) {
319+
if (null !== $definition->getFactory()) {
320+
throw new LogicException(sprintf('The "%s" service cannot declare a factory as well as a constructor method.', $service->getAttribute('id')));
321+
}
322+
323+
$definition->setConstructor($constructor[0]->getAttribute('method'));
324+
}
325+
317326
if ($configurators = $this->getChildren($service, 'configurator')) {
318327
$configurator = $configurators[0];
319328
if ($function = $configurator->getAttribute('function')) {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\DependencyInjection\ContainerInterface;
2424
use Symfony\Component\DependencyInjection\Definition;
2525
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
26+
use Symfony\Component\DependencyInjection\Exception\LogicException;
2627
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
2728
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
2829
use Symfony\Component\DependencyInjection\Reference;
@@ -63,6 +64,7 @@ class YamlFileLoader extends FileLoader
6364
'autowire' => 'autowire',
6465
'autoconfigure' => 'autoconfigure',
6566
'bind' => 'bind',
67+
'constructor' => 'constructor',
6668
];
6769

6870
private const PROTOTYPE_KEYWORDS = [
@@ -84,6 +86,7 @@ class YamlFileLoader extends FileLoader
8486
'autowire' => 'autowire',
8587
'autoconfigure' => 'autoconfigure',
8688
'bind' => 'bind',
89+
'constructor' => 'constructor',
8790
];
8891

8992
private const INSTANCEOF_KEYWORDS = [
@@ -96,6 +99,7 @@ class YamlFileLoader extends FileLoader
9699
'tags' => 'tags',
97100
'autowire' => 'autowire',
98101
'bind' => 'bind',
102+
'constructor' => 'constructor',
99103
];
100104

101105
private const DEFAULTS_KEYWORDS = [
@@ -501,6 +505,14 @@ private function parseDefinition(string $id, array|string|null $service, string
501505
$definition->setFactory($this->parseCallable($service['factory'], 'factory', $id, $file));
502506
}
503507

508+
if (isset($service['constructor'])) {
509+
if (null !== $definition->getFactory()) {
510+
throw new LogicException(sprintf('The "%s" service cannot declare a factory as well as a constructor method.', $id));
511+
}
512+
513+
$definition->setConstructor($service['constructor']);
514+
}
515+
504516
if (isset($service['file'])) {
505517
$definition->setFile($service['file']);
506518
}

src/Symfony/Component/DependencyInjection/Loader/schema/dic/services/services-1.0.xsd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@
126126
<xsd:attribute name="expression" type="xsd:string" />
127127
</xsd:complexType>
128128

129+
<xsd:complexType name="constructor">
130+
<xsd:attribute name="method" type="xsd:string" />
131+
</xsd:complexType>
132+
129133
<xsd:complexType name="defaults">
130134
<xsd:annotation>
131135
<xsd:documentation><![CDATA[
@@ -147,6 +151,7 @@
147151
<xsd:element name="argument" type="argument" minOccurs="0" maxOccurs="unbounded" />
148152
<xsd:element name="configurator" type="callable" minOccurs="0" maxOccurs="1" />
149153
<xsd:element name="factory" type="factory" minOccurs="0" maxOccurs="1" />
154+
<xsd:element name="constructor" type="constructor" minOccurs="0" maxOccurs="1" />
150155
<xsd:element name="deprecated" type="deprecated" minOccurs="0" maxOccurs="1" />
151156
<xsd:element name="call" type="call" minOccurs="0" maxOccurs="unbounded" />
152157
<xsd:element name="tag" type="tag" minOccurs="0" maxOccurs="unbounded" />

0 commit comments

Comments
 (0)