reviewer-1.2.x-dev/src/Reviewer/Task/TaskFactory.php

src/Reviewer/Task/TaskFactory.php
<?php

declare(strict_types=1);

namespace Drupal\reviewer\Reviewer\Task;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Exception\AutowiringFailedException;

/**
 * Creates tasks.
 */
final readonly class TaskFactory implements TaskFactoryInterface {

  // phpcs:ignore Drupal.Commenting.FunctionComment.Missing
  public function __construct(private ContainerInterface $container) {}

  /**
   * {@inheritdoc}
   */
  public function create(string $class): TaskInterface {
    $args = [];
    $task_constructor = new \ReflectionMethod($class, '__construct');
    foreach ($task_constructor->getParameters() as $parameter) {
      $service = (string) $parameter->getType();
      if (!$this->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(),
          $class,
        ));
      }
      $args[] = $this->container->get($service);
    }

    return new $class(...$args);
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc