diff --git a/README.md b/README.md index 116186f..c511696 100644 --- a/README.md +++ b/README.md @@ -52,10 +52,10 @@ use \Pecee\SimpleRouter\SimpleRouter; require_once 'routes.php'; // change this to whatever makes sense in your project // The apps default namespace (so we don't have to specify it each time we use MyController@home) -$defaultControllerNamespace = 'MyWebsite\\Controller'; +SimpleRouter::setDefaultNamespace('MyWebsite\Controller'); // Do the routing -SimpleRouter::start($defaultControllerNamespace); +SimpleRouter::start(); ``` ## Adding routes @@ -129,6 +129,7 @@ class CustomExceptionHandler implements IExceptionHandler { // If the error-code is 404; show another route which contains the page-not-found if($error->getCode() === 404) { + // Throw your custom 404-page view // - or - // load another route with our 404 page @@ -196,13 +197,16 @@ use Pecee\SimpleRouter\SimpleRouter; class Router extends SimpleRouter { - public static function start($defaultNamespace = null) { + public static function start() { // change this to whatever makes sense in your project require_once 'routes.php'; + + // change default namespace for all routes + parent::setDefaultNamespace('\Demo\Controllers'); // Do initial stuff - parent::start('\\Demo\\Controllers'); + parent::start(); } diff --git a/demo-project/app/Controllers/ApiController.php b/demo-project/app/Controllers/ApiController.php index e06b241..c8e951f 100644 --- a/demo-project/app/Controllers/ApiController.php +++ b/demo-project/app/Controllers/ApiController.php @@ -9,12 +9,10 @@ class ApiController { // The variable authenticated is set to true in the ApiVerification middleware class. - $request = SimpleRouter::request(); - header('content-type: application/json'); echo json_encode([ - 'authenticated' => $request->authenticated + 'authenticated' => request()->authenticated ]); } diff --git a/demo-project/app/Controllers/DefaultController.php b/demo-project/app/Controllers/DefaultController.php index 41afc10..29c538f 100644 --- a/demo-project/app/Controllers/DefaultController.php +++ b/demo-project/app/Controllers/DefaultController.php @@ -6,7 +6,7 @@ class DefaultController { public function index() { // implement - echo 'DefaultController -> index'; + echo sprintf('DefaultController -> index (?fun=%s)', input()->get('fun')); } diff --git a/demo-project/app/Router.php b/demo-project/app/Router.php index 131a8d7..2fd10db 100644 --- a/demo-project/app/Router.php +++ b/demo-project/app/Router.php @@ -19,8 +19,10 @@ class Router extends SimpleRouter { // Load our custom routes require_once 'routes.php'; + parent::setDefaultNamespace('\Demo\Controllers'); + // Do initial stuff - parent::start('\\Demo\\Controllers'); + parent::start(); } diff --git a/demo-project/app/helpers.php b/demo-project/app/helpers.php index ac5ee06..1f96af4 100644 --- a/demo-project/app/helpers.php +++ b/demo-project/app/helpers.php @@ -1,5 +1,5 @@ getInput(); } \ No newline at end of file diff --git a/src/Pecee/Exception/RouterException.php b/src/Pecee/Exception/RouterException.php index 5432c66..a9eb41c 100644 --- a/src/Pecee/Exception/RouterException.php +++ b/src/Pecee/Exception/RouterException.php @@ -1,3 +1,4 @@ request = $request; $this->setGet(); $this->setPost(); diff --git a/src/Pecee/Http/Input/InputCollection.php b/src/Pecee/Http/Input/InputCollection.php index 09d601b..352a667 100644 --- a/src/Pecee/Http/Input/InputCollection.php +++ b/src/Pecee/Http/Input/InputCollection.php @@ -10,9 +10,10 @@ class InputCollection implements \IteratorAggregate { * Useful for searching for finding items where $index doesn't contain form name. * * @param string $index + * @param string|null $defaultValue * @return mixed */ - public function findFirst($index) { + public function findFirst($index, $defaultValue = null) { if(count($this->data)) { if(isset($this->data[$index])) { @@ -26,7 +27,7 @@ class InputCollection implements \IteratorAggregate { } } - return null; + return $defaultValue; } /** diff --git a/src/Pecee/Http/Request.php b/src/Pecee/Http/Request.php index f077f49..9e7185c 100644 --- a/src/Pecee/Http/Request.php +++ b/src/Pecee/Http/Request.php @@ -5,45 +5,41 @@ use Pecee\Http\Input\Input; class Request { - protected static $instance; + protected $data = array(); + protected $headers; + protected $host; + protected $uri; + protected $method; + protected $input; - protected $data; - - /** - * Return new instance - * @return static - */ - public static function getInstance() { - if(self::$instance === null) { - self::$instance = new static(); - } - return self::$instance; - } public function __construct() { - $this->data = array(); - $this->host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : array(); - $this->uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : array(); - $this->method = (isset($_POST['_method'])) ? strtolower($_POST['_method']) : (isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : array()); - $this->headers = $this->getAllHeaders(); + $this->parseHeaders(); $this->input = new Input($this); + + $this->host = $this->getHeader('http_host');; + $this->uri = $this->getHeader('request_uri'); + $this->method = strtolower($this->input->post->findFirst('_method', $this->getHeader('request_method'))); } - protected function getAllHeaders() { - $headers = array(); + protected function parseHeaders() { + $this->headers = array(); + foreach ($_SERVER as $name => $value) { - if (substr($name, 0, 5) === 'HTTP_') { - $headers[strtolower(str_replace('_', '-', substr($name, 5)))] = $value; - } + $this->headers[strtolower($name)] = $value; } - return $headers; } - public function getIsSecure() { - if(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) === 'https') { + public function isSecure() { + if($this->getHeader('http_x_forwarded_proto') === 'https') { return true; } - return isset($_SERVER['HTTPS']) ? true : (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] === 443); + + if($this->getHeader('https') !== null) { + return true; + } + + return ($this->getHeader('server_port') === 443); } /** @@ -72,7 +68,7 @@ class Request { * @return string|null */ public function getUser() { - return (isset($_SERVER['PHP_AUTH_USER'])) ? $_SERVER['PHP_AUTH_USER']: null; + return $this->getHeader('php_auth_user'); } /** @@ -80,11 +76,11 @@ class Request { * @return string|null */ public function getPassword() { - return (isset($_SERVER['PHP_AUTH_PW'])) ? $_SERVER['PHP_AUTH_PW']: null; + return $this->getHeader('php_auth_pw'); } /** - * Get headers + * Get all headers * @return array */ public function getHeaders() { @@ -96,10 +92,15 @@ class Request { * @return string */ public function getIp() { - if(isset($_SERVER['HTTP_CF_CONNECTING_IP'])) { - return $_SERVER['HTTP_CF_CONNECTING_IP']; + if($this->getHeader('http_cf_connecting_ip') !== null) { + return $this->getHeader('http_cf_connecting_ip'); } - return ((isset($_SERVER['HTTP_X_FORWARDED_FOR']) && strlen($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null); + + if($this->getHeader('http_x_forwarded_for') !== null && strlen($this->getHeader('http_x_forwarded_for'))) { + return $this->getHeader('http_x_forwarded_for'); + } + + return $this->getHeader('remote_addr'); } /** @@ -107,7 +108,7 @@ class Request { * @return string */ public function getReferer() { - return isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; + return $this->getHeader('http_referer'); } /** @@ -115,16 +116,17 @@ class Request { * @return string */ public function getUserAgent() { - return isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; + return $this->getHeader('http_user_agent'); } /** * Get header value by name * @param string $name + * @param object|null $defaultValue * @return string|null */ - public function getHeader($name) { - return (isset($this->headers[strtolower($name)])) ? $this->headers[strtolower($name)] : null; + public function getHeader($name, $defaultValue = null) { + return isset($this->headers[strtolower($name)]) ? $this->headers[strtolower($name)] : $defaultValue; } /** @@ -135,15 +137,42 @@ class Request { return $this->input; } + /** + * Is format accepted + * @param string $format + * @return bool + */ public function isFormatAccepted($format) { - return (isset($_SERVER['HTTP_ACCEPT']) && stripos($_SERVER['HTTP_ACCEPT'], $format) > -1); + return ($this->getHeader('http_accept') !== null && stripos($this->getHeader('http_accept'), $format) > -1); } + /** + * Get accept formats + * @return array + */ public function getAcceptFormats() { - if(isset($_SERVER['HTTP_ACCEPT'])) { - return explode(',', $_SERVER['HTTP_ACCEPT']); - } - return array(); + return explode(',', $this->getHeader('http_accept')); + } + + /** + * @param string $uri + */ + public function setUri($uri) { + $this->uri = $uri; + } + + /** + * @param string $host + */ + public function setHost($host) { + $this->host = $host; + } + + /** + * @param string $method + */ + public function setMethod($method) { + $this->method = $method; } public function __set($name, $value = null) { @@ -154,33 +183,4 @@ class Request { return isset($this->data[$name]) ? $this->data[$name] : null; } - /** - * Get the currently loaded route. - * @return \Pecee\SimpleRouter\RouterEntry - */ - public function getLoadedRoute() { - return $this->loadedRoute; - } - - /** - * @param mixed $uri - */ - public function setUri($uri) { - $this->uri = $uri; - } - - /** - * @param mixed $host - */ - public function setHost($host) { - $this->host = $host; - } - - /** - * @param mixed $method - */ - public function setMethod($method) { - $this->method = $method; - } - } \ No newline at end of file diff --git a/src/Pecee/Http/Response.php b/src/Pecee/Http/Response.php index ba7bfeb..a1a3106 100644 --- a/src/Pecee/Http/Response.php +++ b/src/Pecee/Http/Response.php @@ -6,7 +6,7 @@ class Response { protected $request; - public function __construct(Request &$request) { + public function __construct(Request $request) { $this->request = $request; } @@ -14,7 +14,7 @@ class Response { * Set the http status code * * @param int $code - * @return self $this + * @return static */ public function httpCode($code) { http_response_code($code); @@ -43,7 +43,7 @@ class Response { /** * Add http authorisation * @param string $name - * @return self $this + * @return static */ public function auth($name = '') { $this->headers([ @@ -87,7 +87,7 @@ class Response { /** * Add header to response * @param string $value - * @return self $this + * @return static */ public function header($value) { header($value); @@ -97,7 +97,7 @@ class Response { /** * Add multiple headers to response * @param array $headers - * @return self $this + * @return static */ public function headers(array $headers) { foreach($headers as $header) { diff --git a/src/Pecee/SimpleRouter/IControllerRoute.php b/src/Pecee/SimpleRouter/IControllerRoute.php new file mode 100644 index 0000000..fae219d --- /dev/null +++ b/src/Pecee/SimpleRouter/IControllerRoute.php @@ -0,0 +1,11 @@ +exceptionHandlers = array(); } + /** + * Add route + * @param RouterEntry $route + * @return RouterEntry + */ public function addRoute(RouterEntry $route) { if($this->currentRoute !== null) { $this->backStack[] = $route; } else { $this->routes[] = $route; } + + return $route; } - protected function processRoutes(array $routes, array $settings = array(), array $prefixes = array(), $backStack = false, $group = null) { + protected function processRoutes(array $routes, array $settings = array(), array $prefixes = array(), $backStack = false, RouterGroup $group = null) { // Loop through each route-request - $routesCount = count($routes); $mergedSettings = array(); /* @var $route RouterEntry */ - for($i = 0; $i < $routesCount; $i++) { + /* @var $group RouterGroup */ + for($i = 0; $i < count($routes); $i++) { $route = $routes[$i]; - $route->addSettings($settings); + if(count($settings)) { + $route->addSettings($settings); + } - if($backStack) { + if($backStack && $group !== null) { $route->setGroup($group); } - if($this->defaultNamespace && !$route->getNamespace()) { + if($route->getNamespace() === null && $this->defaultNamespace !== null) { $namespace = $this->defaultNamespace; if ($route->getNamespace()) { $namespace .= '\\' . $route->getNamespace(); @@ -71,42 +128,37 @@ class RouterBase { $route->setNamespace($namespace); } - $newPrefixes = $prefixes; - - if($route->getPrefix() && trim($route->getPrefix(), '/') !== '') { - array_push($newPrefixes, trim($route->getPrefix(), '/')); + if($group !== null && $group->getPrefix() !== null && trim($group->getPrefix(), '/') !== '') { + $prefixes[] = trim($group->getPrefix(), '/'); } - /* @var $group RouterGroup */ $group = null; + $this->currentRoute = $route; - if(!($route instanceof RouterGroup)) { - if(is_array($newPrefixes) && count($newPrefixes) && $backStack) { - $route->setUrl( '/' . join('/', $newPrefixes) . $route->getUrl() ); + if($route instanceof ILoadableRoute) { + if(is_array($prefixes) && count($prefixes) && $backStack) { + $route->setUrl( '/' . join('/', $prefixes) . $route->getUrl() ); } $this->controllerUrlMap[] = $route; - } + } else { + if(is_callable($route->getCallback())) { - $this->currentRoute = $route; + $route->renderRoute($this->request); - if($route instanceof RouterGroup && is_callable($route->getCallback())) { + if ($route->matchRoute($this->request)) { - $route->renderRoute($this->request); + $group = $route; - if($route->matchRoute($this->request)) { + $mergedSettings = array_merge($settings, $group->getMergeableSettings()); - $group = $route; + // Add ExceptionHandler + if ($group->getExceptionHandler() !== null) { + $this->exceptionHandlers[] = $route; + } - $mergedSettings = array_merge($settings, $group->getMergeableSettings()); - - // Add ExceptionHandler - if ($group->getExceptionHandler() !== null) { - $this->exceptionHandlers[] = $route; } - } - } $this->currentRoute = null; @@ -116,7 +168,7 @@ class RouterBase { $this->backStack = array(); // Route any routes added to the backstack - $this->processRoutes($backStack, $mergedSettings, $newPrefixes, true, $group); + $this->processRoutes($backStack, $mergedSettings, $prefixes, true, $group); } } } @@ -124,7 +176,6 @@ class RouterBase { public function routeRequest($original = true) { $originalUri = $this->request->getUri(); - $routeNotAllowed = false; try { @@ -144,23 +195,17 @@ class RouterBase { // Loop through each route-request $this->processRoutes($this->routes); - if($original === true) { + if($original === true && $this->csrfVerifier !== null) { // Verify csrf token for request - if ($this->baseCsrfVerifier !== null) { - $this->baseCsrfVerifier->handle($this->request); - } + $this->csrfVerifier->handle($this->request); } - $max = count($this->controllerUrlMap); - /* @var $route RouterEntry */ - for ($i = 0; $i < $max; $i++) { + for ($i = 0; $i < count($this->controllerUrlMap); $i++) { $route = $this->controllerUrlMap[$i]; - $routeMatch = $route->matchRoute($this->request); - - if ($routeMatch) { + if ($route->matchRoute($this->request)) { if (count($route->getRequestMethods()) && !in_array($this->request->getMethod(), $route->getRequestMethods())) { $routeNotAllowed = true; @@ -221,6 +266,7 @@ class RouterBase { } /** + * Get default namespace * @return string */ public function getDefaultNamespace(){ @@ -228,6 +274,7 @@ class RouterBase { } /** + * Set the main default namespace that all routes will inherit * @param string $defaultNamespace * @return static */ @@ -237,6 +284,7 @@ class RouterBase { } /** + * Get bootmanagers * @return array */ public function getBootManagers() { @@ -244,38 +292,27 @@ class RouterBase { } /** + * Set bootmanagers * @param array $bootManagers */ public function setBootManagers(array $bootManagers) { $this->bootManagers = $bootManagers; } + /** + * Add bootmanager + * @param RouterBootManager $bootManager + */ public function addBootManager(RouterBootManager $bootManager) { $this->bootManagers[] = $bootManager; } /** + * Get the loaded route * @return RouterEntry */ public function getLoadedRoute() { - if(!($this->request->loadedRoute instanceof RouterGroup)) { - return $this->request->loadedRoute; - } - return null; - } - - /** - * @return array - */ - public function getBackstack() { - return $this->backStack; - } - - /** - * @return RouterEntry - */ - public function getCurrentRoute(){ - return $this->currentRoute; + return $this->request->loadedRoute; } /** @@ -303,21 +340,21 @@ class RouterBase { } /** - * Get base csrf verifier class + * Get csrf verifier class * @return BaseCsrfVerifier */ - public function getBaseCsrfVerifier() { - return $this->baseCsrfVerifier; + public function getCsrfVerifier() { + return $this->csrfVerifier; } /** - * Set base csrf verifier class + * Set csrf verifier class * - * @param BaseCsrfVerifier $baseCsrfVerifier - * @return self + * @param BaseCsrfVerifier $csrfVerifier + * @return static */ - public function setBaseCsrfVerifier(BaseCsrfVerifier $baseCsrfVerifier) { - $this->baseCsrfVerifier = $baseCsrfVerifier; + public function setCsrfVerifier(BaseCsrfVerifier $csrfVerifier) { + $this->csrfVerifier = $csrfVerifier; return $this; } @@ -353,25 +390,22 @@ class RouterBase { $url = $domain . '/' . trim($route->getUrl(), '/'); - if(($route instanceof RouterController || $route instanceof RouterResource) && $method !== null) { - if($method !== null) { - $url .= $method; - } + if($route instanceof IControllerRoute && $method !== null) { + $url .= $method; if(count($parameters)) { $url .= join('/', $parameters); } } else { - /* @var $route RouterEntry */ - if(is_array($parameters)) { + if($parameters !== null && is_array($parameters)) { $params = array_merge($route->getParameters(), $parameters); } else { $params = $route->getParameters(); } - $otherParams = []; - + $otherParams = array(); $i = 0; + foreach($params as $param => $value) { $value = (isset($parameters[$param])) ? $parameters[$param] : $value; if(stripos($url, '{' . $param. '}') !== false || stripos($url, '{' . $param . '?}') !== false) { @@ -406,7 +440,7 @@ class RouterBase { // Return current route if no options has been specified if($controller === null && $parameters === null) { - $getParams = (is_array($getParams)) ? array_merge($_GET, $getParams) : $_GET; + $getParams = ($getParams !== null && is_array($getParams)) ? array_merge($_GET, $getParams) : $_GET; $url = parse_url($this->request->getUri(), PHP_URL_PATH); @@ -423,7 +457,6 @@ class RouterBase { $c = ''; $method = null; - $max = count($this->controllerUrlMap); /* @var $route RouterRoute */ @@ -432,8 +465,9 @@ class RouterBase { $route = $this->controllerUrlMap[$i]; // Check an alias exist, if the matches - use it - if($route instanceof RouterRoute) { - + if($route instanceof IControllerRoute) { + $c = $route->getController(); + } else { if($route->hasAlias($controller)) { return $this->processUrl($route, $route->getMethod(), $parameters, $getParams); } @@ -441,8 +475,6 @@ class RouterBase { if(!is_callable($route->getCallback()) && stripos($route->getCallback(), '@') !== false) { $c = $route->getCallback(); } - } else if($route instanceof RouterController || $route instanceof RouterResource) { - $c = $route->getController(); } if($c === $controller || strpos($c, $controller) === 0) { @@ -457,10 +489,10 @@ class RouterBase { $route = $this->controllerUrlMap[$i]; - if($route instanceof RouterRoute && !is_callable($route->getCallback()) && stripos($route->getCallback(), '@') !== false) { - $c = $route->getClass(); - } else if($route instanceof RouterController || $route instanceof RouterResource) { + if($route instanceof IControllerRoute) { $c = $route->getController(); + } else if(!is_callable($route->getCallback()) && stripos($route->getCallback(), '@') !== false) { + $c = $route->getClass(); } if(stripos($controller, '@') !== false) { @@ -477,10 +509,8 @@ class RouterBase { $controller = ($controller === null) ? '/' : $controller; $url = array($controller); - if(is_array($parameters)) { - foreach($parameters as $key => $value) { - array_push($url,$value); - } + if($parameters !== null && is_array($parameters) && count($parameters)) { + $url = array_merge($url, $parameters); } $url = '/' . trim(join('/', $url), '/') . '/'; @@ -500,6 +530,7 @@ class RouterBase { if(static::$instance === null) { static::$instance = new static(); } + return static::$instance; } diff --git a/src/Pecee/SimpleRouter/RouterController.php b/src/Pecee/SimpleRouter/RouterController.php index bcb5cdc..4d1723d 100644 --- a/src/Pecee/SimpleRouter/RouterController.php +++ b/src/Pecee/SimpleRouter/RouterController.php @@ -4,7 +4,7 @@ namespace Pecee\SimpleRouter; use Pecee\Exception\RouterException; use Pecee\Http\Request; -class RouterController extends RouterEntry { +class RouterController extends RouterEntry implements ILoadableRoute, IControllerRoute { const DEFAULT_METHOD = 'index'; @@ -13,7 +13,6 @@ class RouterController extends RouterEntry { protected $method; public function __construct($url, $controller) { - parent::__construct(); $this->url = $url; $this->controller = $controller; } @@ -44,8 +43,8 @@ class RouterController extends RouterEntry { } public function matchRoute(Request $request) { - $url = parse_url(urldecode($request->getUri())); - $url = rtrim($url['path'], '/') . '/'; + $url = parse_url(urldecode($request->getUri()), PHP_URL_PATH); + $url = rtrim($url, '/') . '/'; if(strtolower($url) == strtolower($this->url) || stripos($url, $this->url) === 0) { @@ -55,11 +54,11 @@ class RouterController extends RouterEntry { if(count($path)) { - $method = (!isset($path[0]) || trim($path[0]) === '') ? self::DEFAULT_METHOD : $path[0]; + $method = (!isset($path[0]) || trim($path[0]) === '') ? static::DEFAULT_METHOD : $path[0]; $this->method = $method; array_shift($path); - $this->parameters = $path; + $this->settings['parameters'] = $path; // Set callback $this->setCallback($this->controller . '@' . $this->method); diff --git a/src/Pecee/SimpleRouter/RouterEntry.php b/src/Pecee/SimpleRouter/RouterEntry.php index 39b2d69..a8a1466 100644 --- a/src/Pecee/SimpleRouter/RouterEntry.php +++ b/src/Pecee/SimpleRouter/RouterEntry.php @@ -14,24 +14,22 @@ abstract class RouterEntry { const REQUEST_TYPE_PATCH = 'patch'; const REQUEST_TYPE_DELETE = 'delete'; - public static $allowedRequestTypes = array( + public static $allowedRequestTypes = [ self::REQUEST_TYPE_DELETE, self::REQUEST_TYPE_GET, self::REQUEST_TYPE_POST, self::REQUEST_TYPE_PUT, - self::REQUEST_TYPE_PATCH - ); + self::REQUEST_TYPE_PATCH, + ]; + + protected $settings = [ + 'requestMethods' => array(), + 'where' => array(), + 'parameters' => array(), + ]; - protected $settings; protected $callback; - public function __construct() { - $this->settings = array(); - $this->settings['requestMethods'] = array(); - $this->settings['where'] = array(); - $this->settings['parameters'] = array(); - } - /** * Returns callback name/identifier for the current route based on the callback. * Useful if you need to get a unique identifier for the loaded route, for instance @@ -48,7 +46,7 @@ abstract class RouterEntry { /** * @param string $callback - * @return self; + * @return static */ public function setCallback($callback) { $this->callback = $callback; @@ -90,50 +88,43 @@ abstract class RouterEntry { /** * @param string $prefix - * @return self - */ + * @return static + * public function setPrefix($prefix) { - $this->prefix = '/' . ltrim($prefix, '/'); + $this->settings['prefix'] = '/' . ltrim($prefix, '/'); return $this; - } + }*/ /** * @param string $middleware - * @return self + * @return static */ public function setMiddleware($middleware) { - $this->middleware = $middleware; + $this->settings['middleware'] = $middleware; return $this; } /** * @param string $namespace - * @return self + * @return static */ public function setNamespace($namespace) { - $this->namespace = $namespace; + $this->settings['namespace'] = $namespace; return $this; } - /** - * @return string - */ - public function getPrefix() { - return $this->prefix; - } - /** * @return string|array */ public function getMiddleware() { - return $this->middleware; + return $this->settingArray('middleware'); } /** * @return string */ public function getNamespace() { - return $this->namespace; + return $this->setting('namespace'); } /** @@ -144,18 +135,18 @@ abstract class RouterEntry { } /** - * @return mixed + * @return array */ public function getParameters(){ - return ($this->parameters === null) ? array() : $this->parameters; + return $this->setting('parameters', array()); } /** * @param mixed $parameters - * @return self + * @return static */ public function setParameters($parameters) { - $this->parameters = $parameters; + $this->settings['parameters'] = $parameters; return $this; } @@ -163,10 +154,10 @@ abstract class RouterEntry { * Add regular expression parameter match * * @param array $options - * @return self + * @return static */ public function where(array $options) { - $this->where = array_merge($this->where, $options); + $this->settings['where'] = array_merge($this->settings['where'], $options); return $this; } @@ -174,10 +165,10 @@ abstract class RouterEntry { * Add regular expression match for url * * @param string $regex - * @return self + * @return static */ public function match($regex) { - $this->regexMatch = $regex; + $this->settings['regexMatch'] = $regex; return $this; } @@ -187,60 +178,27 @@ abstract class RouterEntry { * @return array */ public function getMergeableSettings() { - $settings = $this->settings; - - if(isset($settings['prefix'])) { - unset($settings['prefix']); - } - - return $settings; + return $this->settings; } /** * @param array $settings - * @return self + * @return static */ - public function addSettings(array $settings = null) { - if(is_array($settings)) { - $this->settings = array_merge($this->settings, $settings); - } + public function addSettings(array $settings) { + $this->settings = array_merge($this->settings, $settings); return $this; } /** * @param array $settings - * @return self + * @return static */ public function setSettings($settings) { $this->settings = $settings; - - if(isset($settings['prefix'])) { - $this->setPrefix($settings['prefix']); - } - return $this; } - /** - * Dynamically access settings value - * - * @param $name - * @return mixed|null - */ - public function __get($name) { - return (isset($this->settings[$name]) ? $this->settings[$name] : null); - } - - /** - * Dynamically set settings value - * - * @param string $name - * @param mixed|null $value - */ - public function __set($name, $value = null) { - $this->settings[$name] = $value; - } - protected function loadClass($name) { if(!class_exists($name)) { throw new RouterException(sprintf('Class %s does not exist', $name)); @@ -273,8 +231,8 @@ abstract class RouterEntry { // Check for optional parameter // Use custom parameter regex if it exists - if(is_array($this->where) && isset($this->where[$parameter])) { - $parameterRegex = $this->where[$parameter]; + if(is_array($this->setting('where')) && isset($this->settings['where'][$parameter])) { + $parameterRegex = $this->settings['where'][$parameter]; } if($lastCharacter === '?') { @@ -284,7 +242,12 @@ abstract class RouterEntry { } else { $regex .= '\/?(?P<' . $parameter . '>'. $parameterRegex .')[^\/]?'; } - $parameterNames[] = array('name' => $parameter, 'required' => $required); + + $parameterNames[] = [ + 'name' => $parameter, + 'required' => $required + ]; + $parameter = ''; $isParameter = false; @@ -307,21 +270,19 @@ abstract class RouterEntry { $max = count($parameterNames); - if($max) { - for($i = 0; $i < $max; $i++) { - $name = $parameterNames[$i]; - $parameterValue = isset($parameterValues[$name['name']]) ? $parameterValues[$name['name']] : null; + for($i = 0; $i < $max; $i++) { + $name = $parameterNames[$i]; + $parameterValue = isset($parameterValues[$name['name']]) ? $parameterValues[$name['name']] : null; - if($name['required'] && $parameterValue === null) { - throw new RouterException('Missing required parameter ' . $name['name'], 404); - } - - if(!$name['required'] && $parameterValue === null) { - continue; - } - - $parameters[$name['name']] = $parameterValue; + if($name['required'] && $parameterValue === null) { + throw new RouterException('Missing required parameter ' . $name['name'], 404); } + + if(!$name['required'] && $parameterValue === null) { + continue; + } + + $parameters[$name['name']] = $parameterValue; } return $parameters; @@ -331,21 +292,11 @@ abstract class RouterEntry { } public function loadMiddleware(Request $request) { - if($this->getMiddleware()) { - if(is_array($this->getMiddleware())) { - foreach($this->getMiddleware() as $middleware) { - $middleware = $this->loadClass($middleware); - if (!($middleware instanceof IMiddleware)) { - throw new RouterException($middleware . ' must be instance of Middleware'); - } - - /* @var $class IMiddleware */ - $middleware->handle($request); - } - } else { - $middleware = $this->loadClass($this->getMiddleware()); + if(count($this->getMiddleware())) { + foreach($this->getMiddleware() as $middleware) { + $middleware = $this->loadClass($middleware); if (!($middleware instanceof IMiddleware)) { - throw new RouterException($this->getMiddleware() . ' must be instance of Middleware'); + throw new RouterException($middleware . ' must be instance of Middleware'); } /* @var $class IMiddleware */ @@ -371,7 +322,7 @@ abstract class RouterEntry { } $parameters = array_filter($this->getParameters(), function($var){ - return !is_null($var); + return ($var !== null); }); call_user_func_array(array($class, $method), $parameters); @@ -386,7 +337,7 @@ abstract class RouterEntry { * Set allowed request methods * * @param array $methods - * @return self $this + * @return static $this */ public function setRequestMethods(array $methods) { $this->settings['requestMethods'] = $methods; @@ -399,22 +350,32 @@ abstract class RouterEntry { * @return array */ public function getRequestMethods() { - if(!isset($this->settings['requestMethods']) || isset($this->settings['requestMethods']) && !is_array($this->settings['requestMethods'])) { - $value = isset($this->settings['requestMethods']) ? $this->settings['requestMethods'] : null; - return array($value); - } - return $this->settings['requestMethods']; + return $this->settingArray('requestMethods'); } public function getGroup() { - return $this->group; + return $this->setting('group'); } public function setGroup($group) { - $this->group = $group; + $this->settings['group'] = $group; return $this; } + protected function setting($name, $defaultValue = null) { + return isset($this->settings[$name]) ? $this->settings[$name] : $defaultValue; + } + + protected function settingArray($name) { + $value = $this->setting($name); + + if($value === null) { + return []; + } + + return (!is_array($value)) ? array($value) : $value; + } + abstract function matchRoute(Request $request); } \ No newline at end of file diff --git a/src/Pecee/SimpleRouter/RouterGroup.php b/src/Pecee/SimpleRouter/RouterGroup.php index 11863b0..16344ac 100644 --- a/src/Pecee/SimpleRouter/RouterGroup.php +++ b/src/Pecee/SimpleRouter/RouterGroup.php @@ -7,20 +7,20 @@ use Pecee\Http\Request; class RouterGroup extends RouterEntry { + protected $loadableRoute = false; + public function matchDomain(Request $request) { - if($this->domain !== null) { + if($this->setting('domain') !== null) { - if(is_array($this->domain)) { + if(is_array($this->setting('domain'))) { - $max = count($this->domain); - - for($i = 0; $i < $max; $i++) { - $domain = $this->domain[$i]; + for($i = 0; $i < count($this->setting('domain')); $i++) { + $domain = $this->settings['domain'][$i]; $parameters = $this->parseParameters($domain, $request->getHost(), '[^.]*'); if($parameters !== null) { - $this->parameters = $parameters; + $this->settings['parameters'] = $parameters; return true; } } @@ -28,10 +28,10 @@ class RouterGroup extends RouterEntry { return false; } - $parameters = $this->parseParameters($this->domain, $request->getHost(), '[^.]*'); + $parameters = $this->parseParameters($this->setting('domain'), $request->getHost(), '[^.]*'); if ($parameters !== null) { - $this->parameters = $parameters; + $this->settings['parameters'] = $parameters; return true; } @@ -43,10 +43,10 @@ class RouterGroup extends RouterEntry { public function renderRoute(Request $request) { // Check if request method is allowed - $hasAccess = (!$this->method); + $hasAccess = true; - if($this->method) { - if(is_array($this->method)) { + if($this->setting('method') !== null) { + if(is_array($this->setting('method'))) { $hasAccess = (in_array($request->getMethod(), $this->getRequestMethods())); } else { $hasAccess = strtolower($this->getRequestMethods()) == strtolower($request->getMethod()); @@ -72,48 +72,69 @@ class RouterGroup extends RouterEntry { } public function setExceptionHandler($class) { - $this->exceptionHandler = $class; + $this->settings['exceptionHandler'] = $class; return $this; } public function getExceptionHandler() { - return $this->exceptionHandler; + return $this->setting('exceptionHandler'); } public function getDomain() { - return $this->domain; + return $this->setting('domain'); + } + + /** + * @param string $prefix + * @return static + */ + public function setPrefix($prefix) { + $this->settings['prefix'] = '/' . ltrim($prefix, '/'); + return $this; + } + + /** + * @return string + */ + public function getPrefix() { + return $this->setting('prefix'); } /** * @param array $settings - * @return self + * @return static */ - public function addSettings(array $settings = null) { - if($this->getNamespace() !== null && isset($settings['namespace'])) { + public function addSettings(array $settings) { + + if ($this->getNamespace() !== null && isset($settings['namespace'])) { unset($settings['namespace']); } // Push middleware if multiple - if($this->getMiddleware() !== null && isset($settings['middleware'])) { + if ($this->getMiddleware() !== null && isset($settings['middleware'])) { - if(!is_array($this->getMiddleware())) { - $middlewares = [ - $this->getMiddleware(), - $settings['middleware'] - ]; + if (!is_array($this->getMiddleware())) { + $settings['middleware'] = array($this->getMiddleware(), $settings['middleware']); } else { - $middlewares = array_push($settings['middleware'], $this->getMiddleware()); + $settings['middleware'][] = $this->getMiddleware(); } - $settings['middleware'] = array_unique(array_reverse($middlewares)); + $settings['middleware'] = array_unique(array_reverse($settings['middleware'])); } - if(is_array($settings)) { - $this->settings = array_merge($this->settings, $settings); - } - + $this->settings = array_merge($this->settings, $settings); return $this; } + public function getMergeableSettings() { + $settings = $this->settings; + + if(isset($settings['prefix'])) { + unset($settings['prefix']); + } + + return $settings; + } + } \ No newline at end of file diff --git a/src/Pecee/SimpleRouter/RouterResource.php b/src/Pecee/SimpleRouter/RouterResource.php index b891028..9829c43 100644 --- a/src/Pecee/SimpleRouter/RouterResource.php +++ b/src/Pecee/SimpleRouter/RouterResource.php @@ -4,17 +4,16 @@ namespace Pecee\SimpleRouter; use Pecee\Exception\RouterException; use Pecee\Http\Request; -class RouterResource extends RouterEntry { +class RouterResource extends RouterEntry implements ILoadableRoute, IControllerRoute { protected $url; protected $controller; protected $postMethod; public function __construct($url, $controller) { - parent::__construct(); $this->url = $url; $this->controller = $controller; - $this->postMethod = strtolower(($_SERVER['REQUEST_METHOD'] != 'GET') ? 'post' : 'get'); + $this->postMethod = strtolower(($_SERVER['REQUEST_METHOD']) !== 'get') ? 'post' : 'get'; } public function renderRoute(Request $request) { @@ -42,7 +41,7 @@ class RouterResource extends RouterEntry { protected function call($method, $parameters) { $this->setCallback($this->controller . '@' . $method); - $this->parameters = $parameters; + $this->settings['parameters'] = $parameters; return true; } @@ -57,39 +56,39 @@ class RouterResource extends RouterEntry { if($parameters !== null) { if(is_array($parameters)) { - $parameters = array_merge($this->parameters, $parameters); + $parameters = array_merge($this->settings['parameters'], $parameters); } $action = isset($parameters['action']) ? $parameters['action'] : null; unset($parameters['action']); // Delete - if($request->getMethod() === self::REQUEST_TYPE_DELETE && $this->postMethod === self::REQUEST_TYPE_POST) { + if($request->getMethod() === static::REQUEST_TYPE_DELETE && $this->postMethod === static::REQUEST_TYPE_POST) { return $this->call('destroy', $parameters); } // Update - if(in_array($request->getMethod(), array(self::REQUEST_TYPE_PATCH, self::REQUEST_TYPE_PUT)) && $this->postMethod === self::REQUEST_TYPE_POST) { + if(in_array($request->getMethod(), array(static::REQUEST_TYPE_PATCH, static::REQUEST_TYPE_PUT)) && $this->postMethod === static::REQUEST_TYPE_POST) { return $this->call('update', $parameters); } // Edit - if(isset($action) && strtolower($action) === 'edit' && $this->postMethod === self::REQUEST_TYPE_GET) { + if(isset($action) && strtolower($action) === 'edit' && $this->postMethod === static::REQUEST_TYPE_GET) { return $this->call('edit', $parameters); } // Create - if(strtolower($action) === 'create' && $request->getMethod() === self::REQUEST_TYPE_GET) { + if(strtolower($action) === 'create' && $request->getMethod() === static::REQUEST_TYPE_GET) { return $this->call('create', $parameters); } // Save - if($this->postMethod === self::REQUEST_TYPE_POST) { + if($this->postMethod === static::REQUEST_TYPE_POST) { return $this->call('store', $parameters); } // Show - if(isset($parameters['id']) && $this->postMethod === self::REQUEST_TYPE_GET) { + if(isset($parameters['id']) && $this->postMethod === static::REQUEST_TYPE_GET) { return $this->call('show', $parameters); } diff --git a/src/Pecee/SimpleRouter/RouterRoute.php b/src/Pecee/SimpleRouter/RouterRoute.php index 08b6581..7fdc3b3 100644 --- a/src/Pecee/SimpleRouter/RouterRoute.php +++ b/src/Pecee/SimpleRouter/RouterRoute.php @@ -4,28 +4,27 @@ namespace Pecee\SimpleRouter; use Pecee\Http\Request; -class RouterRoute extends RouterEntry { +class RouterRoute extends RouterEntry implements ILoadableRoute { const PARAMETERS_REGEX_MATCH = '{([A-Za-z\-\_]*?)\?{0,1}}'; protected $url; public function __construct($url, $callback) { - parent::__construct(); $this->setUrl($url); $this->setCallback($callback); } public function matchRoute(Request $request) { - $url = parse_url(urldecode($request->getUri())); - $url = rtrim($url['path'], '/') . '/'; + $url = parse_url(urldecode($request->getUri()), PHP_URL_PATH); + $url = rtrim($url, '/') . '/'; // Match on custom defined regular expression - if($this->regexMatch) { + if($this->setting('regexMatch') !== null) { $parameters = array(); - if(preg_match('/('.$this->regexMatch.')/is', $request->getHost() . $url, $parameters)) { - $this->parameters = (!is_array($parameters[0]) ? array($parameters[0]) : $parameters[0]); + if(preg_match('/(' . $this->setting('regexMatch') . ')/is', $request->getHost() . $url, $parameters)) { + $this->settings['parameters'] = (!is_array($parameters[0]) ? array($parameters[0]) : $parameters[0]); return true; } return null; @@ -37,13 +36,7 @@ class RouterRoute extends RouterEntry { $parameters = $this->parseParameters($route, $url); if($parameters !== null) { - - if(is_array($this->parameters)) { - $this->parameters = array_merge($this->parameters, $parameters); - } else { - $this->parameters = $parameters; - } - + $this->settings['parameters'] = array_merge($this->settingArray('parameters'), $parameters); return true; } @@ -59,22 +52,23 @@ class RouterRoute extends RouterEntry { /** * @param string $url - * @return self + * @return static */ public function setUrl($url) { $parameters = array(); $matches = array(); - if(preg_match_all('/'.self::PARAMETERS_REGEX_MATCH.'/is', $url, $matches)) { + if(preg_match_all('/' . static::PARAMETERS_REGEX_MATCH . '/is', $url, $matches)) { $parameters = $matches[1]; } if(count($parameters)) { - $tmp = array(); - foreach($parameters as $param) { - $tmp[$param] = null; + + foreach(array_keys($parameters) as $key) { + $parameters[$key] = null; } - $this->parameters = $tmp; + + $this->settings['parameters'] = $parameters; } $this->url = $url; @@ -86,23 +80,24 @@ class RouterRoute extends RouterEntry { * @return string|array */ public function getAlias(){ - return $this->alias; + return $this->setting('alias'); } /** * Check if route has given alias. * - * @param $name + * @param string $name * @return bool */ public function hasAlias($name) { - if(is_array($this->alias)) { - foreach($this->alias as $alias) { - if(strtolower($alias) === strtolower($name)) { - return true; + if ($this->getAlias() !== null) { + if (is_array($this->getAlias())) { + foreach ($this->setting('alias') as $alias) { + if (strtolower($alias) === strtolower($name)) { + return true; + } } } - } else { return strtolower($this->getAlias()) === strtolower($name); } @@ -112,21 +107,21 @@ class RouterRoute extends RouterEntry { /** * Set the url alias for easier getting the url route. * @param string|array $alias - * @return self + * @return static */ public function setAlias($alias){ - $this->alias = $alias; + $this->settings['alias'] = $alias; return $this; } - public function setSettings($settings) { + public function addSettings(array $settings) { // Change as to alias - if(isset($settings{'as'})) { + if(isset($settings['as'])) { $this->setAlias($settings['as']); } - return parent::setSettings($settings); + return parent::addSettings($settings); } } \ No newline at end of file diff --git a/src/Pecee/SimpleRouter/SimpleRouter.php b/src/Pecee/SimpleRouter/SimpleRouter.php index d5a3a7c..352e69d 100644 --- a/src/Pecee/SimpleRouter/SimpleRouter.php +++ b/src/Pecee/SimpleRouter/SimpleRouter.php @@ -15,11 +15,18 @@ class SimpleRouter { /** * Start/route request - * @param null $defaultNamespace * @throws \Pecee\Exception\RouterException */ - public static function start($defaultNamespace = null) { - RouterBase::getInstance()->setDefaultNamespace($defaultNamespace)->routeRequest(); + public static function start() { + RouterBase::getInstance()->routeRequest(); + } + + /** + * Set default namespace for all routes + * @param string $defaultNamespace + */ + public static function setDefaultNamespace($defaultNamespace) { + RouterBase::getInstance()->setDefaultNamespace($defaultNamespace); } /** @@ -27,7 +34,7 @@ class SimpleRouter { * @param BaseCsrfVerifier $baseCsrfVerifier */ public static function csrfVerifier(BaseCsrfVerifier $baseCsrfVerifier) { - RouterBase::getInstance()->setBaseCsrfVerifier($baseCsrfVerifier); + RouterBase::getInstance()->setCsrfVerifier($baseCsrfVerifier); } public static function addBootManager(RouterBootManager $bootManager) { @@ -35,19 +42,19 @@ class SimpleRouter { } public static function get($url, $callback, array $settings = null) { - return self::match(['get'], $url, $callback, $settings); + return static::match(['get'], $url, $callback, $settings); } public static function post($url, $callback, array $settings = null) { - return self::match(['post'], $url, $callback, $settings); + return static::match(['post'], $url, $callback, $settings); } public static function put($url, $callback, array $settings = null) { - return self::match(['put'], $url, $callback, $settings); + return static::match(['put'], $url, $callback, $settings); } public static function delete($url, $callback, array $settings = null) { - return self::match(['delete'], $url, $callback, $settings); + return static::match(['delete'], $url, $callback, $settings); } public static function group($settings = array(), $callback) { @@ -72,7 +79,7 @@ class SimpleRouter { * @return RouterRoute */ public static function basic($url, $callback, array $settings = null) { - return self::match(['get', 'post'], $url, $callback, $settings); + return static::match(['get', 'post'], $url, $callback, $settings); } public static function match(array $requestMethods, $url, $callback, array $settings = null) { @@ -119,21 +126,25 @@ class SimpleRouter { $route->addSettings($settings); } - RouterBase::getInstance()->addRoute($route); + static::router()->addRoute($route); return $route; } public static function getRoute($controller = null, $parameters = null, $getParams = null) { - return RouterBase::getInstance()->getRoute($controller, $parameters, $getParams); + return static::router()->getRoute($controller, $parameters, $getParams); } public static function request() { - return RouterBase::getInstance()->getRequest(); + return static::router()->getRequest(); } public static function response() { - return RouterBase::getInstance()->getResponse(); + return static::router()->getResponse(); + } + + protected static function router() { + return RouterBase::getInstance(); } } \ No newline at end of file diff --git a/test/GroupTest.php b/test/GroupTest.php index b9f6c23..f467454 100644 --- a/test/GroupTest.php +++ b/test/GroupTest.php @@ -7,15 +7,13 @@ class GroupTest extends PHPUnit_Framework_TestCase { protected $result; - protected function group() { - $this->result = true; - } - public function testGroup() { $this->result = false; - \Pecee\SimpleRouter\SimpleRouter::group(['prefix' => '/group'], $this->group()); + \Pecee\SimpleRouter\SimpleRouter::group(['prefix' => '/group'], function() { + $this->result = true; + }); try { \Pecee\SimpleRouter\SimpleRouter::start(); @@ -41,4 +39,39 @@ class GroupTest extends PHPUnit_Framework_TestCase { \Pecee\SimpleRouter\SimpleRouter::start(); } + public function testManyRoutes() { + + \Pecee\SimpleRouter\RouterBase::getInstance()->reset(); + \Pecee\SimpleRouter\SimpleRouter::request()->setUri('/my/match'); + \Pecee\SimpleRouter\SimpleRouter::request()->setMethod('get'); + + \Pecee\SimpleRouter\SimpleRouter::group(['prefix' => '/api'], function() { + \Pecee\SimpleRouter\SimpleRouter::group(['prefix' => '/v1'], function() { + \Pecee\SimpleRouter\SimpleRouter::get('/test', 'DummyController@start'); + }); + }); + + \Pecee\SimpleRouter\SimpleRouter::get('/my/match', 'DummyController@start'); + + \Pecee\SimpleRouter\SimpleRouter::group(['prefix' => '/service'], function() { + \Pecee\SimpleRouter\SimpleRouter::group(['prefix' => '/v1'], function() { + \Pecee\SimpleRouter\SimpleRouter::get('/no-match', 'DummyController@start'); + }); + }); + + \Pecee\SimpleRouter\SimpleRouter::start(); + } + + public function testUrls() { + + \Pecee\SimpleRouter\SimpleRouter::get('/my/fancy/url/1', 'DummyController@start', ['as' => 'fancy1']); + \Pecee\SimpleRouter\SimpleRouter::get('/my/fancy/url/2', 'DummyController@start')->setAlias('fancy2'); + + \Pecee\SimpleRouter\SimpleRouter::start(); + + $this->assertTrue((\Pecee\SimpleRouter\SimpleRouter::getRoute('fancy1') === '/my/fancy/url/1/')); + $this->assertTrue((\Pecee\SimpleRouter\SimpleRouter::getRoute('fancy2') === '/my/fancy/url/2/')); + + } + } \ No newline at end of file