GuzzleDefinitionDiscovery.php in Libraries API 8.3
File
src/ExternalLibrary/Definition/GuzzleDefinitionDiscovery.php
View source
<?php
namespace Drupal\libraries\ExternalLibrary\Definition;
use Drupal\Component\Serialization\SerializationInterface;
use Drupal\libraries\ExternalLibrary\Exception\LibraryDefinitionNotFoundException;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
class GuzzleDefinitionDiscovery extends FileDefinitionDiscoveryBase implements DefinitionDiscoveryInterface {
protected $httpClient;
public function __construct(ClientInterface $http_client, SerializationInterface $serializer, $base_url) {
parent::__construct($serializer, $base_url);
$this->httpClient = $http_client;
}
public function hasDefinition($id) {
try {
$response = $this->httpClient
->request('GET', $this
->getFileUri($id));
return $response
->getStatusCode() === 200;
} catch (GuzzleException $exception) {
return FALSE;
}
}
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);
}
}
}