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
!10867
An error occurred while fetching the assigned milestone of the selected merge_request.
Rebase 11.x
Code
Review changes
Check out branch
Download
Patches
Plain diff
Closed
Rebase 11.x
issue/drupal-3452852:3452852-autowire-trait-plugins-rebase
into
11.x
Overview
0
Commits
15
Pipelines
1
Changes
13
Closed
mortona2k
requested to merge
issue/drupal-3452852:3452852-autowire-trait-plugins-rebase
into
11.x
4 months ago
Overview
0
Commits
15
Pipelines
1
Changes
13
Expand
This reverts commit
9a7dd4ec
.
Conflicts:
core/.phpstan-baseline.php
Closes
#3452852
0
0
Merge request reports
Compare
11.x
11.x (base)
and
latest version
latest version
f639357a
15 commits,
4 months ago
13 files
+
203
−
105
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
13
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