Skip to content
Permalink
Browse files

Use instances of Network\Request everywhere.

Now that Network\Request implements the PSR7 interface we can use it
instead of the diactoros implementation. Long term I'd like to replace
most of our code with the diactoros version. However, because of
backwards compatibility issues that will not be possible in 3.x.
  • Loading branch information...
markstory committed Sep 30, 2016
1 parent c94eef2 commit 45ca5d0337cae62c87ae2172c961a84c629b0dd6
@@ -64,7 +64,7 @@ public function bootstrap()
/**
* Invoke the application.
*
* - Convert the PSR request/response into CakePHP equivalents.
* - Convert the PSR response into CakePHP equivalents.
* - Create the controller that will handle this request.
* - Invoke the controller.
*
@@ -75,12 +75,10 @@ public function bootstrap()
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
{
// Convert the request/response to CakePHP equivalents.
$cakeRequest = RequestTransformer::toCake($request);
$cakeResponse = ResponseTransformer::toCake($response);
// Dispatch the request/response to CakePHP
$cakeResponse = $this->getDispatcher()->dispatch($cakeRequest, $cakeResponse);
$cakeResponse = $this->getDispatcher()->dispatch($request, $cakeResponse);
// Convert the response back into a PSR7 object.
return ResponseTransformer::toPsr($cakeResponse);
@@ -73,15 +73,7 @@ public function run(ServerRequestInterface $request = null, ResponseInterface $r
{
$this->app->bootstrap();
$response = $response ?: new Response();
try {
$request = $request ?: ServerRequestFactory::fromGlobals();
} catch (UnexpectedValueException $e) {
$response->getBody()->write('Bad Request');
return $response
->withHeader('Content-Type', 'text/plain')
->withStatus(400);
}
$request = $request ?: ServerRequestFactory::fromGlobals();
$middleware = $this->app->middleware(new MiddlewareQueue());
if (!($middleware instanceof MiddlewareQueue)) {
@@ -15,6 +15,7 @@
namespace Cake\Http;
use Cake\Core\Configure;
use Cake\Network\Request;
use Cake\Network\Session;
use Cake\Utility\Hash;
use Zend\Diactoros\ServerRequestFactory as BaseFactory;
@@ -38,18 +39,25 @@ public static function fromGlobals(
array $cookies = null,
array $files = null
) {
$request = parent::fromGlobals($server, $query, $body, $cookies, $files);
$uri = $request->getUri();
$server = static::normalizeServer($server ?: $_SERVER);
$uri = static::createUri($server);
$sessionConfig = (array)Configure::read('Session') + [
'defaults' => 'php',
'cookiePath' => $uri->webroot
];
$session = Session::create($sessionConfig);
$request = $request->withAttribute('base', $uri->base)
->withAttribute('webroot', $uri->webroot)
->withAttribute('session', $session);
$request = new Request([
'environment' => $server,
'uri' => $uri,
'files' => $files,
'input' => 'php://input',
'cookies' => $cookies ?: $_COOKIE,
'query' => $query ?: $_GET,
'post' => $body ?: $_POST,
'webroot' => $uri->webroot,
'base' => $uri->base,
'session' => $session,
]);
return $request;
}
@@ -217,31 +217,11 @@ class Request implements ArrayAccess, ServerRequestInterface
* the request.
*
* @return \Cake\Network\Request
* @deprecated 3.4.0 Use `Cake\Http\ServerRequestFactory` instead.
*/
public static function createFromGlobals()
{
$uri = ServerRequestFactory::createUri($_SERVER);
$base = $uri->base;
$webroot = $uri->webroot;
$sessionConfig = (array)Configure::read('Session') + [
'defaults' => 'php',
'cookiePath' => $webroot
];
$config = [
'query' => $_GET,
'post' => $_POST,
'files' => $_FILES,
'cookies' => $_COOKIE,
'environment' => $_SERVER + $_ENV,
'uri' => $uri,
'base' => $base,
'webroot' => $webroot,
'session' => Session::create($sessionConfig)
];
return new static($config);
return ServerRequestFactory::fromGlobals();
}
/**
@@ -256,15 +236,16 @@ public static function createFromGlobals()
* - `files` Uploaded file data formatted like $_FILES.
* - `cookies` Cookies for this request.
* - `environment` $_SERVER and $_ENV data.
* - `url` The URL without the base path for the request.
* - ~~`url`~~ The URL without the base path for the request. This option is deprecated and will be removed in 4.0.0
* - `uri` The PSR7 UriInterface object. If null, one will be created.
* - `base` The base URL for the request.
* - `webroot` The webroot directory for the request.
* - `input` The data that would come from php://input this is useful for simulating
* - `session` An instance of a Session object
* requests with put, patch or delete data.
* - `session` An instance of a Session object
*
* @param string|array $config An array of request data to create a request with.
* The string version of this argument is *deprecated* and will be removed in 4.0.0
*/
public function __construct($config = [])
{
@@ -116,24 +116,6 @@ public function testRunWithGlobals()
);
}
/**
* test run where the protocol is invalid
*
* @return void
*/
public function testRunInvalidProtocol()
{
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/onclick 1=1';
$app = new MiddlewareApplication($this->config);
$server = new Server($app);
$res = $server->run();
$this->assertEquals(400, $res->getStatusCode());
$this->assertEquals('text/plain', $res->getHeaderLine('content-type'));
$this->assertEquals('Bad Request', '' . $res->getBody());
}
/**
* Test an application failing to build middleware properly
*

0 comments on commit 45ca5d0

Please sign in to comment.
You can’t perform that action at this time.