Skip to content

Commit cad7fbb

Browse files
committed
[DI] Autowire public typed properties
1 parent e0f6cdb commit cad7fbb

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.1.0
5+
-----
6+
7+
* added support to autowire public typed properties in php 7.4
8+
49
5.0.0
510
-----
611

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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\ContainerInterface;
15+
use Symfony\Component\DependencyInjection\Definition;
16+
use Symfony\Component\DependencyInjection\TypedReference;
17+
18+
/**
19+
* Looks for definitions with autowiring enabled and registers their corresponding "@required" properties.
20+
*
21+
* @author Sebastien Morel (Plopix) <morel.seb@gmail.com>
22+
* @author Nicolas Grekas <p@tchwork.com>
23+
*/
24+
class AutowireRequiredPropertiesPass extends AbstractRecursivePass
25+
{
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
protected function processValue($value, bool $isRoot = false)
30+
{
31+
if (\PHP_VERSION_ID < 70400) {
32+
return $value;
33+
}
34+
$value = parent::processValue($value, $isRoot);
35+
36+
if (!$value instanceof Definition || !$value->isAutowired() || $value->isAbstract() || !$value->getClass()) {
37+
return $value;
38+
}
39+
if (!$reflectionClass = $this->container->getReflectionClass($value->getClass(), false)) {
40+
return $value;
41+
}
42+
43+
$properties = $value->getProperties();
44+
foreach ($reflectionClass->getProperties() as $reflectionProperty) {
45+
if (false === $doc = $reflectionProperty->getDocComment()) {
46+
continue;
47+
}
48+
if (false === stripos($doc, '@required') || !preg_match('#(?:^/\*\*|\n\s*+\*)\s*+@required(?:\s|\*/$)#i', $doc)) {
49+
continue;
50+
}
51+
if (\array_key_exists($name = $reflectionProperty->getName(), $properties)) {
52+
continue;
53+
}
54+
55+
$type = $reflectionProperty->getType()->getName();
56+
$value->setProperty($name, new TypedReference($type, $type, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $name));
57+
}
58+
59+
return $value;
60+
}
61+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public function __construct()
5757
new ResolveFactoryClassPass(),
5858
new ResolveNamedArgumentsPass(),
5959
new AutowireRequiredMethodsPass(),
60+
new AutowireRequiredPropertiesPass(),
6061
new ResolveBindingsPass(),
6162
new ServiceLocatorTagPass(),
6263
new CheckDefinitionValidityPass(),
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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\Tests\Compiler;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredPropertiesPass;
16+
use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
17+
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
19+
require_once __DIR__.'/../Fixtures/includes/autowiring_classes.php';
20+
21+
if (\PHP_VERSION_ID >= 70400) {
22+
require_once __DIR__.'/../Fixtures/includes/autowiring_classes_74.php';
23+
}
24+
25+
/**
26+
* @requires PHP 7.4
27+
*/
28+
class AutowireRequiredPropertiesPassTest extends TestCase
29+
{
30+
public function testInjection()
31+
{
32+
$container = new ContainerBuilder();
33+
$container->register(Bar::class);
34+
$container->register(A::class);
35+
$container->register(B::class);
36+
$container->register(PropertiesInjection::class)->setAutowired(true);
37+
38+
(new ResolveClassPass())->process($container);
39+
(new AutowireRequiredPropertiesPass())->process($container);
40+
41+
$properties = $container->getDefinition(PropertiesInjection::class)->getProperties();
42+
43+
$this->assertArrayHasKey('plop', $properties);
44+
$this->assertEquals(Bar::class, (string) $properties['plop']);
45+
}
46+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
4+
5+
class PropertiesInjection
6+
{
7+
/**
8+
* @required
9+
*/
10+
public Bar $plop;
11+
12+
public function __construct(A $a)
13+
{
14+
}
15+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy