Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drupal
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
drupal
Merge requests
!8306
Add an Autowire trait for plugins
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Add an Autowire trait for plugins
issue/drupal-3452852:3452852-autowire-trait-plugins
into
11.x
Overview
0
Commits
14
Pipelines
15
Changes
14
Open
dpi
requested to merge
issue/drupal-3452852:3452852-autowire-trait-plugins
into
11.x
11 months ago
Overview
0
Commits
14
Pipelines
15
Changes
14
Expand
Closes
#3452852
0
0
Merge request reports
Compare
11.x
version 14
ce088daa
4 months ago
version 13
8f1c6a80
7 months ago
version 12
31f7e207
7 months ago
version 11
412c3b97
7 months ago
version 10
cc40b0e0
7 months ago
version 9
9a7dd4ec
7 months ago
version 8
c2fac516
7 months ago
version 7
58504b57
9 months ago
version 6
6e8cfddd
9 months ago
version 5
a04a3b72
9 months ago
version 4
8c5dc9c0
9 months ago
version 3
83b66e0c
9 months ago
version 2
83b66e0c
9 months ago
version 1
d8801704
11 months ago
11.x (HEAD)
and
latest version
latest version
c4a7e4cc
14 commits,
4 months ago
version 14
ce088daa
13 commits,
4 months ago
version 13
8f1c6a80
15 commits,
7 months ago
version 12
31f7e207
15 commits,
7 months ago
version 11
412c3b97
13 commits,
7 months ago
version 10
cc40b0e0
11 commits,
7 months ago
version 9
9a7dd4ec
10 commits,
7 months ago
version 8
c2fac516
9 commits,
7 months ago
version 7
58504b57
8 commits,
9 months ago
version 6
6e8cfddd
7 commits,
9 months ago
version 5
a04a3b72
6 commits,
9 months ago
version 4
8c5dc9c0
5 commits,
9 months ago
version 3
83b66e0c
4 commits,
9 months ago
version 2
83b66e0c
458 commits,
9 months ago
version 1
d8801704
1 commit,
11 months ago
14 files
+
203
−
111
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
14
Search (e.g. *.vue) (Ctrl+P)
core/lib/Drupal/Core/DependencyInjection/AutowireArgumentsTrait.php
0 → 100644
+
46
−
0
Options
<?php
declare
(
strict_types
=
1
);
namespace
Drupal\Core\DependencyInjection
;
use
Symfony\Component\DependencyInjection\Attribute\Autowire
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
use
Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
;
/**
* Defines a base trait for automatically wiring dependency arguments.
*/
trait
AutowireArgumentsTrait
{
/**
* Instantiates a new instance of the implementing class using autowiring.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The service container this instance should use.
* @param ...$args
* Any predefined arguments to pass to the constructor.
*
* @return static
*/
public
static
function
autowireArguments
(
ContainerInterface
$container
,
...
$args
)
{
if
(
method_exists
(
static
::
class
,
'__construct'
))
{
$constructor
=
new
\ReflectionMethod
(
static
::
class
,
'__construct'
);
foreach
(
array_slice
(
$constructor
->
getParameters
(),
count
(
$args
))
as
$parameter
)
{
$service
=
ltrim
((
string
)
$parameter
->
getType
(),
'?'
);
foreach
(
$parameter
->
getAttributes
(
Autowire
::
class
)
as
$attribute
)
{
$service
=
(
string
)
$attribute
->
newInstance
()
->
value
;
}
if
(
!
$container
->
has
(
$service
))
{
throw
new
AutowiringFailedException
(
$service
,
sprintf
(
'Cannot autowire service "%s": argument "$%s" of method "%s::_construct()", you should configure its value explicitly.'
,
$service
,
$parameter
->
getName
(),
static
::
class
));
}
$args
[]
=
$container
->
get
(
$service
);
}
}
return
new
static
(
...
$args
);
}
}
Loading