|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Blitz PHP framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the LICENSE file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace BlitzPHP\Cli\Commands\Utilities; |
|
13
|
|
|
|
|
14
|
|
|
use BlitzPHP\Cli\Commands\Routes\MiddlewareCollector; |
|
15
|
|
|
use BlitzPHP\Cli\Console\Command; |
|
16
|
|
|
use BlitzPHP\Container\Services; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* verifie les middleware d'une route. |
|
20
|
|
|
*/ |
|
21
|
|
|
class MiddlewareCheck extends Command |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string Groupe |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $group = 'BlitzPHP'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string Nom |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $name = 'middleware:check'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string Description |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $description = 'Vérifiez les middleware d\'une route.'; |
|
37
|
|
|
|
|
38
|
|
|
protected $service = 'Service de configuration'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Arguments de la commande |
|
42
|
|
|
* |
|
43
|
|
|
* @var array<string, string> |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $arguments = [ |
|
46
|
|
|
'method' => 'La methode HTTP. get, post, put, etc.', |
|
47
|
|
|
'route' => 'La route (chemin d\'URI) pour vérifier les middlewares.', |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritDoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function execute(array $params) |
|
54
|
|
|
{ |
|
55
|
2 |
|
$method = strtolower($this->argument('method', $params[0] ?? '')); |
|
56
|
2 |
|
$route = $this->argument('route', $params[1] ?? ''); |
|
57
|
|
|
|
|
58
|
|
|
if (empty($route) || $method === '') { |
|
59
|
2 |
|
$this->fail('Vous devez spécifier un verbe HTTP et une route.')->eol(); |
|
60
|
|
|
$this->write('Exemple: middleware:check get /')->eol(); |
|
61
|
|
|
$this->write(' middleware:check put products/1'); |
|
62
|
|
|
|
|
63
|
|
|
return EXIT_ERROR; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// Chargement des routes |
|
67
|
2 |
|
Services::routes()->loadRoutes(); |
|
68
|
|
|
|
|
69
|
2 |
|
$middlewareCollector = new MiddlewareCollector(); |
|
70
|
|
|
|
|
71
|
2 |
|
$middlewares = $middlewareCollector->get($method, $route); |
|
72
|
|
|
|
|
73
|
|
|
// PageNotFoundException |
|
74
|
|
|
if ($middlewares === ['<unknown>']) { |
|
75
|
2 |
|
$this->fail('Impossible de trouver une route: '); |
|
76
|
2 |
|
$this->colorize('"' . strtoupper($method) . ' ' . $route . '"', 'black'); |
|
77
|
|
|
|
|
78
|
2 |
|
return EXIT_ERROR; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$this->table([ |
|
82
|
|
|
[ |
|
83
|
|
|
'Methode' => strtoupper($method), |
|
84
|
|
|
'Route' => $route, |
|
85
|
|
|
'Middlewares' => implode(' ', $middlewares), |
|
86
|
|
|
], |
|
87
|
2 |
|
]); |
|
88
|
|
|
|
|
89
|
2 |
|
return EXIT_SUCCESS; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|