DevelServicesCommand.php

Same filename in this branch
  1. 5.x src/Drush/Commands/DevelServicesCommand.php

Namespace

Drupal\devel\Command

File

src/Command/DevelServicesCommand.php

View source
<?php

declare (strict_types=1);
namespace Drupal\devel\Command;

use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Attribute\Option;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Lists the services in the container.
 */
class DevelServicesCommand {
  use FormatTrait;
  public const NAME = 'devel:services';
  public function __construct(#[Autowire(service: 'service_container')] protected readonly ContainerInterface $container) {
  }
  
  /**
   * Lists the services in the container.
   */
  public function __invoke(OutputInterface $output, #[Argument('Optional prefix to filter the service list by.')] ?string $prefix = NULL, #[Option('The output format: table, json or yaml.')] string $format = 'table') : int {
    // Both Drupal's container and Symfony's container builder have this method,
    // though no interface that they share declares it.
    // @phpstan-ignore method.notFound
    $services = $this->container
      ->getServiceIds();
    if ($prefix !== NULL && $prefix !== '') {
      $services = preg_grep(sprintf('/%s/', $prefix), $services);
    }
    if (empty($services)) {
      throw new \RuntimeException('No container services found.');
    }
    sort($services);
    $rows = array_map(static fn(string $id): array => [
      'id' => $id,
    ], $services);
    $this->writeRows($output, $rows, [
      'id' => 'Id',
    ], $format);
    return Command::SUCCESS;
  }

}

Classes

Title Deprecated Summary
DevelServicesCommand Lists the services in the container.