-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Autowire in commandfiles (#5889)
- Loading branch information
Showing
78 changed files
with
537 additions
and
773 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Drush\Commands; | ||
|
||
use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
use Symfony\Component\DependencyInjection\Exception\AutowiringFailedException; | ||
|
||
/** | ||
* A copy of \Drupal\Core\DependencyInjection\AutowireTrait with first params' type hint changed. | ||
* | ||
* Defines a trait for automatically wiring dependencies from the container. | ||
* | ||
* This trait uses reflection and may cause performance issues with classes | ||
* that will be instantiated multiple times. | ||
*/ | ||
trait AutowireTrait | ||
{ | ||
/** | ||
* Instantiates a new instance of the implementing class using autowiring. | ||
* | ||
* @param \Psr\Container\ContainerInterface $container | ||
* The service container this instance should use. | ||
* | ||
* @return static | ||
*/ | ||
public static function create(\Psr\Container\ContainerInterface $container) | ||
{ | ||
$args = []; | ||
|
||
if (method_exists(static::class, '__construct')) { | ||
$constructor = new \ReflectionMethod(static::class, '__construct'); | ||
foreach ($constructor->getParameters() 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.