File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed
src/Symfony/Component/DependencyInjection Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change
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 \Attribute ;
13
+
14
+ use Symfony \Component \DependencyInjection \Definition ;
15
+ use Symfony \Component \DependencyInjection \Reference ;
16
+ use Symfony \Component \DependencyInjection \TypedReference ;
17
+
18
+ /**
19
+ * Tells which method should be turned into a Closure based on the name of the parameter it's attached to.
20
+ */
21
+ #[\Attribute(\Attribute::TARGET_PARAMETER )]
22
+ class AutowireMethodOf extends AutowireCallable
23
+ {
24
+ /**
25
+ * @param string $service The service containing the method to autowire
26
+ * @param bool|class-string $lazy Whether to use lazy-loading for this argument
27
+ */
28
+ public function __construct (string $ service , bool |string $ lazy = false )
29
+ {
30
+ parent ::__construct ([new Reference ($ service )], lazy: $ lazy );
31
+ }
32
+
33
+ public function buildDefinition (mixed $ value , ?string $ type , \ReflectionParameter $ parameter ): Definition
34
+ {
35
+ $ value [1 ] = $ parameter ->name ;
36
+
37
+ return parent ::buildDefinition ($ value , $ type , $ parameter );
38
+ }
39
+ }
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ CHANGELOG
8
8
* Add argument ` $prepend ` to ` ContainerConfigurator::extension() ` to prepend the configuration instead of appending it
9
9
* Have ` ServiceLocator ` implement ` ServiceCollectionInterface `
10
10
* Add ` #[Lazy] ` attribute as shortcut for ` #[Autowire(lazy: [bool|string])] ` and ` #[Autoconfigure(lazy: [bool|string])] `
11
+ * Add ` #[AutowireMethodOf] ` attribute to autowire a method of a service as a callable
11
12
12
13
7.0
13
14
---
You can’t perform that action at this time.
0 commit comments