namespace IPF\Auth;
-class Middleware
+class Middleware extends \IPF_Middleware
{
const SessionKey = 'IPF_User_auth';
--- /dev/null
+<?php
+
+class IPF_Middleware
+{
+ protected $next, $settings;
+
+ function __construct($next, $settings)
+ {
+ $this->next = $next;
+ $this->settings = $settings;
+ }
+
+ function processRequest($request)
+ {
+ return false;
+ }
+
+ function processResponse($request, $response)
+ {
+ return $response;
+ }
+
+ function call($request)
+ {
+ $response = $this->processRequest($request);
+ if ($response !== false) {
+ return $response;
+ }
+
+ if ($this->next) {
+ $response = $this->next->call($request);
+ }
+
+ return $this->processResponse($request, $response);
+ }
+}
+
<?php
-class IPF_Middleware_Common
+class IPF_Middleware_Common extends IPF_Middleware
{
function processRequest($request)
{
- if (IPF::get('append_slash', true)) {
+ if ($this->settings->get('append_slash', true)) {
$url = $request->absoluteUrl();
- if (substr($url, -1) !== '/')
+ if (substr($url, -1) !== '/') {
return new IPF_HTTP_Response_Redirect($url.'/');
+ }
}
return false;
}
--- /dev/null
+<?php
+
+class IPF_Dispatch_Middleware extends IPF_Middleware
+{
+ function processRequest($request)
+ {
+ $router = \IPF_Project::getInstance()->router;
+
+ $m = $router->match($request);
+ if (!$m) {
+ return new IPF_HTTP_Response_NotFound($request);
+ }
+
+ list($route, $match) = $m;
+ $request->params = $match;
+
+ try {
+ return IPF::callFunction($route->func, array($request, $match));
+ } catch (IPF_Router_Shortcut $e) {
+ return $e->response($request);
+ }
+ }
+}
+
--- /dev/null
+<?php
+
+class IPF_Error_Middleware extends IPF_Middleware
+{
+ function call($request)
+ {
+ try {
+ $response = null;
+ if ($this->next) {
+ $response = $this->next->call($request);
+ }
+
+ if (!($response instanceof IPF_HTTP_Response)) {
+ throw new \Exception('Response in not a IPF_HTTP_Response');
+ }
+
+ return $response;
+ } catch (\Exception $exception) {
+ error_log($exception);
+ if ($this->settings->get('debug'))
+ return new IPF_HTTP_Response_ServerErrorDebug($request, $exception);
+ else
+ return new IPF_HTTP_Response_ServerError($request, $exception);
+ }
+ }
+}
+
<?php
-class IPF_Serve_Static_Middleware
+class IPF_Serve_Static_Middleware extends IPF_Middleware
{
function processRequest($request)
{
- $staticUrl = IPF::get('static_url');
+ $staticUrl = $this->settings->get('static_url');
if (!preg_match('#^'.preg_quote($staticUrl).'(.*)$#', $request->query, $matches))
return false;
return $this->apps;
}
+ private function chainMiddlewares()
+ {
+ $middlewares = IPF::get('middlewares', array());
+ array_unshift($middlewares, 'IPF_Error_Middleware');
+ array_push($middlewares, 'IPF_Dispatch_Middleware');
+
+ $m = null;
+ foreach (array_reverse($middlewares) as $mw) {
+ $m = new $mw($m, IPF::$settings);
+ }
+ return $m;
+ }
+
public function run()
{
\PFF\Container::setFactory('databaseConnection', array('IPF_Database', 'connect'));
$cli->run();
} else {
$this->request = new IPF_HTTP_Request;
- $this->router->dispatch($this->request);
+
+ $response = $this->chainMiddlewares()->call($this->request);
+ $response->render($this->request->method !== 'HEAD');
+
$this->request = null;
}
}
}
- public static function response500($request, $exception)
- {
- error_log($exception);
- if (IPF::get('debug'))
- return new IPF_HTTP_Response_ServerErrorDebug($request, $exception);
- else
- return new IPF_HTTP_Response_ServerError($request, $exception);
- }
-
- public function dispatch($req)
- {
- try {
- $middleware = array();
- foreach (IPF::get('middlewares', array()) as $mw) {
- $middleware[] = new $mw();
- }
- $skip = false;
- foreach ($middleware as $mw) {
- if (method_exists($mw, 'processRequest')) {
- $response = $mw->processRequest($req);
- if ($response !== false) {
- // $response is a response
- $response->render($req->method != 'HEAD' and !defined('IN_UNIT_TESTS'));
- $skip = true;
- break;
- }
- }
- }
- if ($skip === false) {
- $response = $this->match($req);
- if (!empty($req->response_vary_on)) {
- $response->headers['Vary'] = $req->response_vary_on;
- }
- $middleware = array_reverse($middleware);
- foreach ($middleware as $mw) {
- if (method_exists($mw, 'processResponse')) {
- $response = $mw->processResponse($req, $response);
- }
- }
- //var_dump($response);
- $response->render($req->method != 'HEAD');
- }
- return array($req, $response);
- } catch (IPF_Exception $e) {
- $response = self::response500($req, $e);
- $response->render();
- }
- }
-
public function match($req)
{
- $func = null;
foreach ($this->routes as $route) {
$match = array();
if ($route->matcher()->match($req->query, $match)) {
- $func = $route->func;
- $req->params = $match;
- break;
+ return array($route, $match);
}
}
-
- if ($func) {
- try {
- $r = IPF::callFunction($func, array($req, $match));
- if (!is_a($r, 'IPF_HTTP_Response')) {
- return self::response500($req, new IPF_Exception('function '.$func.'() must return IPF_HTTP_Response instance'));
- }
- return $r;
- } catch (IPF_Router_Shortcut $e) {
- return $e->response($req);
- } catch (IPF_Exception $e) {
- return self::response500($req, $e);
- }
- }
-
- return new IPF_HTTP_Response_NotFound($req);
+ return null;
}
public function describe()
<?php
-class IPF_Session_Middleware
+class IPF_Session_Middleware extends IPF_Middleware
{
- function processRequest(&$request)
+ function processRequest($request)
{
- $session_cookie = \PFF\Arr::get($request->COOKIE, IPF::get('session_cookie_id'));
+ $session_cookie = \PFF\Arr::get($request->COOKIE, $this->settings->get('session_cookie_id'));
$request->session = Session::get($session_cookie);
if (!$request->session)
function processResponse($request, $response)
{
$session_cookie = $request->session->cookie();
- $response->cookies[IPF::get('session_cookie_id')] = $session_cookie;
+ $response->cookies[$this->settings->get('session_cookie_id')] = $session_cookie;
return $response;
}
}