/** * Before Send Prepare Expression * @param mixed $expr * @return iStreamable|string */ function onBeforeSendPrepareExpression($expr) { if ($expr instanceof RequestInterface || is_string($expr)) { ## convert PSR request to Poirot $expr = new HttpRequest($expr); } if (!$expr instanceof iHttpRequest) { throw new \InvalidArgumentException(sprintf('Request Expression must be string, iHttpRequest or RequestInterface. given: (%s).', \Poirot\Std\flatten($expr))); } // ... ## handle prepare request headers event $httpRequest = $expr; $emitter = $this->event()->trigger(TransporterHttpEvents::EVENT_REQUEST_SEND_PREPARE, ['request' => $httpRequest, 'transporter' => $this]); /** @var iHttpRequest $expr */ $expr = $httpRequest = $emitter->collector()->getRequest(); if ($httpRequest instanceof iHttpRequest) { # ! # support for large body streams /** @var Streamable\AggregateStream $expr */ $expr = new Streamable\AggregateStream(); $expr->addStream((new Streamable\TemporaryStream($httpRequest->renderRequestLine() . $httpRequest->renderHeaders()))->rewind()); $body = $httpRequest->getBody(); if ($body !== null || $body !== '') { if (!$body instanceof iStreamable) { $body = new Streamable\TemporaryStream($body); } $expr->addStream($body->rewind()); } } return $expr; }
/** * @param iHttpUri|UriInterface|string $baseUrl * @return $this */ public function setBaseUrl($baseUrl) { if (is_string($baseUrl) || $baseUrl instanceof UriInterface) { $baseUrl = new HttpUri($baseUrl); } if (!$baseUrl instanceof iHttpUri) { throw new \InvalidArgumentException(sprintf('BaseUrl must instance of iHttpUri, UriInterface or string. given: "%s"', \Poirot\Std\flatten($baseUrl))); } $this->baseUrl = $baseUrl; return $this; }
/** * @override can give uri objects * Server Url That we Will Connect To * * @param iHttpUri|UriInterface|string $serverUrl * * @return $this */ public function setServerUrl($serverUrl) { if ($serverUrl instanceof UriInterface) { $serverUrl = new HttpUri($serverUrl); } if ($serverUrl instanceof iHttpUri) { $serverUrl->toString(); } if (is_object($serverUrl)) { $serverUrl = (string) $serverUrl; } if (!is_string($serverUrl)) { throw new \InvalidArgumentException(sprintf('Server Url must instance of iHttpUri, UriInterface or string representing url address. given: "%s".', \Poirot\Std\flatten($serverUrl))); } $this->serverUrl = $serverUrl; return $this; }
/** * Create Service * @return mixed * @throws \Exception */ function createService() { $assetic = new Assetic(); /** @var Config $config */ $config = $this->services()->get('sapi')->config(); $config = $config->get('asset_manager', []); if (isset($config['resolver'])) { $resolver = $config['resolver']; ## resolver as a service if (is_string($resolver)) { $resolver = class_exists($resolver) ? new $resolver() : $this->services()->get($resolver); } if (!$resolver instanceof iLoader) { throw new \Exception(sprintf('Asset Manager Resolver Must Instance of iAssetManager, given: %s.', \Poirot\Std\flatten($resolver))); } $assetic->setResolver($resolver); } $resolver = $assetic->resolver(); $resClass = get_class($resolver); if (isset($config[$resClass]) && method_exists($resolver, 'from')) { $resolver->from($config[$resClass]); } return $assetic; }
/** * Send Expression On the wire * * - be aware if you pass streamable it will continue from current * not rewind implemented here * * !! get expression from getRequest() * * @throws ApiCallException|ConnectException * @return string Response */ final function doSend() { # prepare new request $this->lastReceive = null; # write stream try { ## prepare expression before send $expr = $this->getRequest(); $expr = $this->onBeforeSendPrepareExpression($expr); if (is_object($expr) && !$expr instanceof iStreamable) { $expr = (string) $expr; } if (is_string($expr)) { $expr = (new Streamable\TemporaryStream($expr))->rewind(); } if (!$expr instanceof iStreamable) { throw new \InvalidArgumentException(sprintf('Http Expression must instance of iHttpRequest, RequestInterface or string. given: "%s".', \Poirot\Std\flatten($expr))); } $response = $this->__handleReqRes($expr); } catch (\Exception $e) { throw new ApiCallException(sprintf('Request Call Error When Send To Server (%s)', $this->optsData()->getServerUrl()), 0, 1, __FILE__, __LINE__, $e); } $this->lastReceive = $response; return $response; }
/** * Set Request Uri To Server * * @param iHttpUri|UriInterface|iSeqPathUri|string $uri * @return $this */ public function setUri($uri) { if (is_string($uri) || $uri instanceof UriInterface) { $uri = new HttpUri($uri); if (!$uri->getScheme() && !$uri->getHost()) { ## this is sequent path $uri = $uri->getPath(); } } if (!$uri instanceof iHttpUri && !$uri instanceof iSeqPathUri) { throw new \InvalidArgumentException(sprintf('Uri must instance of iHttpUri, UriInterface, iSeqPathUri or string. given: "%s"', \Poirot\Std\flatten($uri))); } $this->uri = $uri; return $this; }
/** * Validate Plugin Instance Object * * @param mixed $pluginInstance * * @throws ContainerInvalidPluginException * @return void */ function validatePlugin($pluginInstance) { if (!$pluginInstance instanceof iBrowserPlugin) { throw new ContainerInvalidPluginException(sprintf('Invalid Plugin Provided For (%s).', \Poirot\Std\flatten($pluginInstance))); } }