From b1146a2c10bbbff681f7827105bf01ad0c72af57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0zni=20Burak=20Demirta=C5=9F?= Date: Sun, 14 Apr 2024 03:34:43 +0300 Subject: [PATCH] feat: v3.0.0 - min php version 8.1, refactor for php8 --- composer.json | 4 ++-- src/Http/Controller.php | 4 ++-- src/Router.php | 47 +++++++++++++++++++++-------------------- src/RouterCommand.php | 31 ++++++++++++++------------- src/RouterRequest.php | 10 ++++----- 5 files changed, 49 insertions(+), 47 deletions(-) diff --git a/composer.json b/composer.json index d050425..8cefdbc 100644 --- a/composer.json +++ b/composer.json @@ -17,9 +17,9 @@ } ], "require": { - "php": ">=7.2.5", + "php": "^8.1", "ext-json": "*", - "symfony/http-foundation": "^5.4.0" + "symfony/http-foundation": "^6.4" }, "require-dev": { "phpunit/phpunit": "^8.5 || ^9.4" diff --git a/src/Http/Controller.php b/src/Http/Controller.php index 5a88f92..bf96bf2 100644 --- a/src/Http/Controller.php +++ b/src/Http/Controller.php @@ -7,10 +7,10 @@ abstract class Controller extends Http /** * @var array Before Middlewares */ - public $middlewareBefore = []; + public array $middlewareBefore = []; /** * @var array After Middlewares */ - public $middlewareAfter = []; + public array $middlewareAfter = []; } \ No newline at end of file diff --git a/src/Router.php b/src/Router.php index 6856bb9..fedd660 100644 --- a/src/Router.php +++ b/src/Router.php @@ -40,19 +40,19 @@ class Router { /** Router Version */ - const VERSION = '2.5.2'; + const VERSION = '3.0.0'; /** @var string $baseFolder Base folder of the project */ - protected $baseFolder; + protected string $baseFolder; /** @var array $routes Routes list */ - protected $routes = []; + protected array $routes = []; /** @var array $groups List of group routes */ - protected $groups = []; + protected array $groups = []; /** @var array $patterns Pattern definitions for parameters of Route */ - protected $patterns = [ + protected array $patterns = [ ':all' => '(.*)', ':any' => '([^/]+)', ':id' => '(\d+)', @@ -67,46 +67,46 @@ class Router ]; /** @var array $namespaces Namespaces of Controllers and Middlewares files */ - protected $namespaces = [ + protected array $namespaces = [ 'middlewares' => '', 'controllers' => '', ]; /** @var array $path Paths of Controllers and Middlewares files */ - protected $paths = [ + protected array $paths = [ 'controllers' => 'Controllers', 'middlewares' => 'Middlewares', ]; /** @var string $mainMethod Main method for controller */ - protected $mainMethod = 'main'; + protected string $mainMethod = 'main'; /** @var string $cacheFile Cache file */ - protected $cacheFile = ''; + protected string $cacheFile = ''; /** @var bool $cacheLoaded Cache is loaded? */ - protected $cacheLoaded = false; + protected bool $cacheLoaded = false; /** @var Closure $errorCallback Route error callback function */ - protected $errorCallback; + protected Closure $errorCallback; /** @var Closure $notFoundCallback Route exception callback function */ - protected $notFoundCallback; + protected Closure $notFoundCallback; /** @var array $middlewares General middlewares for per request */ - protected $middlewares = []; + protected array $middlewares = []; /** @var array $routeMiddlewares Route middlewares */ - protected $routeMiddlewares = []; + protected array $routeMiddlewares = []; /** @var array $middlewareGroups Middleware Groups */ - protected $middlewareGroups = []; + protected array $middlewareGroups = []; /** @var RouterRequest */ - private $request; + private RouterRequest $request; /** @var bool */ - private $debug = false; + private bool $debug = false; /** * Router constructor method. @@ -170,7 +170,7 @@ public function __call($method, $params) [$route, $callback] = $params; $options = $params[2] ?? null; - if (strstr($route, ':')) { + if (str_contains($route, ':')) { $route1 = $route2 = ''; foreach (explode('/', $route) as $key => $value) { if ($value != '') { @@ -228,13 +228,13 @@ public function add(string $methods, string $route, $callback, array $options = /** * Add new route rules pattern; String or Array * - * @param string|array $pattern + * @param array|string $pattern * @param string|null $attr * * @return mixed * @throws */ - public function pattern($pattern, string $attr = null) + public function pattern(array|string $pattern, string $attr = null) { if (is_array($pattern)) { foreach ($pattern as $key => $value) { @@ -381,7 +381,7 @@ public function controller(string $route, string $controller, array $options = [ $classMethods = get_class_methods($controller); if ($classMethods) { foreach ($classMethods as $methodName) { - if (!strstr($methodName, '__')) { + if (!str_contains($methodName, '__')) { $method = 'any'; foreach (explode('|', $this->request->validMethods()) as $m) { if (stripos($methodName, $m = strtolower($m), 0) === 0) { @@ -741,11 +741,11 @@ protected function addRoute(string $uri, string $method, $callback, ?array $opti } /** - * @param array|string $middleware + * @param array|string|null $middleware * * @return array */ - protected function calculateMiddleware($middleware): array + protected function calculateMiddleware(array|string|null $middleware): array { if (is_null($middleware)) { return []; @@ -761,6 +761,7 @@ protected function calculateMiddleware($middleware): array * @param array $params * * @return void + * @throws Exception */ protected function runRouteCommand($command, array $params = []): void { diff --git a/src/RouterCommand.php b/src/RouterCommand.php index 4f6a027..82faf18 100644 --- a/src/RouterCommand.php +++ b/src/RouterCommand.php @@ -18,25 +18,25 @@ class RouterCommand protected static $instance = null; /** @var string */ - protected $baseFolder; + protected string $baseFolder; /** @var array */ - protected $paths; + protected array $paths; /** @var array */ - protected $namespaces; + protected array $namespaces; /** @var Request */ - protected $request; + protected Request $request; /** @var Response */ - protected $response; + protected Response $response; /** @var array */ - protected $middlewares = []; + protected array $middlewares = []; /** @var array */ - protected $markedMiddlewares = []; + protected array $markedMiddlewares = []; /** * RouterCommand constructor. @@ -177,11 +177,11 @@ public function beforeAfter($command) * @return mixed * @throws Exception */ - public function runRoute($command, array $params = []) + public function runRoute(string|Closure $command, array $params = []): mixed { $info = $this->getControllerInfo(); if (!is_object($command)) { - $invokable = strpos($command, '@') === false; + $invokable = !str_contains($command, '@'); $class = $command; if (!$invokable) { [$class, $method] = explode('@', $command); @@ -247,7 +247,7 @@ protected function resolveClass(string $class, string $path, string $namespace): * @return Response|mixed * @throws ReflectionException */ - protected function runMethodWithParams($function, array $params) + protected function runMethodWithParams(array|Closure $function, array $params): mixed { $reflection = is_array($function) ? new ReflectionMethod($function[0], $function[1]) @@ -298,6 +298,7 @@ protected function resolveCallbackParameters(Reflector $reflection, array $uriPa * * @return bool|void * @throws ReflectionException + * @throws Exception */ protected function runMiddleware(string $command, string $middleware, array $params, array $info) { @@ -307,7 +308,7 @@ protected function runMiddleware(string $command, string $middleware, array $par if (in_array($command, $this->markedMiddlewares)) { return true; } - array_push($this->markedMiddlewares, $command); + $this->markedMiddlewares[] = $command; if (!method_exists($controller, $middlewareMethod)) { $this->exception("{$middlewareMethod}() method is not found in {$middleware} class."); @@ -320,7 +321,7 @@ protected function runMiddleware(string $command, string $middleware, array $par exit; } - return $response; + return true; } /** @@ -328,7 +329,7 @@ protected function runMiddleware(string $command, string $middleware, array $par * * @return array|string */ - protected function resolveMiddleware(string $middleware) + protected function resolveMiddleware(string $middleware): array|string { $middlewares = $this->middlewares; if (isset($middlewares['middlewareGroups'][$middleware])) { @@ -348,9 +349,9 @@ protected function resolveMiddleware(string $middleware) * * @return Response|mixed */ - public function sendResponse($response) + public function sendResponse($response): mixed { - if (is_array($response) || strpos($this->request->headers->get('Accept') ?? '', 'application/json') !== false) { + if (is_array($response) || str_contains($this->request->headers->get('Accept') ?? '', 'application/json')) { $this->response->headers->set('Content-Type', 'application/json'); return $this->response ->setContent($response instanceof Response ? $response->getContent() : json_encode($response)) diff --git a/src/RouterRequest.php b/src/RouterRequest.php index 03de508..95fc09c 100644 --- a/src/RouterRequest.php +++ b/src/RouterRequest.php @@ -8,13 +8,13 @@ class RouterRequest { /** @var string $validMethods Valid methods for Router */ - protected $validMethods = 'GET|POST|PUT|DELETE|HEAD|OPTIONS|PATCH|ANY|AJAX|XGET|XPOST|XPUT|XDELETE|XPATCH'; + protected string $validMethods = 'GET|POST|PUT|DELETE|HEAD|OPTIONS|PATCH|ANY|AJAX|XGET|XPOST|XPUT|XDELETE|XPATCH'; /** @var Request $request */ - private $request; + private Request $request; /** @var Response $response */ - private $response; + private Response $response; /** * RouterRequest constructor. @@ -63,7 +63,7 @@ public function validMethods(): string public function validMethod(string $data, string $method): bool { $valid = false; - if (strstr($data, '|')) { + if (str_contains($data, '|')) { foreach (explode('|', $data) as $value) { $valid = $this->checkMethods($value, $method); if ($valid) { @@ -108,7 +108,7 @@ protected function checkMethods(string $value, string $method): bool return true; } - if ($this->request->isXmlHttpRequest() && strpos($value, 'X') === 0 + if ($this->request->isXmlHttpRequest() && str_starts_with($value, 'X') && $method === ltrim($value, 'X')) { return true; }