Пример #1
0
 /**
  * Handle all static helpers.
  * @return array
  */
 protected static function make($verb, $urls, $data, $callback)
 {
     if (!is_array($urls)) {
         $urls = array($urls => $data);
     } elseif (!(bool) count(array_filter(array_keys($urls), 'is_string'))) {
         foreach ($urls as $key => $url) {
             $urls[$url] = null;
             unset($urls[$key]);
         }
     }
     $dispatcher = new Dispatcher();
     $requests = array();
     $dataStore = array();
     foreach ($urls as $url => $data) {
         $requests[] = new Request($url);
         $dataStore[] = $data;
     }
     new static($verb, $dispatcher, $requests, $dataStore, $callback);
     $requests = $dispatcher->all();
     $responses = array();
     foreach ($requests as $request) {
         $responses[] = $request->getResponse();
     }
     return $responses;
 }
Пример #2
0
 public function testExecute()
 {
     $dispatcher = new Dispatcher();
     $request1 = m::mock('jyggen\\Curl\\Request', array('http://example.com/'))->shouldDeferMissing();
     $request2 = m::mock('jyggen\\Curl\\Request', array('http://example.org/'))->shouldDeferMissing();
     $request1->shouldReceive('execute')->andReturn(true);
     $request2->shouldReceive('execute')->andReturn(true);
     $request1->shouldReceive('getRawResponse')->andReturnUsing(function () use($request1) {
         return curl_multi_getcontent($request1->getHandle());
     });
     $request2->shouldReceive('getRawResponse')->andReturnUsing(function () use($request2) {
         return curl_multi_getcontent($request2->getHandle());
     });
     $dispatcher->add($request1);
     $dispatcher->add($request2);
     $dispatcher->execute();
     $response1 = $request1->getRawResponse();
     $response2 = $request2->getRawResponse();
     $this->assertStringStartsWith('HTTP/1.1 200 OK', $response1);
     $this->assertStringStartsWith('HTTP/1.1 200 OK', $response2);
 }
Пример #3
0
 public function getDocumentByURI($args)
 {
     $uris = is_array($args) ? $args : [$args];
     $dp = new Dispatcher();
     $requests = $documents = [];
     foreach ($uris as $uri) {
         $requests[] = new Request('http://' . self::DOMAIN . '/' . $uri);
         $dp->add(end($requests));
     }
     $dp->execute();
     foreach ($requests as $request) {
         $raw = substr($request->getRawResponse(), $request->getInfo(CURLINFO_HEADER_SIZE));
         if ($request->getResponse()->headers->get('location')) {
             throw new RedirectException();
         } else {
             if (!$raw) {
                 throw new URIException();
             }
         }
         $documents[] = new Document($raw);
     }
     return is_array($args) ? $documents : $documents[0];
 }
Пример #4
0
 /**
  * Handle all static helpers.
  * @param  mixed $name
  * @param  array $arguments
  * @return mixed
  */
 public static function __callStatic($name, $arguments)
 {
     $allowedVerbs = array('delete', 'get', 'post', 'put');
     if (in_array($name, $allowedVerbs)) {
         // We require at least one URL.
         if (!isset($arguments[0])) {
             $message = sprintf('Missing argument 1 for %s::%s()', get_called_class(), $name);
             throw new InvalidArgumentException($message);
         }
         $urls = $arguments[0];
         $data = isset($arguments[1]) ? $arguments[1] : null;
         if (!is_array($urls)) {
             $urls = array($urls => $data);
         } elseif (!(bool) count(array_filter(array_keys($urls), 'is_string'))) {
             foreach ($urls as $key => $url) {
                 $urls[$url] = null;
                 unset($urls[$key]);
             }
         }
         $dispatcher = new Dispatcher();
         $requests = array();
         $dataStore = array();
         foreach ($urls as $url => $data) {
             $requests[] = new Request($url);
             $dataStore[] = $data;
         }
         new static($name, $dispatcher, $requests, $dataStore);
         $requests = $dispatcher->get();
         $responses = array();
         foreach ($requests as $request) {
             $responses[] = $request->getResponse();
         }
         return $responses;
     } else {
         throw new BadMethodCallException(sprintf('Call to undefined method %s::%s()', get_called_class(), $name));
     }
 }