First Commit
This commit is contained in:
Executable
+77
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Helper\Table;
|
||||
use Kernel\Application;
|
||||
use Kernel\ModuleLoader;
|
||||
|
||||
class RouteListCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'route:list';
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->setDescription('Lists all routes registered in the application.');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
// We need to instantiate the application to load the routes
|
||||
// But be careful not to execute session_start() if it is already active or causes output
|
||||
|
||||
// Simulate application environment
|
||||
if (session_status() == PHP_SESSION_NONE) {
|
||||
// Avoid headers error if something attempts to start session
|
||||
// But Application initSession attempts to start.
|
||||
// Let's suppress session errors for the command
|
||||
@session_start();
|
||||
}
|
||||
|
||||
$app = new Application();
|
||||
|
||||
// The Application constructor already loads routes via initRouter and initModuleLoader
|
||||
// But we must ensure it loads everything that adds routes
|
||||
|
||||
// Since Application is a singleton, we can try to get the instance or create it
|
||||
// If we create new Application(), it runs the constructor.
|
||||
|
||||
$routes = $app->getRoutes();
|
||||
|
||||
if (empty($routes)) {
|
||||
$output->writeln('<info>Nenhuma rota encontrada.</info>');
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
$table = new Table($output);
|
||||
$table->setHeaders(['Método', 'URI', 'Handler']);
|
||||
|
||||
foreach ($routes as $route) {
|
||||
[$method, $uri, $handler] = $route;
|
||||
|
||||
$handlerName = 'Closure';
|
||||
if (is_array($handler)) {
|
||||
if (is_object($handler[0])) {
|
||||
$handlerName = get_class($handler[0]) . '@' . $handler[1];
|
||||
} elseif (is_string($handler[0])) {
|
||||
$handlerName = $handler[0] . '@' . $handler[1];
|
||||
}
|
||||
} elseif (is_string($handler)) {
|
||||
$handlerName = $handler;
|
||||
}
|
||||
|
||||
$table->addRow([
|
||||
$method,
|
||||
$uri,
|
||||
$handlerName
|
||||
]);
|
||||
}
|
||||
|
||||
$table->render();
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user