Source of file HTTPProvider.php
Size: 3,955 Bytes - Last Modified: 2021-12-23T10:31:47+00:00
/var/www/docs.ssmods.com/process/src/src/PersistedQuery/HTTPProvider.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 | <?php namespace SilverStripe\GraphQL\PersistedQuery; use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Injector\Injectable; use InvalidArgumentException; use Exception; use SilverStripe\Core\Injector\Injector; /** * Class HTTPProvider * @package SilverStripe\GraphQL\PersistedQuery */ class HTTPProvider implements PersistedQueryMappingProvider { use Configurable, Injectable; /** * Timeout for the HTTP request * @var int * @config */ private static $timeout = 5; /** * Example: * <code> * SilverStripe\Core\Injector\Injector: * SilverStripe\GraphQL\PersistedQuery\PersistedQueryMappingProvider: * class :SilverStripe\GraphQL\PersistedQuery\HTTPProvider: * properties: * schemaMapping: * default: 'http://example.com/mapping.json' * </code> * * Note: The mapping supports multi-schema feature, you can have other schemaKey rather than 'default' * * @var array * @config */ protected $schemaToURL = [ 'default' => '' ]; /** * @var HTTPClient */ protected $client; /** * A cache of schema key to HTTP responses * @var array */ protected $responseCache = []; /** * HTTPProvider constructor. * @param HTTPClient $client */ public function __construct(HTTPClient $client = null) { if (!$client) { $client = Injector::inst()->get(GuzzleHTTPClient::class); } $this->setClient($client); } /** * return a map from <id> to <query> * * @param string $schemaKey * @return array */ public function getQueryMapping($schemaKey = 'default') { if (isset($this->responseCache[$schemaKey])) { return $this->responseCache[$schemaKey]; } /** @noinspection PhpUndefinedFieldInspection */ /** @noinspection StaticInvocationViaThisInspection */ $urlWithKey = $this->getSchemaMapping(); if (!isset($urlWithKey[$schemaKey])) { return []; } $url = trim($urlWithKey[$schemaKey]); $map = null; try { $contents = $this->getClient()->getURL($url, $this->config()->get('timeout')); $map = json_decode($contents, true); } catch (Exception $e) { user_error($e->getMessage(), E_USER_WARNING); $map = []; } if (!is_array($map)) { $map = []; } $this->responseCache[$schemaKey] = $map; return $map; } /** * return a query given an ID * * @param string $queryID * @param string $schemaKey * @return string */ public function getByID($queryID, $schemaKey = 'default') { $mapping = $this->getQueryMapping($schemaKey); return isset($mapping[$queryID]) ? $mapping[$queryID] : null; } /** * @param array $mapping * @return $this */ public function setSchemaMapping(array $mapping) { foreach ($mapping as $schemaKey => $url) { if (!filter_var($url, FILTER_VALIDATE_URL)) { throw new InvalidArgumentException( 'setSchemaMapping accepts an array of schema keys to URLs' ); } } // If the URLs have changed, stale the cache. $this->responseCache = []; $this->schemaToURL = $mapping; return $this; } /** * @return array */ public function getSchemaMapping() { return $this->schemaToURL; } /** * @param HTTPClient $client * @return $this */ public function setClient(HTTPClient $client) { $this->client = $client; return $this; } /** * @return HTTPClient */ public function getClient() { return $this->client; } } |