File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed
src/Symfony/Component/DependencyInjection Expand file tree Collapse file tree 2 files changed +39
-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
+
17
+ /**
18
+ * Tells which method should be turned into a Closure based on the name of the parameter it's attached to.
19
+ */
20
+ #[\Attribute(\Attribute::TARGET_PARAMETER )]
21
+ class AutowireMethodOf extends AutowireCallable
22
+ {
23
+ /**
24
+ * @param string $service The service containing the method to autowire
25
+ * @param bool|class-string $lazy Whether to use lazy-loading for this argument
26
+ */
27
+ public function __construct (string $ service , bool |string $ lazy = false )
28
+ {
29
+ parent ::__construct ([new Reference ($ service )], lazy: $ lazy );
30
+ }
31
+
32
+ public function buildDefinition (mixed $ value , ?string $ type , \ReflectionParameter $ parameter ): Definition
33
+ {
34
+ $ value [1 ] = $ parameter ->name ;
35
+
36
+ return parent ::buildDefinition ($ value , $ type , $ parameter );
37
+ }
38
+ }
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