Skip to content

Commit

Permalink
Simplify ApplicationFactory a little (#5926)
Browse files Browse the repository at this point in the history
  • Loading branch information
weitzman committed Mar 23, 2024
1 parent a7f9dcb commit 35cec3a
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/dependency-injection.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Autowire
------------------
:octicons-tag-24: 12.5+

Command files may inject Drush and Drupal services by adding the [AutowireTrait](https://github.com/drush-ops/drush/blob/13.x/src/Commands/AutowireTrait.php) to the class (example: [PmCommands](https://github.com/drush-ops/drush/blob/13.x/src/Commands/pm/PmCommands.php)). This enables your [Constructor parameter type hints determine the the injected service](https://www.drupal.org/node/3396179). When a type hint is insufficient, an [#[Autowire] Attribute](https://www.drupal.org/node/3396179) on the constructor property (with _service:_ named argument) directs AutoWireTrait to the right service (example: [LoginCommands](https://github.com/drush-ops/drush/blob/13.x/src/Commands/core/LoginCommands.php)).
Command files may inject Drush and Drupal services by adding the [AutowireTrait](https://github.com/drush-ops/drush/blob/13.x/src/Commands/AutowireTrait.php) to the class (example: [PmCommands](https://github.com/drush-ops/drush/blob/13.x/src/Commands/pm/PmCommands.php)). This enables your [Constructor parameter type hints determine the the injected service](https://www.drupal.org/node/3396179). When a type hint is insufficient, an [#[Autowire] Attribute](https://www.drupal.org/node/3396179) on the constructor property (with _service:_ named argument) directs AutoWireTrait to the right service (example: [LoginCommands](https://github.com/drush-ops/drush/blob/13.x/src/Commands/core/LoginCommands.php)).

If your command is not found by Drush, add the `-vvv` option for debug info about any service instantiation errors. If Autowire is still insufficient, a commandfile may implement its own `create()` method (see below).

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/core/MkCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function docs(): void
$destination_path = Path::join($dir_root, 'docs', $destination);
$this->prepare($destination_path);
$container = Drush::getContainer();
$application_generate = (new ApplicationFactory($container->get('service_container'), $container, $this->logger()))->create();
$application_generate = (new ApplicationFactory($container, $this->logger()))->create();
$all = $this->createAnnotatedCommands($application_generate, Drush::getApplication());
$namespaced = ListCommands::categorize($all);
[$nav_generators, $pages_generators, $map_generators] = $this->writeContentFilesAndBuildNavAndBuildRedirectMap($namespaced, $destination, $dir_root, $destination_path);
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/generate/ApplicationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
use Drush\Commands\generate\Generators\Drush\DrushCommandFile;
use Drush\Commands\generate\Generators\Drush\DrushGeneratorFile;
use Drush\Runtime\ServiceManager;
use Psr\Container\ContainerInterface;
use Psr\Container\ContainerInterface as DrushContainer;
use Psr\Log\LoggerInterface;

class ApplicationFactory
{
private ServiceManager $serviceManager;
private \Symfony\Component\DependencyInjection\ContainerInterface $container;

public function __construct(
private ContainerInterface $container,
private DrushContainer $drush_container,
private LoggerInterface $logger
) {
$this->serviceManager = $this->drush_container->get('service.manager');
$this->container = $this->drush_container->get('service_container');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/generate/GenerateCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static function create(ContainerInterface $container): self
#[CLI\Complete(method_name_or_callable: 'generatorNameComplete')]
public function generate(string $generator = '', $options = ['replace' => false, 'working-dir' => self::REQ, 'answer' => [], 'destination' => self::REQ, 'dry-run' => false]): int
{
$application = (new ApplicationFactory($this->container, $this->drush_container, $this->logger()))->create();
$application = (new ApplicationFactory($this->drush_container, $this->logger()))->create();

if (!$generator || $generator == 'list') {
$all = $application->all();
Expand Down

0 comments on commit 35cec3a

Please sign in to comment.