Skip to content

Commit 1374c03

Browse files
[DependencyInjection] Add constructor option to services declaration and to #[Autoconfigure]
1 parent da8e8d0 commit 1374c03

40 files changed

+368
-6
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 services declaration and to `#[Autoconfigure]`
2122

2223
6.2
2324
---

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class InlineServiceConfigurator extends AbstractConfigurator
2323
use Traits\BindTrait;
2424
use Traits\CallTrait;
2525
use Traits\ConfiguratorTrait;
26+
use Traits\ConstructorTrait;
2627
use Traits\FactoryTrait;
2728
use Traits\FileTrait;
2829
use Traits\LazyTrait;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class InstanceofConfigurator extends AbstractServiceConfigurator
2222
use Traits\BindTrait;
2323
use Traits\CallTrait;
2424
use Traits\ConfiguratorTrait;
25+
use Traits\ConstructorTrait;
2526
use Traits\LazyTrait;
2627
use Traits\PropertyTrait;
2728
use Traits\PublicTrait;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class PrototypeConfigurator extends AbstractServiceConfigurator
2626
use Traits\BindTrait;
2727
use Traits\CallTrait;
2828
use Traits\ConfiguratorTrait;
29+
use Traits\ConstructorTrait;
2930
use Traits\DeprecateTrait;
3031
use Traits\FactoryTrait;
3132
use Traits\LazyTrait;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class ServiceConfigurator extends AbstractServiceConfigurator
2727
use Traits\CallTrait;
2828
use Traits\ClassTrait;
2929
use Traits\ConfiguratorTrait;
30+
use Traits\ConstructorTrait;
3031
use Traits\DecorateTrait;
3132
use Traits\DeprecateTrait;
3233
use Traits\FactoryTrait;
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 ConstructorTrait
15+
{
16+
/**
17+
* Sets a static constructor.
18+
*
19+
* @return $this
20+
*/
21+
final public function constructor(string $constructor): static
22+
{
23+
$this->definition->setFactory([null, $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 = $service->getAttribute('constructor')) {
319+
if (null !== $definition->getFactory()) {
320+
throw new LogicException(sprintf('The "%s" service cannot declare a factory as well as a constructor.', $service->getAttribute('id')));
321+
}
322+
323+
$definition->setFactory([null, $constructor]);
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.', $id));
511+
}
512+
513+
$definition->setFactory([null, $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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
<xsd:attribute name="decoration-priority" type="xsd:integer" />
169169
<xsd:attribute name="autowire" type="boolean" />
170170
<xsd:attribute name="autoconfigure" type="boolean" />
171+
<xsd:attribute name="constructor" type="xsd:string" />
171172
</xsd:complexType>
172173

173174
<xsd:complexType name="instanceof">
@@ -184,6 +185,7 @@
184185
<xsd:attribute name="lazy" type="xsd:string" />
185186
<xsd:attribute name="autowire" type="boolean" />
186187
<xsd:attribute name="autoconfigure" type="boolean" />
188+
<xsd:attribute name="constructor" type="boolean" />
187189
</xsd:complexType>
188190

189191
<xsd:complexType name="prototype">
@@ -208,6 +210,7 @@
208210
<xsd:attribute name="parent" type="xsd:string" />
209211
<xsd:attribute name="autowire" type="boolean" />
210212
<xsd:attribute name="autoconfigure" type="boolean" />
213+
<xsd:attribute name="constructor" type="boolean" />
211214
</xsd:complexType>
212215

213216
<xsd:complexType name="stack">

0 commit comments

Comments
 (0)