/**
  * Constructor.
  *
  * Supported options:
  *
  * - base_uri Default application hostname, optionally including base URL,
  *   for purge and refresh requests (optional). This is required if you
  *   purge and refresh paths instead of absolute URLs.
  *
  * @param array                $servers        Caching proxy server hostnames or IP
  *                                             addresses, including port if not port 80.
  *                                             E.g. ['127.0.0.1:6081']
  * @param array                $options        List of options for the client
  * @param HttpAsyncClient|null $httpClient     Client capable of sending HTTP requests. If no
  *                                             client is supplied, a default one is created
  * @param MessageFactory|null  $messageFactory Factory for PSR-7 messages. If none supplied,
  *                                             a default one is created
  * @param UriFactory|null      $uriFactory     Factory for PSR-7 URIs. If not specified, a
  *                                             default one is created
  */
 public function __construct(array $servers, array $options = [], HttpAsyncClient $httpClient = null, MessageFactory $messageFactory = null, UriFactory $uriFactory = null)
 {
     if (!$httpClient) {
         $httpClient = HttpAsyncClientDiscovery::find();
     }
     if (!$uriFactory) {
         $uriFactory = UriFactoryDiscovery::find();
     }
     $this->options = $this->getDefaultOptions()->resolve($options);
     $this->httpAdapter = new HttpAdapter($servers, $this->options['base_uri'], $httpClient, $uriFactory);
     $this->messageFactory = $messageFactory ?: MessageFactoryDiscovery::find();
 }
 /**
  * Register php-http interfaces to container.
  */
 protected function registerHttplugFactories()
 {
     $this->app->bind('httplug.message_factory.default', function ($app) {
         return MessageFactoryDiscovery::find();
     });
     $this->app->alias('httplug.message_factory.default', MessageFactory::class);
     $this->app->alias('httplug.message_factory.default', ResponseFactory::class);
     $this->app->bind('httplug.uri_factory.default', function ($app) {
         return UriFactoryDiscovery::find();
     });
     $this->app->alias('httplug.uri_factory.default', UriFactory::class);
     $this->app->bind('httplug.stream_factory.default', function ($app) {
         return StreamFactoryDiscovery::find();
     });
     $this->app->alias('httplug.stream_factory.default', StreamFactory::class);
 }
예제 #3
0
 /**
  * Create PSR-7 URI object from URI string.
  *
  * @param string $uriString
  *
  * @return UriInterface
  */
 private function createUri($uriString)
 {
     return UriFactoryDiscovery::find()->createUri($uriString);
 }
예제 #4
0
 /**
  * Filter a URL
  *
  * Prefix the URL with "http://" if it has no scheme, then check the URL
  * for validity. You can specify what parts of the URL are allowed.
  *
  * @param string       $uriString
  * @param string[]     $allowedParts Array of allowed URL parts (optional)
  *
  * @throws InvalidUrlException If URL is invalid, the scheme is not http or
  *                             contains parts that are not expected.
  *
  * @return UriInterface Filtered URI (with default scheme if there was no scheme)
  */
 private function filterUri($uriString, array $allowedParts = [])
 {
     // Creating a PSR-7 URI without scheme (with parse_url) results in the
     // original hostname to be seen as path. So first add a scheme if none
     // is given.
     if (false === strpos($uriString, '://')) {
         $uriString = sprintf('%s://%s', 'http', $uriString);
     }
     try {
         $uri = UriFactoryDiscovery::find()->createUri($uriString);
     } catch (\InvalidArgumentException $e) {
         throw InvalidUrlException::invalidUrl($uriString);
     }
     if (!$uri->getScheme()) {
         throw InvalidUrlException::invalidUrl($uriString, 'empty scheme');
     }
     if (count($allowedParts) > 0) {
         $parts = parse_url((string) $uri);
         $diff = array_diff(array_keys($parts), $allowedParts);
         if (count($diff) > 0) {
             throw InvalidUrlException::invalidUrlParts($uriString, $allowedParts);
         }
     }
     return $uri;
 }
예제 #5
0
 public function build($schemaPath)
 {
     // Build serializer
     if ($this->serializer === null) {
         if (empty($this->encoders)) {
             $this->encoders = [new JsonEncoder(), new XmlEncoder()];
         }
         if (empty($this->denormalizers)) {
             $this->denormalizers[] = new ResourceDenormalizer($this->paginationProvider);
         }
         $this->serializer = new Serializer($this->denormalizers, $this->encoders);
     }
     if ($this->uriFactory === null) {
         $this->uriFactory = UriFactoryDiscovery::find();
     }
     if ($this->messageFactory === null) {
         $this->messageFactory = MessageFactoryDiscovery::find();
     }
     if ($this->httpClient === null) {
         $this->httpClient = HttpClientDiscovery::find();
     }
     $schemaFactory = new SwaggerSchemaFactory();
     if ($this->cache !== null) {
         $schemaFactory = new CachedSchemaFactoryDecorator($this->cache, $schemaFactory);
     }
     $this->schema = $schemaFactory->createSchema($schemaPath);
     if (!isset($this->requestValidator)) {
         $this->requestValidator = new MessageValidator(new Validator(), new SymfonyDecoderAdapter(new ChainDecoder($this->encoders)));
     }
     return new ApiService($this->uriFactory, new UriTemplate(), $this->httpClient, $this->messageFactory, $this->schema, $this->requestValidator, $this->serializer, $this->config);
 }
예제 #6
0
 public static function create() : self
 {
     return new self(HttpClientDiscovery::find(), MessageFactoryDiscovery::find(), UriFactoryDiscovery::find());
 }
 /**
  * @return UriFactory
  */
 private function getUriFactory()
 {
     if ($this->uriFactory === null) {
         $this->uriFactory = UriFactoryDiscovery::find();
     }
     return $this->uriFactory;
 }