You are here

class HttpServiceApiHandler in HTTP Client Manager 8

Same name and namespace in other branches
  1. 8.2 src/HttpServiceApiHandler.php \Drupal\http_client_manager\HttpServiceApiHandler

Class HttpServiceApiHandler.

@package Drupal\http_client_manager

Hierarchy

Expanded class hierarchy of HttpServiceApiHandler

2 files declare their use of HttpServiceApiHandler
HttpClientManagerPreview.php in src/Controller/HttpClientManagerPreview.php
http_client_manager.module in ./http_client_manager.module
Contains http_client_manager.module..
1 string reference to 'HttpServiceApiHandler'
http_client_manager.services.yml in ./http_client_manager.services.yml
http_client_manager.services.yml
1 service uses HttpServiceApiHandler
http_client_manager.http_services_api in ./http_client_manager.services.yml
Drupal\http_client_manager\HttpServiceApiHandler

File

src/HttpServiceApiHandler.php, line 17

Namespace

Drupal\http_client_manager
View source
class HttpServiceApiHandler implements HttpServiceApiHandlerInterface {

  /**
   * Drupal root.
   *
   * @var string
   */
  protected $root;

  /**
   * Drupal\Core\Extension\ModuleHandler definition.
   *
   * @var \Drupal\Core\Extension\ModuleHandler
   */
  protected $moduleHandler;

  /**
   * Drupal\Core\StringTranslation\TranslationManager definition.
   *
   * @var \Drupal\Core\StringTranslation\TranslationManager
   */
  protected $stringTranslation;

  /**
   * Drupal\Core\Controller\ControllerResolver definition.
   *
   * @var \Drupal\Core\Controller\ControllerResolver
   */
  protected $controllerResolver;

  /**
   * All defined services api descriptions.
   *
   * @var array
   */
  protected $servicesApi;

  /**
   * The HTTP Client Manager config.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * HttpServiceApiHandler constructor.
   *
   * @param string $root
   *   The Application root.
   * @param \Drupal\Core\Extension\ModuleHandler $module_handler
   *   The module handler service.
   * @param \Drupal\Core\StringTranslation\TranslationManager $string_translation
   *   The string translation manager.
   * @param \Drupal\Core\Controller\ControllerResolver $controller_resolver
   *   The controller resolver service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   */
  public function __construct($root, ModuleHandler $module_handler, TranslationManager $string_translation, ControllerResolver $controller_resolver, ConfigFactoryInterface $config_factory) {
    $this->root = $root;
    $this->moduleHandler = $module_handler;
    $this->stringTranslation = $string_translation;
    $this->controllerResolver = $controller_resolver;
    $this->config = $config_factory
      ->get('http_client_manager.settings');
    $this->servicesApi = $this
      ->getServicesApi();
  }

  /**
   * Gets the YAML discovery.
   *
   * @return \Drupal\Component\Discovery\YamlDiscovery
   *   The YAML discovery.
   */
  protected function getYamlDiscovery() {
    if (!isset($this->yamlDiscovery)) {
      $this->yamlDiscovery = new YamlDiscovery('http_services_api', $this->moduleHandler
        ->getModuleDirectories());
    }
    return $this->yamlDiscovery;
  }

  /**
   * {@inheritdoc}
   */
  public function getServicesApi() {
    if (empty($this->servicesApi)) {
      $this
        ->buildServicesApiYaml();
    }
    return $this->servicesApi;
  }

  /**
   * {@inheritdoc}
   */
  public function load($id) {
    if (empty($this->servicesApi[$id])) {
      $message = sprintf('Undefined Http Service Api id "%s"', $id);
      throw new \InvalidArgumentException($message);
    }
    return $this->servicesApi[$id];
  }

  /**
   * {@inheritdoc}
   */
  public function moduleProvidesApi($module_name) {
    $servicesApi = $this
      ->getServicesApi();
    foreach ($servicesApi as $serviceApi) {
      if ($serviceApi['provider'] == $module_name) {
        return TRUE;
      }
    }
    return FALSE;
  }

