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
- class \Drupal\libraries\ExternalLibrary\Definition\FileDefinitionDiscoveryBase implements DefinitionDiscoveryInterface
- class \Drupal\libraries\ExternalLibrary\Definition\GuzzleDefinitionDiscovery implements DefinitionDiscoveryInterface
Expanded class hierarchy of GuzzleDefinitionDiscovery
File
- src/
ExternalLibrary/ Definition/ GuzzleDefinitionDiscovery.php, line 18
Namespace
Drupal\libraries\ExternalLibrary\DefinitionView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FileDefinitionDiscoveryBase:: |
protected | property | The base URI for the library files. | |
FileDefinitionDiscoveryBase:: |
protected | property | The serializer for the library definition files. | |
FileDefinitionDiscoveryBase:: |
public | function |
Gets a library definition by its ID. Overrides DefinitionDiscoveryInterface:: |
|
FileDefinitionDiscoveryBase:: |
protected | function | Returns the file URI of the library definition file for a given library ID. | |
GuzzleDefinitionDiscovery:: |
protected | property | The HTTP client. | |
GuzzleDefinitionDiscovery:: |
protected | function |
Gets the contents of the library file. Overrides FileDefinitionDiscoveryBase:: |
|
GuzzleDefinitionDiscovery:: |
public | function |
Checks whether a library definition exists. Overrides DefinitionDiscoveryInterface:: |
|
GuzzleDefinitionDiscovery:: |
public | function |
Constructs a Guzzle-based definition discvoery. Overrides FileDefinitionDiscoveryBase:: |