|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of web-stack |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Slick\WebStack\UserInterface\Console; |
|
13
|
|
|
|
|
14
|
|
|
use Slick\Http\Message\Server\Request; |
|
15
|
|
|
use Slick\ModuleApi\Infrastructure\FrontController\MiddlewareHandlerInterface; |
|
16
|
|
|
use Slick\ModuleApi\Infrastructure\FrontController\Position; |
|
17
|
|
|
use Slick\WebStack\Infrastructure\FrontController\WebApplication; |
|
18
|
|
|
use Symfony\Component\Console\Attribute\AsCommand; |
|
19
|
|
|
use Symfony\Component\Console\Command\Command; |
|
20
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
22
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* StackDisplayCommand |
|
26
|
|
|
* |
|
27
|
|
|
* @package Slick\WebStack\UserInterface\Console |
|
28
|
|
|
*/ |
|
29
|
|
|
#[AsCommand( |
|
30
|
|
|
name: "app:http-stack", |
|
31
|
|
|
description: "Displays the list of the middlewares configured on the HTTP stack.", |
|
32
|
|
|
aliases: ["stack"] |
|
33
|
|
|
)] |
|
34
|
|
|
final class StackDisplayCommand extends Command |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* Executes the command. |
|
38
|
|
|
* |
|
39
|
|
|
* @param InputInterface $input The input object. |
|
40
|
|
|
* @param OutputInterface $output The output object. |
|
41
|
|
|
* |
|
42
|
|
|
* @return int The exit code of the command. |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
45
|
|
|
{ |
|
46
|
|
|
$style = new SymfonyStyle($input, $output); |
|
47
|
|
|
$app = new WebApplication(new Request("GET", "/"), APP_ROOT); |
|
48
|
|
|
$app->loadModules(); |
|
49
|
|
|
$app->loadMiddlewares(); |
|
50
|
|
|
$list = $app->middlewareList(); |
|
51
|
|
|
$table = $style->createTable(); |
|
52
|
|
|
$table->setHeaderTitle("HTTP Stack Middlewares"); |
|
53
|
|
|
$table->setHeaders(["Name", "Middleware", "Position"]); |
|
54
|
|
|
/** @var MiddlewareHandlerInterface $middleware */ |
|
55
|
|
|
foreach ($list as $middleware) { |
|
56
|
|
|
$table->addRow([ |
|
57
|
|
|
$middleware->name(), |
|
58
|
|
|
$this->parseMiddlewareClass($middleware), |
|
59
|
|
|
$this->parsePosition($middleware) |
|
60
|
|
|
]); |
|
61
|
|
|
} |
|
62
|
|
|
$style->writeln(''); |
|
63
|
|
|
$table->render(); |
|
64
|
|
|
$style->write(" <info>Modules file: </info>{$app->rootPath()}".$app::MODULES_PATH."\n"); |
|
65
|
|
|
return Command::SUCCESS; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function parseMiddlewareClass(MiddlewareHandlerInterface $middleware): string |
|
69
|
|
|
{ |
|
70
|
|
|
if (is_string($middleware->handler())) { |
|
71
|
|
|
return $middleware->handler(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (is_callable($middleware->handler())) { |
|
75
|
|
|
return "--Callable--"; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return get_class($middleware->handler()); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function parsePosition(MiddlewareHandlerInterface $middleware): string |
|
82
|
|
|
{ |
|
83
|
|
|
$reference = $middleware->middlewarePosition()->reference(); |
|
84
|
|
|
$refCheck = fn (?string $ref) => $ref ? " of $ref" : ""; |
|
85
|
|
|
|
|
86
|
|
|
return match ($middleware->middlewarePosition()->position()->value) { |
|
87
|
|
|
Position::Top->value => "On top{$refCheck($reference)}", |
|
88
|
|
|
Position::Bottom->value => "On bottom{$refCheck($reference)}", |
|
89
|
|
|
Position::After->value => "After $reference", |
|
90
|
|
|
Position::Before->value => "Before $reference", |
|
91
|
|
|
}; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|