예제 #1
0
 /**
  * Setup
  * Setup the request and dispatch it to Guzzle
  *
  * @access protected
  * @param [type] $method [description]
  */
 protected function setUp($method = null, $body = null, $additional_headers = array())
 {
     //has the request already been dispatched to Guzzle?
     if ($this->hasBeenDispatched() === true) {
         throw new \ErrorException('The request has already been dispatched to Guzzle!');
     }
     //set the method as a property to our users can access it later
     $this->method = $method;
     //we have to have a url before dispatching a request
     if ($this->getUrl() === false) {
         throw new \ErrorException('The url must be set before trying to dispatch a request!');
     }
     //if the developer hasn't set a client lets set one for them
     if (is_null($this->getGuzzleClient())) {
         $config = array();
         if ($this->useAuth === true) {
             $config['defaults']['auth'] = array($this->getUsername(), $this->getPassword());
         }
         if (count($additional_headers) > 0) {
             //this means the developer has specified addtional headers
             //lets pop them into the array
             $config['defaults']['headers'] = $additional_headers;
         }
         $this->setGuzzleClient(new Client($config));
     }
     //should we use the Glow complete event
     if ($this->getUseCompleteEvent() === true) {
         $this->guzzleClient->getEmitter()->on('complete', function (CompleteEvent $e) {
             if ($this->errorOccurred === false) {
                 $this->guzzleResponse = $e->getResponse();
                 $this->guzzleCurlInfo = $e->getTransferInfo();
             }
         });
     }
     //should we use the Glow error event
     if ($this->getUseErrorEvent() === true) {
         $this->guzzleClient->getEmitter()->on('error', function (ErrorEvent $e) {
             $this->guzzleResponse = $e->getResponse();
             $this->guzzleCurlInfo = $e->getTransferInfo();
             $this->errorOccurred = true;
             $this->guzzleException = $e->getException();
         });
     }
     //should we track history
     if ($this->getUseHistory() === true) {
         $this->guzzleClient->getEmitter()->on('before', function (BeforeEvent $e) {
             $this->trackedUrls[] = $e->getRequest()->getUrl();
         });
         $this->guzzleHistory = new History();
     }
     //should we use a cookie jar?
     if ($this->getUseCookieJar() === true) {
         //if the developer didn't send us a cookie jar lets create one
         if (is_null($this->getGuzzleCookieJar())) {
             $this->setGuzzleCookieJar(new Cookie());
         }
     }
     //start our subscribers array
     $subscribers['subscribers'] = array();
     //are we subscribing history?
     if ($this->getUseHistory() === true) {
         $subscribers['subscribers'][] = $this->getGuzzleHistory();
     }
     //are we subscribing cookies?
     if ($this->getUseCookieJar() === true) {
         $subscribers['subscribers'][] = $this->getGuzzleCookieJar();
     }
     //if we didn't add any subscribers lets reset our subscribers array to base array
     if (count($subscribers['subscribers']) == 0) {
         $subscribers = array();
     }
     $options = array();
     if (!is_null($body)) {
         $options = array_merge($subscribers, array('body' => $body));
     } else {
         $options = $subscribers;
     }
     //setup our Guzzle Request Object
     $this->guzzleRequest = $this->guzzleClient->createRequest($this->getMethod(), $this->getUrl(), $options);
     //did the developer specify a custom useragent?
     if (!is_null($this->getUserAgent())) {
         $this->guzzleRequest->setHeader('User-Agent', $this->getUserAgent());
     }
     //dispatch the request
     try {
         $this->guzzleClient->send($this->guzzleRequest);
     } catch (\Exception $e) {
         $this->guzzleException = $e;
         $this->errorOccurred = true;
         //if throwing Guzzle exceptions is on lets rethrow the exception
         if ($this->throwGuzzleExceptions === true) {
             throw $e;
         }
     }
     //update our dispatch property
     $this->beenDispatched = true;
     return $this;
 }
예제 #2
0
파일: Builder.php 프로젝트: k-gun/oppa
 /**
  * Add select statement.
  * @param  any    $field
  * @param  bool   $reset
  * @param  string $alias (for sub-select)
  * @return self
  * @throws Oppa\InvalidValueException
  */
 public final function select($field = null, bool $reset = true, string $alias = null) : self
 {
     $reset && $this->reset();
     // handle other query object
     if ($field instanceof $this) {
         if (empty($alias)) {
             throw new InvalidValueException('Alias is required!');
         }
         return $this->push('select', sprintf('(%s) AS %s', $field->toString(), $alias));
     }
     // handle json select
     if (is_array($field)) {
         return $this->push('select', join('', $field));
     }
     // pass for aggregate method, e.g select().aggregate('count', 'id')
     if (empty($field)) {
         $field = ['1'];
     }
     return $this->push('select', trim($field, ', '));
 }