Skip to content

Commit 2a0d524

Browse files
[DependencyInjection] Add constructor option to services declaration and to #[Autoconfigure]
1 parent a75fc8c commit 2a0d524

Some content is hidden

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

44 files changed

+395
-29
lines changed

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
}

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ CHANGELOG
2121
* Make it possible to cast callables into single-method interfaces
2222
* Deprecate `#[MapDecorated]`, use `#[AutowireDecorated]` instead
2323
* Deprecate the `@required` annotation, use the `Symfony\Contracts\Service\Attribute\Required` attribute instead
24+
* Add `constructor` option to services declaration and to `#[Autoconfigure]`
2425

2526
6.2
2627
---

Dumper/XmlDumper.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -167,20 +167,24 @@ private function addService(Definition $definition, ?string $id, \DOMElement $pa
167167
$this->addMethodCalls($definition->getMethodCalls(), $service);
168168

169169
if ($callable = $definition->getFactory()) {
170-
$factory = $this->document->createElement('factory');
171-
172-
if (\is_array($callable) && $callable[0] instanceof Definition) {
173-
$this->addService($callable[0], null, $factory);
174-
$factory->setAttribute('method', $callable[1]);
175-
} elseif (\is_array($callable)) {
176-
if (null !== $callable[0]) {
177-
$factory->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
178-
}
179-
$factory->setAttribute('method', $callable[1]);
170+
if (\is_array($callable) && ['Closure', 'fromCallable'] !== $callable && $definition->getClass() === $callable[0]) {
171+
$service->setAttribute('constructor', $callable[1]);
180172
} else {
181-
$factory->setAttribute('function', $callable);
173+
$factory = $this->document->createElement('factory');
174+
175+
if (\is_array($callable) && $callable[0] instanceof Definition) {
176+
$this->addService($callable[0], null, $factory);
177+
$factory->setAttribute('method', $callable[1]);
178+
} elseif (\is_array($callable)) {
179+
if (null !== $callable[0]) {
180+
$factory->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
181+
}
182+
$factory->setAttribute('method', $callable[1]);
183+
} else {
184+
$factory->setAttribute('function', $callable);
185+
}
186+
$service->appendChild($factory);
182187
}
183-
$service->appendChild($factory);
184188
}
185189

186190
if ($definition->isDeprecated()) {

Dumper/YamlDumper.php

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

153153
if ($callable = $definition->getFactory()) {
154-
$code .= sprintf(" factory: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0));
154+
if (\is_array($callable) && ['Closure', 'fromCallable'] !== $callable && $definition->getClass() === $callable[0]) {
155+
$code .= sprintf(" constructor: %s\n", $callable[1]);
156+
} else {
157+
$code .= sprintf(" factory: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0));
158+
}
155159
}
156160

157161
if ($callable = $definition->getConfigurator()) {

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;

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;

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;

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 <[email protected]>
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+
}

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')) {

0 commit comments

Comments
 (0)