Пример #1
0
 /**
  * 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;
 }
Пример #2
0
 /**
  * @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;
 }
Пример #3
0
 /**
  * Build Platform Specific Expression To Send
  * Trough Connection
  *
  * @param iApiMethod|ReqMethod $ReqMethod Method Interface
  *
  * @return HttpRequest
  */
 function makeExpression(iApiMethod $ReqMethod)
 {
     ## make a copy of browser when making changes on it by ReqMethod
     ### with bind browser options
     $CUR_BROWSER = $this->browser;
     $this->browser = clone $CUR_BROWSER;
     if (!$ReqMethod instanceof ReqMethod) {
         $ReqMethod = new ReqMethod($ReqMethod->toArray());
     }
     # Request Options:
     ## (1)
     /*
      * $browser->POST('/api/v1/auth/login', [
      *      'form_data' => [
      *      // ...
      */
     if ($ReqMethod->getBrowser()) {
         ## Browser specific options
         $prepConn = false;
         foreach ($ReqMethod->getBrowser()->props()->readable as $prop) {
             if ($ReqMethod->getBrowser()->__isset($prop)) {
                 $this->browser->optsData()->__set($prop, $ReqMethod->getBrowser()->__get($prop));
                 $prepConn = true;
             }
         }
         ## prepare connection again with new configs
         !$prepConn ?: $this->prepareTransporter($this->_connection, true);
     }
     // ...
     if ($ReqMethod->getUri() instanceof iHttpUri) {
         ## Reset Server Base Url When Absolute Http URI Requested
         /*
          * $browser->get(
          *   'http://www.pasargad-co.ir/forms/contact'
          *   , [ 'connection' => ['time_out' => 30],
          *     // ...
          */
         $t_uri = $ReqMethod->getUri();
         if ($t_uri->getHost()) {
             $this->browser->optsData()->setBaseUrl($t_uri);
             $this->prepareTransporter($this->_connection);
         }
         ### continue with sequence http uri
         $t_uri = $ReqMethod->getUri()->getPath() ? $ReqMethod->getUri()->getPath() : new SeqPathJoinUri('/');
         $ReqMethod->setUri($t_uri);
     }
     # Build Request Http Message:
     ## (2)
     $REQUEST = $this->__getRequestObject();
     $REQUEST->setMethod($ReqMethod->getMethod());
     $serverUrl = $this->_connection->inOptions()->getServerUrl();
     $serverUrl = new HttpUri($serverUrl);
     $REQUEST->setHost($serverUrl->getHost());
     ## req Headers ------------------------------------------------------------------\
     ### default headers
     $reqHeaders = $REQUEST->getHeaders();
     if ($this->browser->optsData()->__isset('user_agent')) {
         $reqHeaders->set(HeaderFactory::factory('User-Agent', $this->browser->optsData()->getUserAgent()));
     }
     $reqHeaders->set(HeaderFactory::factory('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'));
     $reqHeaders->set(HeaderFactory::factory('Cache-Control', 'no-cache'));
     /*if ($this->browser->inOptions()->getConnection())
       (!$this->browser->inOptions()->getConnection()->isAllowDecoding())
           ?: $reqHeaders->set(HeaderFactory::factory('Accept-Encoding'
           , 'gzip, deflate, sdch'
       ));*/
     ### headers as default browser defined header
     foreach ($this->browser->optsData()->getRequest()->getHeaders() as $h) {
         $reqHeaders->set($h);
     }
     ### headers as request method options
     if ($ReqMethod->getHeaders()) {
         /** @var iHeader $h */
         foreach ($ReqMethod->getHeaders() as $h) {
             if ($reqHeaders->has($h->getLabel())) {
                 $reqHeaders = $reqHeaders->del($h->getLabel());
             }
             $reqHeaders->set($h);
         }
     }
     ## req Uri ----------------------------------------------------------------------\
     $basePath = null;
     if ($this->browser->optsData()->__isset('base_url')) {
         $basePath = $this->browser->optsData()->getBaseUrl()->getPath();
     }
     if ($basePath === null) {
         $basePath = new SeqPathJoinUri('/');
     }
     $targetUri = $basePath->merge($ReqMethod->getUri());
     ### merge with request base url path
     $REQUEST->getUri()->setPath($targetUri);
     ### remove unnecessary iHttpUri parts such as port, host, ...
     ### its presented as host and etc. on Request Message Object
     $uri = new HttpUri(['path' => $REQUEST->getUri()->getPath(), 'query' => $REQUEST->getUri()->getQuery(), 'fragment' => $REQUEST->getUri()->getFragment()]);
     $REQUEST->setUri($uri);
     ## req body ---------------------------------------------------------------------\
     $REQUEST->setBody($ReqMethod->getBody());
     # Implement Browser Plugins:
     ## (3)
     foreach ($ReqMethod->getBrowser()->props()->readable as $prop) {
         if (!$this->getPluginManager()->has($prop)) {
             /*
              * $browser->POST('/api/v1/auth/login', [
              *      'form_data' => [ // <=== plugin form_data will trigger with this params
              *      // ...
              */
             continue;
         }
         ## no plugin bind on this option
         /** @var Browser\Plugin\AbstractBrowserPlugin $plugin */
         $plugin = $this->getPluginManager()->fresh($prop);
         $plugin->from($ReqMethod->getBrowser()->__get($prop));
         ### options for service
         if ($plugin instanceof iBrowserExpressionPlugin) {
             $plugin->withHttpRequest($REQUEST);
         }
     }
     $this->browser = $CUR_BROWSER;
     return $REQUEST;
 }