Skip to content

Commit b1146a2

Browse files
committed
feat: v3.0.0 - min php version 8.1, refactor for php8
1 parent fd48a01 commit b1146a2

File tree

5 files changed

+49
-47
lines changed

5 files changed

+49
-47
lines changed

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
}
1818
],
1919
"require": {
20-
"php": ">=7.2.5",
20+
"php": "^8.1",
2121
"ext-json": "*",
22-
"symfony/http-foundation": "^5.4.0"
22+
"symfony/http-foundation": "^6.4"
2323
},
2424
"require-dev": {
2525
"phpunit/phpunit": "^8.5 || ^9.4"

src/Http/Controller.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ abstract class Controller extends Http
77
/**
88
* @var array Before Middlewares
99
*/
10-
public $middlewareBefore = [];
10+
public array $middlewareBefore = [];
1111

1212
/**
1313
* @var array After Middlewares
1414
*/
15-
public $middlewareAfter = [];
15+
public array $middlewareAfter = [];
1616
}

src/Router.php

+24-23
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,19 @@
4040
class Router
4141
{
4242
/** Router Version */
43-
const VERSION = '2.5.2';
43+
const VERSION = '3.0.0';
4444

4545
/** @var string $baseFolder Base folder of the project */
46-
protected $baseFolder;
46+
protected string $baseFolder;
4747

4848
/** @var array $routes Routes list */
49-
protected $routes = [];
49+
protected array $routes = [];
5050

5151
/** @var array $groups List of group routes */
52-
protected $groups = [];
52+
protected array $groups = [];
5353

5454
/** @var array $patterns Pattern definitions for parameters of Route */
55-
protected $patterns = [
55+
protected array $patterns = [
5656
':all' => '(.*)',
5757
':any' => '([^/]+)',
5858
':id' => '(\d+)',
@@ -67,46 +67,46 @@ class Router
6767
];
6868

6969
/** @var array $namespaces Namespaces of Controllers and Middlewares files */
70-
protected $namespaces = [
70+
protected array $namespaces = [
7171
'middlewares' => '',
7272
'controllers' => '',
7373
];
7474

7575
/** @var array $path Paths of Controllers and Middlewares files */
76-
protected $paths = [
76+
protected array $paths = [
7777
'controllers' => 'Controllers',
7878
'middlewares' => 'Middlewares',
7979
];
8080

8181
/** @var string $mainMethod Main method for controller */
82-
protected $mainMethod = 'main';
82+
protected string $mainMethod = 'main';
8383

8484
/** @var string $cacheFile Cache file */
85-
protected $cacheFile = '';
85+
protected string $cacheFile = '';
8686

8787
/** @var bool $cacheLoaded Cache is loaded? */
88-
protected $cacheLoaded = false;
88+
protected bool $cacheLoaded = false;
8989

9090
/** @var Closure $errorCallback Route error callback function */
91-
protected $errorCallback;
91+
protected Closure $errorCallback;
9292

9393
/** @var Closure $notFoundCallback Route exception callback function */
94-
protected $notFoundCallback;
94+
protected Closure $notFoundCallback;
9595

9696
/** @var array $middlewares General middlewares for per request */
97-
protected $middlewares = [];
97+
protected array $middlewares = [];
9898

9999
/** @var array $routeMiddlewares Route middlewares */
100-
protected $routeMiddlewares = [];
100+
protected array $routeMiddlewares = [];
101101

102102
/** @var array $middlewareGroups Middleware Groups */
103-
protected $middlewareGroups = [];
103+
protected array $middlewareGroups = [];
104104

105105
/** @var RouterRequest */
106-
private $request;
106+
private RouterRequest $request;
107107

108108
/** @var bool */
109-
private $debug = false;
109+
private bool $debug = false;
110110

111111
/**
112112
* Router constructor method.
@@ -170,7 +170,7 @@ public function __call($method, $params)
170170

171171
[$route, $callback] = $params;
172172
$options = $params[2] ?? null;
173-
if (strstr($route, ':')) {
173+
if (str_contains($route, ':')) {
174174
$route1 = $route2 = '';
175175
foreach (explode('/', $route) as $key => $value) {
176176
if ($value != '') {
@@ -228,13 +228,13 @@ public function add(string $methods, string $route, $callback, array $options =
228228
/**
229229
* Add new route rules pattern; String or Array
230230
*
231-
* @param string|array $pattern
231+
* @param array|string $pattern
232232
* @param string|null $attr
233233
*
234234
* @return mixed
235235
* @throws
236236
*/
237-
public function pattern($pattern, string $attr = null)
237+
public function pattern(array|string $pattern, string $attr = null)
238238
{
239239
if (is_array($pattern)) {
240240
foreach ($pattern as $key => $value) {
@@ -381,7 +381,7 @@ public function controller(string $route, string $controller, array $options = [
381381
$classMethods = get_class_methods($controller);
382382
if ($classMethods) {
383383
foreach ($classMethods as $methodName) {
384-
if (!strstr($methodName, '__')) {
384+
if (!str_contains($methodName, '__')) {
385385
$method = 'any';
386386
foreach (explode('|', $this->request->validMethods()) as $m) {
387387
if (stripos($methodName, $m = strtolower($m), 0) === 0) {
@@ -741,11 +741,11 @@ protected function addRoute(string $uri, string $method, $callback, ?array $opti
741741
}
742742

743743
/**
744-
* @param array|string $middleware
744+
* @param array|string|null $middleware
745745
*
746746
* @return array
747747
*/
748-
protected function calculateMiddleware($middleware): array
748+
protected function calculateMiddleware(array|string|null $middleware): array
749749
{
750750
if (is_null($middleware)) {
751751
return [];
@@ -761,6 +761,7 @@ protected function calculateMiddleware($middleware): array
761761
* @param array $params
762762
*
763763
* @return void
764+
* @throws Exception
764765
*/
765766
protected function runRouteCommand($command, array $params = []): void
766767
{

src/RouterCommand.php

+16-15
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,25 @@ class RouterCommand
1818
protected static $instance = null;
1919

2020
/** @var string */
21-
protected $baseFolder;
21+
protected string $baseFolder;
2222

2323
/** @var array */
24-
protected $paths;
24+
protected array $paths;
2525

2626
/** @var array */
27-
protected $namespaces;
27+
protected array $namespaces;
2828

2929
/** @var Request */
30-
protected $request;
30+
protected Request $request;
3131

3232
/** @var Response */
33-
protected $response;
33+
protected Response $response;
3434

3535
/** @var array */
36-
protected $middlewares = [];
36+
protected array $middlewares = [];
3737

3838
/** @var array */
39-
protected $markedMiddlewares = [];
39+
protected array $markedMiddlewares = [];
4040

4141
/**
4242
* RouterCommand constructor.
@@ -177,11 +177,11 @@ public function beforeAfter($command)
177177
* @return mixed
178178
* @throws Exception
179179
*/
180-
public function runRoute($command, array $params = [])
180+
public function runRoute(string|Closure $command, array $params = []): mixed
181181
{
182182
$info = $this->getControllerInfo();
183183
if (!is_object($command)) {
184-
$invokable = strpos($command, '@') === false;
184+
$invokable = !str_contains($command, '@');
185185
$class = $command;
186186
if (!$invokable) {
187187
[$class, $method] = explode('@', $command);
@@ -247,7 +247,7 @@ protected function resolveClass(string $class, string $path, string $namespace):
247247
* @return Response|mixed
248248
* @throws ReflectionException
249249
*/
250-
protected function runMethodWithParams($function, array $params)
250+
protected function runMethodWithParams(array|Closure $function, array $params): mixed
251251
{
252252
$reflection = is_array($function)
253253
? new ReflectionMethod($function[0], $function[1])
@@ -298,6 +298,7 @@ protected function resolveCallbackParameters(Reflector $reflection, array $uriPa
298298
*
299299
* @return bool|void
300300
* @throws ReflectionException
301+
* @throws Exception
301302
*/
302303
protected function runMiddleware(string $command, string $middleware, array $params, array $info)
303304
{
@@ -307,7 +308,7 @@ protected function runMiddleware(string $command, string $middleware, array $par
307308
if (in_array($command, $this->markedMiddlewares)) {
308309
return true;
309310
}
310-
array_push($this->markedMiddlewares, $command);
311+
$this->markedMiddlewares[] = $command;
311312

312313
if (!method_exists($controller, $middlewareMethod)) {
313314
$this->exception("{$middlewareMethod}() method is not found in {$middleware} class.");
@@ -320,15 +321,15 @@ protected function runMiddleware(string $command, string $middleware, array $par
320321
exit;
321322
}
322323

323-
return $response;
324+
return true;
324325
}
325326

326327
/**
327328
* @param string $middleware
328329
*
329330
* @return array|string
330331
*/
331-
protected function resolveMiddleware(string $middleware)
332+
protected function resolveMiddleware(string $middleware): array|string
332333
{
333334
$middlewares = $this->middlewares;
334335
if (isset($middlewares['middlewareGroups'][$middleware])) {
@@ -348,9 +349,9 @@ protected function resolveMiddleware(string $middleware)
348349
*
349350
* @return Response|mixed
350351
*/
351-
public function sendResponse($response)
352+
public function sendResponse($response): mixed
352353
{
353-
if (is_array($response) || strpos($this->request->headers->get('Accept') ?? '', 'application/json') !== false) {
354+
if (is_array($response) || str_contains($this->request->headers->get('Accept') ?? '', 'application/json')) {
354355
$this->response->headers->set('Content-Type', 'application/json');
355356
return $this->response
356357
->setContent($response instanceof Response ? $response->getContent() : json_encode($response))

src/RouterRequest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
class RouterRequest
99
{
1010
/** @var string $validMethods Valid methods for Router */
11-
protected $validMethods = 'GET|POST|PUT|DELETE|HEAD|OPTIONS|PATCH|ANY|AJAX|XGET|XPOST|XPUT|XDELETE|XPATCH';
11+
protected string $validMethods = 'GET|POST|PUT|DELETE|HEAD|OPTIONS|PATCH|ANY|AJAX|XGET|XPOST|XPUT|XDELETE|XPATCH';
1212

1313
/** @var Request $request */
14-
private $request;
14+
private Request $request;
1515

1616
/** @var Response $response */
17-
private $response;
17+
private Response $response;
1818

1919
/**
2020
* RouterRequest constructor.
@@ -63,7 +63,7 @@ public function validMethods(): string
6363
public function validMethod(string $data, string $method): bool
6464
{
6565
$valid = false;
66-
if (strstr($data, '|')) {
66+
if (str_contains($data, '|')) {
6767
foreach (explode('|', $data) as $value) {
6868
$valid = $this->checkMethods($value, $method);
6969
if ($valid) {
@@ -108,7 +108,7 @@ protected function checkMethods(string $value, string $method): bool
108108
return true;
109109
}
110110

111-
if ($this->request->isXmlHttpRequest() && strpos($value, 'X') === 0
111+
if ($this->request->isXmlHttpRequest() && str_starts_with($value, 'X')
112112
&& $method === ltrim($value, 'X')) {
113113
return true;
114114
}

0 commit comments

Comments
 (0)