@@ -6,11 +6,11 @@ |
||
| 6 | 6 | use Psr\Http\Message\ResponseInterface; |
| 7 | 7 | use Psr\Http\Server\RequestHandlerInterface; |
| 8 | 8 | |
| 9 | -class Auth extends Middleware{ |
|
| 9 | +class Auth extends Middleware { |
|
| 10 | 10 | |
| 11 | 11 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
| 12 | 12 | { |
| 13 | - if(!array_key_exists('user',$_SESSION)){ |
|
| 13 | + if (!array_key_exists('user', $_SESSION)) { |
|
| 14 | 14 | $this->error = 'The user must be logged in to the system'; |
| 15 | 15 | } |
| 16 | 16 | |
@@ -7,11 +7,11 @@ |
||
| 7 | 7 | use Psr\Http\Message\ResponseInterface; |
| 8 | 8 | use Psr\Http\Server\RequestHandlerInterface; |
| 9 | 9 | |
| 10 | -class Lasted extends Middleware{ |
|
| 10 | +class Lasted extends Middleware { |
|
| 11 | 11 | |
| 12 | 12 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
| 13 | 13 | { |
| 14 | - if(!isset($this->error)){ |
|
| 14 | + if (!isset($this->error)) { |
|
| 15 | 15 | throw new Exception("Access not belonged: {$this->error}"); |
| 16 | 16 | } |
| 17 | 17 | |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | { |
| 17 | 17 | $this->hasRouteName($name); |
| 18 | 18 | |
| 19 | - if(!$this->loaded){ |
|
| 19 | + if (!$this->loaded) { |
|
| 20 | 20 | $this->loadIn($name); |
| 21 | 21 | } |
| 22 | 22 | |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | |
| 26 | 26 | public function hasCurrentRoute(): void |
| 27 | 27 | { |
| 28 | - if(!isset($this->currentRoute)){ |
|
| 28 | + if (!isset($this->currentRoute)) { |
|
| 29 | 29 | throw new \RuntimeException('Route not yet loaded'); |
| 30 | 30 | } |
| 31 | 31 | } |
@@ -12,32 +12,32 @@ discard block |
||
| 12 | 12 | |
| 13 | 13 | public static function get(string $uri, $closure): RouterInterface |
| 14 | 14 | { |
| 15 | - return self::set('get',$uri,$closure); |
|
| 15 | + return self::set('get', $uri, $closure); |
|
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | public static function post(string $uri, $closure): RouterInterface |
| 19 | 19 | { |
| 20 | - return self::set('post',$uri,$closure); |
|
| 20 | + return self::set('post', $uri, $closure); |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | public static function ajax(string $uri, $closure): RouterInterface |
| 24 | 24 | { |
| 25 | - return self::set('ajax',$uri,$closure); |
|
| 25 | + return self::set('ajax', $uri, $closure); |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | public static function delete(string $uri, $closure): RouterInterface |
| 29 | 29 | { |
| 30 | - return self::set('delete',$uri,$closure); |
|
| 30 | + return self::set('delete', $uri, $closure); |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | public static function put(string $uri, $closure): RouterInterface |
| 34 | 34 | { |
| 35 | - return self::set('put',$uri,$closure); |
|
| 35 | + return self::set('put', $uri, $closure); |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | public static function patch(string $uri, $closure): RouterInterface |
| 39 | 39 | { |
| 40 | - return self::set('patch',$uri,$closure); |
|
| 40 | + return self::set('patch', $uri, $closure); |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | public static function match(string $method, string $uri, $closure): RouterInterface |
@@ -48,14 +48,14 @@ discard block |
||
| 48 | 48 | |
| 49 | 49 | public static function any(string $uri, $closure): RouterInterface |
| 50 | 50 | { |
| 51 | - return self::set('*',$uri,$closure); |
|
| 51 | + return self::set('*', $uri, $closure); |
|
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | private static function set(string $method, string $uri, $closure): RouterInterface |
| 55 | 55 | { |
| 56 | - $uri = (substr($uri,0,1) !=='/' and strlen($uri) > 0) ? "/{$uri}" : $uri; |
|
| 56 | + $uri = (substr($uri, 0, 1) !== '/' and strlen($uri) > 0) ? "/{$uri}" : $uri; |
|
| 57 | 57 | |
| 58 | - self::checkDuplicity($uri,$method); |
|
| 58 | + self::checkDuplicity($uri, $method); |
|
| 59 | 59 | |
| 60 | 60 | self::getInstance()->routes[] = [ |
| 61 | 61 | 'uri' => new Uri(self::getInstance()->host.self::getInstance()->prefix.$uri), |
@@ -74,8 +74,8 @@ discard block |
||
| 74 | 74 | |
| 75 | 75 | private static function checkDuplicity(string $uri, string $method): void |
| 76 | 76 | { |
| 77 | - foreach(self::getInstance()->routes as $route){ |
|
| 78 | - if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
| 77 | + foreach (self::getInstance()->routes as $route) { |
|
| 78 | + if (md5($route['uri'].$route['method']) === md5($uri.$method)) { |
|
| 79 | 79 | throw new \RuntimeException("There is already a route with the URI {$uri} and with the {$method} METHOD configured."); |
| 80 | 80 | } |
| 81 | 81 | } |
@@ -57,10 +57,10 @@ discard block |
||
| 57 | 57 | |
| 58 | 58 | self::checkDuplicity($uri,$method); |
| 59 | 59 | |
| 60 | - self::getInstance()->routes[] = [ |
|
| 61 | - 'uri' => new Uri(self::getInstance()->host.self::getInstance()->prefix.$uri), |
|
| 62 | - 'action' => $closure, |
|
| 63 | - 'method' => strtoupper($method), |
|
| 60 | + self::getInstance()->routes[] = [ |
|
| 61 | + 'uri' => new Uri(self::getInstance()->host.self::getInstance()->prefix.$uri), |
|
| 62 | + 'action' => $closure, |
|
| 63 | + 'method' => strtoupper($method), |
|
| 64 | 64 | 'middlewares' => null, |
| 65 | 65 | 'where' => null, |
| 66 | 66 | 'before' => [], |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | private static function checkDuplicity(string $uri, string $method): void |
| 76 | 76 | { |
| 77 | 77 | foreach(self::getInstance()->routes as $route){ |
| 78 | - if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
| 78 | + if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
| 79 | 79 | throw new \RuntimeException("There is already a route with the URI {$uri} and with the {$method} METHOD configured."); |
| 80 | 80 | } |
| 81 | 81 | } |
@@ -20,8 +20,8 @@ discard block |
||
| 20 | 20 | |
| 21 | 21 | public static function globalMiddlewares(array $middlewares): RouterInterface |
| 22 | 22 | { |
| 23 | - foreach($middlewares as $middleware){ |
|
| 24 | - if(!class_exists($middleware)){ |
|
| 23 | + foreach ($middlewares as $middleware) { |
|
| 24 | + if (!class_exists($middleware)) { |
|
| 25 | 25 | throw new \RuntimeException("Middleware class {$middleware} not exists"); |
| 26 | 26 | } |
| 27 | 27 | } |
@@ -31,16 +31,16 @@ discard block |
||
| 31 | 31 | |
| 32 | 32 | public static function middleware($middlewares): RouterInterface |
| 33 | 33 | { |
| 34 | - $middlewares = (is_array($middlewares)) ? $middlewares : [ $middlewares ]; |
|
| 34 | + $middlewares = (is_array($middlewares)) ? $middlewares : [$middlewares]; |
|
| 35 | 35 | $route = self::getInstance()->inSave(); |
| 36 | - $route['middlewares'] = (is_array($route['middlewares'])) ? array_merge($route['middlewares'],$middlewares) : $middlewares; |
|
| 37 | - self::getInstance()->updateRoute($route,array_key_last(self::getInstance()->routes)); |
|
| 36 | + $route['middlewares'] = (is_array($route['middlewares'])) ? array_merge($route['middlewares'], $middlewares) : $middlewares; |
|
| 37 | + self::getInstance()->updateRoute($route, array_key_last(self::getInstance()->routes)); |
|
| 38 | 38 | return self::getInstance(); |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | private static function existMiddleware(string $name): void |
| 42 | 42 | { |
| 43 | - if(!class_exists($name) && !array_key_exists($name,self::$globalMiddlewares)){ |
|
| 43 | + if (!class_exists($name) && !array_key_exists($name, self::$globalMiddlewares)) { |
|
| 44 | 44 | throw new \RuntimeException("Middleware {$name} does not exist"); |
| 45 | 45 | } |
| 46 | 46 | } |
@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | |
| 10 | 10 | /* NOTE: in case of error an exception is thrown */ |
| 11 | 11 | |
| 12 | -try{ |
|
| 12 | +try { |
|
| 13 | 13 | |
| 14 | 14 | Router::globalMiddlewares([ |
| 15 | 15 | 'Lasted'=> \Example\Middleware\Lasted::class |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | /* Return current action route */ |
| 27 | 27 | $action = Router::currentAction(); |
| 28 | 28 | |
| 29 | -}catch(Exception $er){ |
|
| 29 | +}catch (Exception $er) { |
|
| 30 | 30 | |
| 31 | 31 | die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}."); |
| 32 | 32 | |
@@ -26,7 +26,7 @@ |
||
| 26 | 26 | /* Return current action route */ |
| 27 | 27 | $action = Router::currentAction(); |
| 28 | 28 | |
| 29 | -}catch(Exception $er){ |
|
| 29 | +} catch(Exception $er){ |
|
| 30 | 30 | |
| 31 | 31 | die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}."); |
| 32 | 32 | |
@@ -8,23 +8,23 @@ |
||
| 8 | 8 | use Psr\Http\Server\MiddlewareInterface; |
| 9 | 9 | use Psr\Http\Server\RequestHandlerInterface; |
| 10 | 10 | |
| 11 | -class Middleware implements MiddlewareInterface{ |
|
| 11 | +class Middleware implements MiddlewareInterface { |
|
| 12 | 12 | protected static array $data = []; |
| 13 | 13 | protected static Response $response; |
| 14 | 14 | |
| 15 | 15 | public function __get($key) |
| 16 | 16 | { |
| 17 | - return (array_key_exists($key,self::$data)) ? self::$data[$key] : null; |
|
| 17 | + return (array_key_exists($key, self::$data)) ? self::$data[$key] : null; |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | - public function __set($key,$value) |
|
| 20 | + public function __set($key, $value) |
|
| 21 | 21 | { |
| 22 | 22 | self::$data[$key] = $value; |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
| 26 | 26 | { |
| 27 | - if(!isset(self::$response)){ |
|
| 27 | + if (!isset(self::$response)) { |
|
| 28 | 28 | self::$response = new Response(); |
| 29 | 29 | } |
| 30 | 30 | return self::$response; |
@@ -10,28 +10,28 @@ discard block |
||
| 10 | 10 | |
| 11 | 11 | protected function hasRouteName(string $name): void |
| 12 | 12 | { |
| 13 | - if(!isset($this->routesName[$name])){ |
|
| 13 | + if (!isset($this->routesName[$name])) { |
|
| 14 | 14 | throw new \RuntimeException("There is no route named with {$name}"); |
| 15 | 15 | } |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | protected function isInNameGroup(): void |
| 19 | 19 | { |
| 20 | - if(!is_null($this->group)){ |
|
| 20 | + if (!is_null($this->group)) { |
|
| 21 | 21 | throw new \RuntimeException("It is not allowed to assign names to groups"); |
| 22 | 22 | } |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | protected function isInPseudGroup(): void |
| 26 | 26 | { |
| 27 | - if(!is_null($this->group)){ |
|
| 27 | + if (!is_null($this->group)) { |
|
| 28 | 28 | throw new \RuntimeException("To assign actions before or after the execution of the route, use beforeGroup or afterGroup"); |
| 29 | 29 | } |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | protected function existRouteName(string $name): void |
| 33 | 33 | { |
| 34 | - if(isset($this->routesName[$name])){ |
|
| 34 | + if (isset($this->routesName[$name])) { |
|
| 35 | 35 | throw new \RuntimeException("There is already a route named with {$name}"); |
| 36 | 36 | } |
| 37 | 37 | } |
@@ -39,12 +39,12 @@ discard block |
||
| 39 | 39 | protected function checkMethod(array $route, $method): void |
| 40 | 40 | { |
| 41 | 41 | $hasMethod = false; |
| 42 | - foreach(explode('|',$route['method']) as $routeMethod){ |
|
| 43 | - if(@preg_match("/{$routeMethod}/",$method) !== 0 || $method === '*'){ |
|
| 42 | + foreach (explode('|', $route['method']) as $routeMethod) { |
|
| 43 | + if (@preg_match("/{$routeMethod}/", $method) !== 0 || $method === '*') { |
|
| 44 | 44 | $hasMethod = true; |
| 45 | 45 | } |
| 46 | 46 | } |
| 47 | - if(!$hasMethod){ |
|
| 47 | + if (!$hasMethod) { |
|
| 48 | 48 | throw new \Exception('This route is not released for the accessed method'); |
| 49 | 49 | } |
| 50 | 50 | |
@@ -2,7 +2,7 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace Example\Controllers; |
| 4 | 4 | |
| 5 | -class User{ |
|
| 5 | +class User { |
|
| 6 | 6 | |
| 7 | 7 | public function requireLogin($param, $param2): void |
| 8 | 8 | { |