You are here

class GuzzleDefinitionDiscovery in Libraries API 8.3

Provides a definition discovery that fetches remote definitions using Guzzle.

By default JSON files are assumed to be in JSON format.

@todo Cache responses statically by ID to avoid multiple HTTP requests when calling hasDefinition() and getDefinition() sequentially.

Hierarchy

Expanded class hierarchy of GuzzleDefinitionDiscovery

File

src/ExternalLibrary/Definition/GuzzleDefinitionDiscovery.php, line 18

Namespace

Drupal\libraries\ExternalLibrary\Definition
View source
class GuzzleDefinitionDiscovery extends FileDefinitionDiscoveryBase implements DefinitionDiscoveryInterface {

  /**
   * The HTTP client.
   *
   * @var \GuzzleHttp\ClientInterface
   */
  protected $httpClient;

  /**
   * Constructs a Guzzle-based definition discvoery.
   *
   * @param \GuzzleHttp\ClientInterface $http_client
   *   The HTTP client.
   * @param \Drupal\Component\Serialization\SerializationInterface $serializer
   *   The serializer for the library definition files.
   * @param string $base_url
   *   The base URL for the library files.
   */
  public function __construct(ClientInterface $http_client, SerializationInterface $serializer, $base_url) {
    parent::__construct($serializer, $base_url);
    $this->httpClient = $http_client;
  }

  /**
   * {@inheritdoc}
   */
  public function hasDefinition($id) {
    try {
      $response = $this->httpClient
        ->request('GET', $this
        ->getFileUri($id));
      return $response
        ->getStatusCode() === 200;
    } catch (GuzzleException $exception) {
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function getSerializedDefinition($id) {
    try {
      $response = $this->httpClient
        ->request('GET', $this
        ->getFileUri($id));
      return (string) $response
        ->getBody();
    } catch (GuzzleException $exception) {
      throw new LibraryDefinitionNotFoundException($id, '', 0, $exception);
    } catch (\RuntimeException $exception) {
      throw new LibraryDefinitionNotFoundException($id, '', 0, $exception);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FileDefinitionDiscoveryBase::$baseUri protected property The base URI for the library files.
FileDefinitionDiscoveryBase::$serializer protected property The serializer for the library definition files.
FileDefinitionDiscoveryBase::getDefinition public function Gets a library definition by its ID. Overrides DefinitionDiscoveryInterface::getDefinition
FileDefinitionDiscoveryBase::getFileUri protected function Returns the file URI of the library definition file for a given library ID.
GuzzleDefinitionDiscovery::$httpClient protected property The HTTP client.
GuzzleDefinitionDiscovery::getSerializedDefinition protected function Gets the contents of the library file. Overrides FileDefinitionDiscoveryBase::getSerializedDefinition
GuzzleDefinitionDiscovery::hasDefinition public function Checks whether a library definition exists. Overrides DefinitionDiscoveryInterface::hasDefinition
GuzzleDefinitionDiscovery::__construct public function Constructs a Guzzle-based definition discvoery. Overrides FileDefinitionDiscoveryBase::__construct