Start integration of PSR 7 Request/Response

This commit is contained in:
Timothy Warren 2016-02-16 16:28:44 -05:00
parent 1ba302999d
commit 98f3026a74
7 changed files with 60 additions and 57 deletions

View File

@ -6,12 +6,13 @@
namespace Aviat\AnimeClient; namespace Aviat\AnimeClient;
use Aura\Html\HelperLocatorFactory; use Aura\Html\HelperLocatorFactory;
use Aura\Web\WebFactory; use Aura\Router\RouterContainer;
use Aura\Router\RouterFactory;
use Aura\Session\SessionFactory; use Aura\Session\SessionFactory;
use Monolog\Logger; use Monolog\Logger;
use Monolog\Handler\RotatingFileHandler; use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\BrowserConsoleHandler; use Monolog\Handler\BrowserConsoleHandler;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Response;
use Aviat\Ion\Di\Container; use Aviat\Ion\Di\Container;
use Aviat\AnimeClient\Auth\HummingbirdAuth; use Aviat\AnimeClient\Auth\HummingbirdAuth;
@ -40,8 +41,7 @@ return function(array $config_array = []) {
$container->set('config', $config); $container->set('config', $config);
// Create Aura Router Object // Create Aura Router Object
$aura_router = (new RouterFactory())->newInstance(); $container->set('aura-router', new RouterContainer);
$container->set('aura-router', $aura_router);
// Create Html helper Object // Create Html helper Object
$html_helper = (new HelperLocatorFactory)->newInstance(); $html_helper = (new HelperLocatorFactory)->newInstance();
@ -53,15 +53,15 @@ return function(array $config_array = []) {
$container->set('html-helper', $html_helper); $container->set('html-helper', $html_helper);
// Create Request/Response Objects // Create Request/Response Objects
$web_factory = new WebFactory([ $request = ServerRequestFactory::fromGlobals(
'_GET' => $_GET, $_SERVER,
'_POST' => $_POST, $_GET,
'_COOKIE' => $_COOKIE, $_POST,
'_SERVER' => $_SERVER, $_COOKIE,
'_FILES' => $_FILES $_FILES
]); );
$container->set('request', $web_factory->newRequest()); $container->set('request', $request);
$container->set('response', $web_factory->newResponse()); $container->set('response', new Response());
// Create session Object // Create session Object
$session = (new SessionFactory())->newInstance($_COOKIE); $session = (new SessionFactory())->newInstance($_COOKIE);

View File

@ -5,18 +5,19 @@
"require": { "require": {
"abeautifulsite/simpleimage": "2.5.*", "abeautifulsite/simpleimage": "2.5.*",
"aura/html": "2.*", "aura/html": "2.*",
"aura/router": "2.2.*", "aura/router": "3.*",
"aura/session": "2.*", "aura/session": "2.*",
"aura/web": "2.*",
"aviat4ion/query": "2.5.*", "aviat4ion/query": "2.5.*",
"container-interop/container-interop": "1.*", "container-interop/container-interop": "1.*",
"danielstjules/stringy": "~2.1", "danielstjules/stringy": "~2.1",
"filp/whoops": "2.0.*", "filp/whoops": "2.0.*",
"guzzlehttp/guzzle": "6.*", "guzzlehttp/guzzle": "6.*",
"monolog/monolog": "1.*", "monolog/monolog": "1.*",
"psr/http-message": "~1.0",
"psr/log": "~1.0", "psr/log": "~1.0",
"robmorgan/phinx": "0.4.*", "robmorgan/phinx": "0.4.*",
"yosymfony/toml": "0.3.*" "yosymfony/toml": "0.3.*",
"zendframework/zend-diactoros": "1.3.*"
}, },
"require-dev": { "require-dev": {
"codeclimate/php-test-reporter": "dev-master" "codeclimate/php-test-reporter": "dev-master"

View File

@ -95,7 +95,7 @@ class Controller {
$this->session = $session->getSegment(AnimeClient::SESSION_SEGMENT); $this->session = $session->getSegment(AnimeClient::SESSION_SEGMENT);
// Set a 'previous' flash value for better redirects // Set a 'previous' flash value for better redirects
$this->session->setFlash('previous', $this->request->server->get('HTTP_REFERER')); $this->session->setFlash('previous', $this->request->getServerParams()['HTTP_REFERER']);
// Set a message box if available // Set a message box if available
$this->base_data['message'] = $this->session->getFlash('message'); $this->base_data['message'] = $this->session->getFlash('message');
@ -130,7 +130,7 @@ class Controller {
public function set_session_redirect($url = NULL) public function set_session_redirect($url = NULL)
{ {
$anime_client = $this->container->get('anime-client'); $anime_client = $this->container->get('anime-client');
$double_form_page = $this->request->server->get('HTTP_REFERER') == $this->request->url->get(); $double_form_page = $this->request->getServerParams()['HTTP_REFERER'] == $this->request->getUri();
// Don't attempt to set the redirect url if // Don't attempt to set the redirect url if
// the page is one of the form type pages, // the page is one of the form type pages,
@ -144,7 +144,7 @@ class Controller {
{ {
$url = ($anime_client->is_view_page()) $url = ($anime_client->is_view_page())
? $this->request->url->get() ? $this->request->url->get()
: $this->request->server->get('HTTP_REFERER'); : $this->request->getServerParams()['HTTP_REFERER'];
} }
$this->session->set('redirect_url', $url); $this->session->set('redirect_url', $url);

View File

@ -29,6 +29,12 @@ class Dispatcher extends RoutingBase {
*/ */
protected $router; protected $router;
/**
* The route matcher
* @var object $matcher
*/
protected $matcher;
/** /**
* Class wrapper for input superglobals * Class wrapper for input superglobals
* @var object * @var object
@ -49,7 +55,8 @@ class Dispatcher extends RoutingBase {
public function __construct(ContainerInterface $container) public function __construct(ContainerInterface $container)
{ {
parent::__construct($container); parent::__construct($container);
$this->router = $container->get('aura-router'); $this->router = $container->get('aura-router')->getMap();
$this->matcher = $container->get('aura-router')->getMatcher();
$this->request = $container->get('request'); $this->request = $container->get('request');
$this->output_routes = $this->_setup_routes(); $this->output_routes = $this->_setup_routes();
@ -64,7 +71,7 @@ class Dispatcher extends RoutingBase {
{ {
$logger = $this->container->getLogger('default'); $logger = $this->container->getLogger('default');
$raw_route = $this->request->url->get(PHP_URL_PATH); $raw_route = $this->request->getUri()->getPath();
$route_path = "/" . trim($raw_route, '/'); $route_path = "/" . trim($raw_route, '/');
$logger->debug('Dispatcher - Routing data from get_route method'); $logger->debug('Dispatcher - Routing data from get_route method');
@ -72,7 +79,7 @@ class Dispatcher extends RoutingBase {
'route_path' => $route_path 'route_path' => $route_path
], TRUE)); ], TRUE));
return $this->router->match($route_path, $_SERVER); return $this->matcher->match($this->request);
} }
/** /**
@ -134,9 +141,9 @@ class Dispatcher extends RoutingBase {
*/ */
protected function process_route($route) protected function process_route($route)
{ {
if (array_key_exists('controller', $route->params)) if (array_key_exists('controller', $route->attributes))
{ {
$controller_name = $route->params['controller']; $controller_name = $route->attributes['controller'];
} }
else else
{ {
@ -150,21 +157,21 @@ class Dispatcher extends RoutingBase {
$controller_name = $map[$controller_name]; $controller_name = $map[$controller_name];
} }
$action_method = (array_key_exists('action', $route->params)) $action_method = (array_key_exists('action', $route->attributes))
? $route->params['action'] ? $route->attributes['action']
: AnimeClient::NOT_FOUND_METHOD; : AnimeClient::NOT_FOUND_METHOD;
$params = (array_key_exists('params', $route->params)) $params = (array_key_exists('params', $route->attributes))
? $route->params['params'] ? $route->attributes['params']
: []; : [];
if ( ! empty($route->tokens)) if ( ! empty($route->tokens))
{ {
foreach ($route->tokens as $key => $v) foreach ($route->tokens as $key => $v)
{ {
if (array_key_exists($key, $route->params)) if (array_key_exists($key, $route->attributes))
{ {
$params[$key] = $route->params[$key]; $params[$key] = $route->attributes[$key];
} }
} }
} }
@ -184,7 +191,7 @@ class Dispatcher extends RoutingBase {
public function get_controller() public function get_controller()
{ {
$route_type = $this->__get('default_list'); $route_type = $this->__get('default_list');
$request_uri = $this->request->url->get(PHP_URL_PATH); $request_uri = $this->request->getUri()->getPath();
$path = trim($request_uri, '/'); $path = trim($request_uri, '/');
$segments = explode('/', $path); $segments = explode('/', $path);
@ -323,15 +330,15 @@ class Dispatcher extends RoutingBase {
// Select the appropriate router method based on the http verb // Select the appropriate router method based on the http verb
$add = (array_key_exists('verb', $route)) $add = (array_key_exists('verb', $route))
? "add" . ucfirst(strtolower($route['verb'])) ? strtolower($route['verb'])
: "addGet"; : "get";
// Add the route to the router object // Add the route to the router object
if ( ! array_key_exists('tokens', $route)) //if ( ! array_key_exists('tokens', $route))
{ {
$routes[] = $this->router->$add($name, $path)->addValues($route); $routes[] = $this->router->$add($name, $path);//->addValues($route);
} }
else /*else
{ {
$tokens = $route['tokens']; $tokens = $route['tokens'];
unset($route['tokens']); unset($route['tokens']);
@ -339,7 +346,7 @@ class Dispatcher extends RoutingBase {
$routes[] = $this->router->$add($name, $path) $routes[] = $this->router->$add($name, $path)
->addValues($route) ->addValues($route)
->addTokens($tokens); ->addTokens($tokens);
} }*/
} }
return $routes; return $routes;

View File

@ -32,7 +32,7 @@ class UrlGenerator extends RoutingBase {
public function __construct(ContainerInterface $container) public function __construct(ContainerInterface $container)
{ {
parent::__construct($container); parent::__construct($container);
$this->host = $container->get('request')->server->get('HTTP_HOST'); $this->host = $container->get('request')->getServerParams()['HTTP_HOST'];
} }
/** /**

View File

@ -26,7 +26,7 @@ abstract class View {
/** /**
* HTTP response Object * HTTP response Object
* *
* @var Aura\Web\Response * @var Zend\Diactoros\Response
*/ */
protected $response; protected $response;
@ -93,7 +93,7 @@ abstract class View {
*/ */
public function setOutput($string) public function setOutput($string)
{ {
$this->output = $this->string($string); $this->response->getBody()->write($string);
return $this; return $this;
} }
@ -106,7 +106,7 @@ abstract class View {
*/ */
public function appendOutput($string) public function appendOutput($string)
{ {
$this->output = $this->string($this->output)->append($string); $this->response->getBody()->write($string);
return $this; return $this;
} }
@ -118,7 +118,7 @@ abstract class View {
*/ */
public function getOutput() public function getOutput()
{ {
return $this->string($this->output)->__toString(); return $this->response->getBody()->__toString();
} }
/** /**

View File

@ -12,8 +12,7 @@
namespace Aviat\Ion\View; namespace Aviat\Ion\View;
use Aura\Web\ResponseSender; use Zend\Diactoros\Response\SapiEmitter;
use Aviat\Ion\View as BaseView; use Aviat\Ion\View as BaseView;
/** /**
@ -41,8 +40,8 @@ class HttpView extends BaseView {
*/ */
public function setStatusCode($code) public function setStatusCode($code)
{ {
$this->response->status->setCode($code); $this->response->withStatus($code);
$this->response->status->setVersion(1.1); $this->response->withProtocolVersion(1.1);
return $this; return $this;
} }
@ -65,18 +64,14 @@ class HttpView extends BaseView {
*/ */
protected function output() protected function output()
{ {
$this->response->headers->set('Content-Security-Policy', "script-src 'self'"); $this->response->withHeader('Content-type', "{$this->contentType};charset=utf-8")
$this->response->headers->set('X-Content-Type-Options', 'nosniff'); ->withHeader('Content-Security-Policy', "script-src 'self'")
$this->response->headers->set('X-XSS-Protection', '1;mode=block'); ->withHeader('X-Content-Type-Options', 'nosniff')
$this->response->headers->set('X-Frame-Options', 'SAMEORIGIN'); ->withHeader('X-XSS-Protection', '1;mode=block')
->withHeader('X-Frame-Options', 'SAMEORIGIN');
$content =& $this->response->content; $sender = new SapiEmitter($this->response);
$content->set($this->output); $sender->emit();
$content->setType($this->contentType);
$content->setCharset('utf-8');
$sender = new ResponseSender($this->response);
$sender->__invoke();
} }
} }