Commit f3032ad5 authored by Jürgen Haas's avatar Jürgen Haas Committed by Adriano
Browse files

Issue #3316197 by jurgenhaas: Provide an action plugin

parent 89e1110d
Loading
Loading
Loading
Loading
+139 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\http_client_manager\Plugin\Action;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Action\ConfigurableActionBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\http_client_manager\HttpClientManagerFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Wrapper action for all http_client_manager commands.
 *
 * @Action(
 *   id = "http_client_manager_command",
 *   deriver = "\Drupal\http_client_manager\Plugin\Action\CommandDeriver",
 *   nodocs = true
 * )
 */
class Command extends ConfigurableActionBase implements ContainerFactoryPluginInterface {

  public const KEY_LAST_RESULT = 'last result';
  public const TIMEOUT_LAST_RESULT = 5;

  /**
   * The client manager factory.
   *
   * @var \Drupal\http_client_manager\HttpClientManagerFactoryInterface
   */
  protected HttpClientManagerFactoryInterface $clientManagerFactory;

  /**
   * The expirable key value store.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
   */
  protected KeyValueStoreExpirableInterface $keyValueStoreExpirable;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, HttpClientManagerFactoryInterface $clientManagerFactory, KeyValueStoreExpirableInterface $keyValueStoreExpirable) {
    $this->clientManagerFactory = $clientManagerFactory;
    $this->keyValueStoreExpirable = $keyValueStoreExpirable;
    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): Command {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('http_client_manager.factory'),
      $container->get('keyvalue.expirable')->get('http_client_manager')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration(): array {
    $config = [];
    [, $serviceId, $commandName] = explode(':', $this->getPluginId());
    $client = $this->clientManagerFactory->get($serviceId);
    $command = $client->getCommand($commandName);
    foreach ($command->getParams() as $id => $param) {
      if ($param->getType() !== NULL) {
        $config[$id] = $param->getDefault();
      }
    }
    return $config + parent::defaultConfiguration();
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
    [, $serviceId, $commandName] = explode(':', $this->getPluginId());
    $client = $this->clientManagerFactory->get($serviceId);
    $command = $client->getCommand($commandName);
    foreach ($command->getParams() as $id => $param) {
      if ($param->getType() !== NULL) {
        $form[$id] = [
          '#type' => 'textfield',
          '#title' => $param->getDescription(),
          '#default_value' => $this->configuration[$id],
          '#required' => $param->isRequired(),
        ];
      }
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
    [, $serviceId, $commandName] = explode(':', $this->getPluginId());
    $client = $this->clientManagerFactory->get($serviceId);
    $command = $client->getCommand($commandName);
    foreach ($command->getParams() as $id => $param) {
      if ($param->getType() !== NULL) {
        $this->configuration[$id] = $form_state->getValue($id);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
    $access = AccessResult::allowed();
    return $return_as_object ? $access : $access->isAllowed();
  }

  /**
   * {@inheritdoc}
   */
  public function execute($object = NULL): void {
    [, $serviceId, $commandName] = explode(':', $this->getPluginId());
    $client = $this->clientManagerFactory->get($serviceId);
    $command = $client->getCommand($commandName);
    $params = [];
    foreach ($command->getParams() as $id => $param) {
      if ($param->getType() !== NULL) {
        $params[$id] = $this->configuration[$id];
      }
    }
    $result = $client->call($commandName, $params);
    $this->keyValueStoreExpirable->setWithExpire(self::KEY_LAST_RESULT, $result->toArray(), self::TIMEOUT_LAST_RESULT);
  }

}
+78 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\http_client_manager\Plugin\Action;

use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\http_client_manager\HttpClientManagerFactoryInterface;
use Drupal\http_client_manager\HttpServiceApiHandlerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Deriver for http_client_manager command actions.
 */
class CommandDeriver extends DeriverBase implements ContainerDeriverInterface {

  /**
   * The service api handler service.
   *
   * @var \Drupal\http_client_manager\HttpServiceApiHandlerInterface
   */
  protected HttpServiceApiHandlerInterface $serviceApiHandler;

  /**
   * The client manager factory.
   *
   * @var \Drupal\http_client_manager\HttpClientManagerFactoryInterface
   */
  protected HttpClientManagerFactoryInterface $clientManagerFactory;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    $instance = new static();
    $instance->setServicesApi($container->get('http_client_manager.http_services_api'));
    $instance->setFactory($container->get('http_client_manager.factory'));
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition): array {
    $this->derivatives = [];
    foreach ($this->serviceApiHandler->getServicesApi() as $service) {
      $client = $this->clientManagerFactory->get($service['id']);
      foreach ($client->getCommands() as $name => $command) {
        $id = implode(':', [$service['id'], $name]);
        $this->derivatives[$id] = [
          'label' => $service['title'] . ' - ' . $command->getSummary(),
          'action_entity_id' => $id,
        ] + $base_plugin_definition;
      }
    }
    return $this->derivatives;
  }

  /**
   * Set the service api handler service.
   *
   * @param \Drupal\http_client_manager\HttpServiceApiHandlerInterface $serviceApiHandler
   *   The service api handler service.
   */
  public function setServicesApi(HttpServiceApiHandlerInterface $serviceApiHandler): void {
    $this->serviceApiHandler = $serviceApiHandler;
  }

  /**
   * Set the client manager factory.
   *
   * @param \Drupal\http_client_manager\HttpClientManagerFactoryInterface $clientManagerFactory
   *   The client manager factory.
   */
  public function setFactory(HttpClientManagerFactoryInterface $clientManagerFactory): void {
    $this->clientManagerFactory = $clientManagerFactory;
  }

}
+79 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\http_client_manager\Plugin\Action;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Wrapper action for all pre-configured http requests.
 *
 * @Action(
 *   id = "http_client_manager_preconfigured_request",
 *   deriver = "\Drupal\http_client_manager\Plugin\Action\PreConfiguredRequestDeriver",
 *   nodocs = true
 * )
 */
class PreConfiguredRequest extends ActionBase implements ContainerFactoryPluginInterface {

  /**
   * The entity storage interface for http config request config entities.
   *
   * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
   */
  protected ConfigEntityStorageInterface $entityStorage;

  /**
   * The expirable key value store.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
   */
  protected KeyValueStoreExpirableInterface $keyValueStoreExpirable;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigEntityStorageInterface $entityStorage, KeyValueStoreExpirableInterface $keyValueStoreExpirable) {
    $this->entityStorage = $entityStorage;
    $this->keyValueStoreExpirable = $keyValueStoreExpirable;
    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): PreConfiguredRequest {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('entity_type.manager')->getStorage('http_config_request'),
      $container->get('keyvalue.expirable')->get('http_client_manager')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
    $access = AccessResult::allowed();
    return $return_as_object ? $access : $access->isAllowed();
  }

  /**
   * {@inheritdoc}
   */
  public function execute($object = NULL): void {
    [, $entityId] = explode(':', $this->getPluginId());
    /** @var \Drupal\http_client_manager\Entity\HttpConfigRequestInterface $request */
    $request = $this->entityStorage->load($entityId);
    $result = $request->execute();
    $this->keyValueStoreExpirable->setWithExpire(Command::KEY_LAST_RESULT, $result->toArray(), Command::TIMEOUT_LAST_RESULT);
  }

}
+57 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\http_client_manager\Plugin\Action;

use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Deriver for preconfigured htrtp requests.
 */
class PreConfiguredRequestDeriver extends DeriverBase implements ContainerDeriverInterface {

  /**
   * The action entity storage.
   *
   * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
   */
  protected ConfigEntityStorageInterface $entityStorage;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    $instance = new static();
    $instance->setEntityStorage($container->get('entity_type.manager')->getStorage('http_config_request'));
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition): array {
    $this->derivatives = [];
    /** @var \Drupal\http_client_manager\Entity\HttpConfigRequestInterface $request */
    foreach ($this->entityStorage->loadMultiple() as $request) {
      $id = $request->id();
      $this->derivatives[$id] = [
        'label' => 'Pre-configured: ' . $request->label(),
        'action_entity_id' => $id,
      ] + $base_plugin_definition;
    }
    return $this->derivatives;
  }

  /**
   * Set the entity storage.
   *
   * @param \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage
   *   The entity storage.
   */
  public function setEntityStorage(ConfigEntityStorageInterface $storage): void {
    $this->entityStorage = $storage;
  }

}