Skip to content

Commit

Permalink
feat: v3.0.0 - min php version 8.1, refactor for php8
Browse files Browse the repository at this point in the history
  • Loading branch information
izniburak committed Apr 14, 2024
1 parent fd48a01 commit b1146a2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 47 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
}
47 changes: 24 additions & 23 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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+)',
Expand All @@ -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.
Expand Down Expand Up @@ -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 != '') {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 [];
Expand All @@ -761,6 +761,7 @@ protected function calculateMiddleware($middleware): array
* @param array $params
*
* @return void
* @throws Exception
*/
protected function runRouteCommand($command, array $params = []): void
{
Expand Down
31 changes: 16 additions & 15 deletions src/RouterCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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)
{
Expand All @@ -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.");
Expand All @@ -320,15 +321,15 @@ protected function runMiddleware(string $command, string $middleware, array $par
exit;
}

return $response;
return true;
}

/**
* @param string $middleware
*
* @return array|string
*/
protected function resolveMiddleware(string $middleware)
protected function resolveMiddleware(string $middleware): array|string
{
$middlewares = $this->middlewares;
if (isset($middlewares['middlewareGroups'][$middleware])) {
Expand All @@ -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))
Expand Down
10 changes: 5 additions & 5 deletions src/RouterRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit b1146a2

Please sign in to comment.