Requests are considered immutable; all methods that might change state are implemented such that they retain the internal state of the current message and return a new instance that contains the changed state.
Inheritance: implements Psr\Http\Message\RequestInterface, use trait MessageTrait, use trait RequestTrait
 /**
  * Makes PSR-7 compliant Request object from provided parameters
  *
  * @param UriInterface|string $uri
  * @param null|string|array $data
  * @param array $queryParameters
  * @param array $headers
  * @param null|string $method
  * @param null|string $contentType
  * @param null|string $contentCharset
  * @return RequestInterface
  * @throws InvalidArgumentException
  */
 public static function make($uri, $data = null, array $queryParameters = [], array $headers = [], $method = null, $contentType = null, $contentCharset = null)
 {
     if (is_null($method)) {
         $method = is_null($data) ? static::METHOD_GET : static::METHOD_POST;
     }
     $uri = UriFactory::make($uri, $queryParameters);
     $stream = new Stream('php://memory', 'r+');
     $request = new Request($uri, $method, $stream, $headers);
     $contentType = $contentType ?: $request->getHeaderLine('Content-Type');
     $contentType = $contentType ?: static::DEFAULT_CONTENT_TYPE;
     $contentTypeParts = explode(';', $contentType);
     $mimeType = trim(array_shift($contentTypeParts));
     if (is_null($data)) {
         $body = null;
     } elseif (is_string($data)) {
         $body = $data;
     } elseif (is_array($data)) {
         $body = static::format($data, $mimeType);
     } else {
         throw new InvalidArgumentException('Data should be either null, string or array');
     }
     if (!is_null($body)) {
         $contentCharset = $contentCharset ?: static::extractCharset($contentTypeParts);
         $contentCharset = $contentCharset ?: static::DEFAULT_CONTENT_CHARSET;
         $contentTypeReassembled = $mimeType . '; charset=' . $contentCharset;
         $request = $request->withHeader('Content-Type', $contentTypeReassembled);
         $request->getBody()->write($body);
     }
     return $request;
 }
Esempio n. 2
0
 /**
  * Instantiate the controller, execute it and return a response.
  * @param $request Request the request.
  * @return  $response Response to send.
  * @throws ControllerNotExist The controller doesn't exist.
  * @throws PageNotExist No route found for the request uri.
  */
 public function execute(Request $request)
 {
     $method = $request->getMethod();
     $argPos = strpos($request->getUri(), '?');
     if ($argPos !== false && $argPos >= 0) {
         $uri = substr($request->getUri(), 0, $argPos);
     } else {
         $uri = $request->getUri();
     }
     $route = self::getRoute($method, $uri);
     if (!$route) {
         throw new PageNotExist('No route found');
     }
     $path = explode('@', $route['path']);
     $controller_name = $path[0];
     $action = $path[1];
     $args = $route['args'];
     // Try to instantiate the controller
     $controller = Config::$app['namespace'] . 'Controllers\\' . $controller_name;
     if (class_exists($controller)) {
         $controller = new $controller($request);
     } else {
         throw new ControllerNotExist('Controller "' . $controller . '" doesn\'t exist !');
     }
     return $controller->execute($action, $args);
 }
 public function testModifyRequest()
 {
     $request = new Request();
     $e = new CgiExecuteEvent('event', $request);
     $e->modifyRequest(function (RequestInterface $request) {
         return $request->withHeader('Content-Type', 'text/html')->withMethod('POST');
     });
     $this->assertSame(['Content-Type' => ['text/html']], $e->getRequest()->getHeaders());
     $this->assertSame('POST', $e->getRequest()->getMethod());
     $this->assertNotSame($request, $e->getRequest());
 }
Esempio n. 4
0
 /**
  * @test
  */
 public function to_sends_request()
 {
     $request = new Request('http://localhost/path?query=yes', 'GET');
     $url = 'https://www.example.com';
     $adapter = $this->getMockBuilder('Proxy\\Adapter\\Dummy\\DummyAdapter')->getMock();
     $verifyParam = $this->callback(function (RequestInterface $request) use($url) {
         return $request->getUri() == 'https://www.example.com/path?query=yes';
     });
     $adapter->expects($this->once())->method('send')->with($verifyParam)->willReturn(new Response());
     $proxy = new Proxy($adapter);
     $proxy->forward($request)->to($url);
 }
Esempio n. 5
0
 /**
  * @param Message|array $message
  * @return Message
  * @throws Exception\Exception
  * @throws Exception\Request
  * @throws Exception\Server
  */
 public function send($message)
 {
     if (!$message instanceof MessageInterface) {
         $message = $this->createMessageFromArray($message);
     }
     $params = $message->getRequestData(false);
     $request = new Request(\Nexmo\Client::BASE_REST . '/sms/json', 'POST', 'php://temp', ['content-type' => 'application/json']);
     $request->getBody()->write(json_encode($params));
     $message->setRequest($request);
     $response = $this->client->send($request);
     $message->setResponse($response);
     //check for valid data, as well as an error response from the API
     $data = $message->getResponseData();
     if (!isset($data['messages'])) {
         throw new Exception\Exception('unexpected response from API');
     }
     //normalize errors (client vrs server)
     foreach ($data['messages'] as $part) {
         switch ($part['status']) {
             case '0':
                 continue;
                 //all okay
             //all okay
             case '1':
                 if (preg_match('#\\[\\s+(\\d+)\\s+\\]#', $part['error-text'], $match)) {
                     usleep($match[1] + 1);
                 } else {
                     sleep(1);
                 }
                 return $this->send($message);
             case '5':
                 $e = new Exception\Server($part['error-text'], $part['status']);
                 $e->setEntity($message);
                 throw $e;
             default:
                 $e = new Exception\Request($part['error-text'], $part['status']);
                 $e->setEntity($message);
                 throw $e;
         }
     }
     return $message;
 }
Esempio n. 6
0
 /**
  * Create notify request object
  */
 public function __construct()
 {
     parent::__construct(sprintf('%s:%s', Client::MULTICAST_ADDRESS, Client::MULTICAST_PORT), self::METHOD);
 }
Esempio n. 7
0
 /**
  * @param null|string                     $uri     URI for the request, if any.
  * @param null|string                     $method  HTTP method for the request, if any.
  * @param string|resource|StreamInterface $body    Message body, if any.
  * @param array                           $headers Headers for the message, if any.
  *
  * @throws \InvalidArgumentException for any invalid value.
  */
 public function __construct($uri = null, $method = null, $body = 'php://input', array $headers = [])
 {
     parent::__construct($uri, $method, $body, $headers);
 }
Esempio n. 8
0
 /**
  * @param null|string|\Psr\Http\Message\UriInterface            $uri        The request uri.
  * @param null|string                                           $method     The request method.
  * @param string|resource|\Psr\Http\Message\StreamInterface $body       The request body.
  * @param array                                                 $headers    The request headers.
  * @param array                                                 $parameters The request parameters.
  */
 public function __construct($uri = null, $method = null, $body = 'php://memory', array $headers = array(), array $parameters = array())
 {
     parent::__construct($uri, $method, $body, $headers);
     $this->parameters = $parameters;
 }
Esempio n. 9
0
 protected function getRequest($params, $path = null)
 {
     if (!is_null($path)) {
         $path = '/verify/' . $path . '/json';
     } else {
         $path = '/verify/json';
     }
     $request = new Request(\Nexmo\Client::BASE_API . $path, 'POST', 'php://temp', ['content-type' => 'application/json']);
     $request->getBody()->write(json_encode($params));
     return $request;
 }