  /**
   * Builds all services api provided by .http_services_api.yml files.
   *
   * @return array[]
   *   Each return api is an array with the following keys:
   *   - id: The machine name of the Service Api.
   *   - title: The human-readable name of the API.
   *   - api_path: The Guzzle description path (relative to module directory).
   *   - base_url: The Service API base url.
   *   - provider: The provider module of the Service Api.
   *   - source: The absolute path to the Service API description file.
   *   - config: An array of additional configurations for the HttpClient class.
   *
   * @code
   * example_service:
   *   title: "Example Service"
   *   api_path: src/HttpService/example_service.json
   *   base_url: "http://www.example.com/api/v1"
   *   config:
   *     command.params:
   *       command.request_options:
   *         timeout: 4
   *         connect_timeout: 3
   * @endcode
   */
  protected function buildServicesApiYaml() {
    $this->servicesApi = array();
    $items = $this
      ->getYamlDiscovery()
      ->findAll();
    foreach ($items as $provider => $servicesApi) {
      $module_path = $this->moduleHandler
        ->getModule($provider)
        ->getPath();
      foreach ($servicesApi as $id => $serviceApi) {
        $this
          ->overrideServiceApiDefinition($id, $serviceApi);
        $this
          ->validateServiceApiDefinition($id, $serviceApi);
        $default = [
          'id' => $id,
          'provider' => $provider,
          'source' => $this->root . '/' . $module_path . '/' . $serviceApi['api_path'],
          'config' => [],
        ];
        $this->servicesApi[$id] = array_merge($default, $serviceApi);
      }
    }
  }

  /**
   * Override Service API definition.
   *
   * Checks for overriding configurations in settings.php for the given Service
   * API Definition.
   *
   * @param string $id
   *   The service api id.
   * @param array $serviceApi
   *   An array of service api definition.
   */
  protected function overrideServiceApiDefinition($id, array &$serviceApi) {
    $settings = Settings::get('http_services_api', []);
    if (empty($settings[$id]) || !$this->config
      ->get('enable_overriding_service_definitions')) {
      return;
    }
    $overrides = array_flip(self::getOverridableProperties());
    $settings[$id] = array_intersect_key($settings[$id], $overrides);
    $serviceApi = array_merge($serviceApi, $settings[$id]);
  }

  /**
   * Get overridable Service API properties.
   *
   * @return array
   *   An array containing a list of overridable property names.
   */
  public static function getOverridableProperties() {
    return [
      'title',
      'base_url',
      'api_path',
      'config',
    ];
  }

  /**
   * Validates Service api definition.
   *
   * @param string $id
   *   The service api id.
   * @param array $serviceApi
   *   An array of service api definition.
   *
   * @return bool
   *   Whether or not the api is valid.
   */
  protected function validateServiceApiDefinition($id, array $serviceApi) {
    $required = [
      'title',
      'base_url',
      'api_path',
    ];
    foreach ($required as $property) {
      if (!isset($serviceApi[$property])) {
        $message = sprintf('Missing required parameter "%s" in "%s" service api definition', $property, $id);
        throw new \RuntimeException($message);
      }
    }
  }

  /**
   * Returns all module names.
   *
   * @return string[]
   *   Returns the human readable names of all modules keyed by machine name.
   */
  protected function getModuleNames() {
    $modules = array();
    foreach (array_keys($this->moduleHandler
      ->getModuleList()) as $module) {
      $modules[$module] = $this->moduleHandler
        ->getName($module);
    }
    asort($modules);
    return $modules;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HttpServiceApiHandler::$config protected property The HTTP Client Manager config.
HttpServiceApiHandler::$controllerResolver protected property Drupal\Core\Controller\ControllerResolver definition.
HttpServiceApiHandler::$moduleHandler protected property Drupal\Core\Extension\ModuleHandler definition.
HttpServiceApiHandler::$root protected property Drupal root.
HttpServiceApiHandler::$servicesApi protected property All defined services api descriptions.
HttpServiceApiHandler::$stringTranslation protected property Drupal\Core\StringTranslation\TranslationManager definition.
HttpServiceApiHandler::buildServicesApiYaml protected function Builds all services api provided by .http_services_api.yml files.
HttpServiceApiHandler::getModuleNames protected function Returns all module names.
HttpServiceApiHandler::getOverridableProperties public static function Get overridable Service API properties.
HttpServiceApiHandler::getServicesApi public function Gets all available services Api. Overrides HttpServiceApiHandlerInterface::getServicesApi
HttpServiceApiHandler::getYamlDiscovery protected function Gets the YAML discovery.
HttpServiceApiHandler::load public function Load Service Api description. Overrides HttpServiceApiHandlerInterface::load
HttpServiceApiHandler::moduleProvidesApi public function Determines whether a module provides some service API. Overrides HttpServiceApiHandlerInterface::moduleProvidesApi
HttpServiceApiHandler::overrideServiceApiDefinition protected function Override Service API definition.
HttpServiceApiHandler::validateServiceApiDefinition protected function Validates Service api definition.
HttpServiceApiHandler::__construct public function HttpServiceApiHandler constructor.