Files
oficial/cli/src/Service/Commands.php
aldarien 307f2ac7d7 feature/cierres (#25)
Varios cambios

Co-authored-by: Juan Pablo Vial <jpvialb@incoviba.cl>
Reviewed-on: #25
2025-07-22 13:18:00 +00:00

52 lines
1.7 KiB
PHP

<?php
namespace Incoviba\Service;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use ReflectionClass;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
class Commands
{
public function __construct(protected LoggerInterface $logger, public ?string $baseCommandsPath = null, public array $skipCommands = [])
{
if ($this->baseCommandsPath === null) {
$this->baseCommandsPath = implode(DIRECTORY_SEPARATOR, [
dirname(__DIR__, 1),
'Command'
]);
}
$this->baseCommandsPath = realpath($this->baseCommandsPath);
}
public function getCommandsList(): array
{
$commands = [];
$files = new RecursiveIteratorIterator((new RecursiveDirectoryIterator($this->baseCommandsPath)));
foreach ($files as $file) {
if ($file->isDir()) {
continue;
}
$basename = ltrim(str_replace(DIRECTORY_SEPARATOR, "\\",
str_replace([$this->baseCommandsPath, '.php'], '', $file->getRealPath())), "\\");
$namespace = "Incoviba\\Command";
$class = "{$namespace}\\{$basename}";
if (!class_exists($class)) {
$this->logger->error("Class {$class} not found");
continue;
}
$ref = new ReflectionClass($class);
$commandData = $ref->getAttributes(AsCommand::class)[0];
$commandName = $commandData->getArguments()['name'];
if (in_array($commandName, $this->skipCommands) or in_array($class, $this->skipCommands)) {
continue;
}
$commands[$commandName] = $class;
}
return $commands;
}
}