feat(cli): lock captures effective description; list shows it; new commands surface view with origin and collision reporting
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
namespace Toolbox\Commands;
|
||||
|
||||
use Toolbox\Lock;
|
||||
use Toolbox\Ownership;
|
||||
|
||||
/**
|
||||
* Superficie ejecutable del proyecto: comandos en src/Commands con sus
|
||||
* aliases, descripciones y origen. A diferencia del despachador del bin,
|
||||
* las colisiones de alias aquí se REPORTAN (marcadas con !) y no abortan.
|
||||
*/
|
||||
final class CommandsCommand extends Command
|
||||
{
|
||||
public static function describe(): string
|
||||
{
|
||||
return 'Lista los comandos disponibles del proyecto';
|
||||
}
|
||||
|
||||
public function run(array $args): int
|
||||
{
|
||||
$parsed = $this->parse($args);
|
||||
$root = $this->root($parsed['options']);
|
||||
|
||||
$autoload = $root . '/autoload.php';
|
||||
if (!defined('ROOT_CORE') && is_file($autoload)) {
|
||||
$previous = getcwd();
|
||||
chdir($root);
|
||||
try {
|
||||
include_once $autoload;
|
||||
} catch (\Throwable) {
|
||||
// el proyecto no arranca: se lista sin clases cargables
|
||||
}
|
||||
if ($previous !== false) {
|
||||
chdir($previous);
|
||||
}
|
||||
}
|
||||
|
||||
$coreDir = defined('ROOT_CORE') ? rtrim(ROOT_CORE, '/') : $root . '/src';
|
||||
$dir = $coreDir . '/Commands';
|
||||
|
||||
if (!is_dir($dir)) {
|
||||
$this->console->out('El proyecto no tiene comandos en src/Commands.');
|
||||
return 0;
|
||||
}
|
||||
|
||||
$lock = Lock::load($root);
|
||||
$ownership = Ownership::fromLock($lock);
|
||||
|
||||
$info = [];
|
||||
$aliasOwners = [];
|
||||
|
||||
foreach (glob($dir . '/*.php') as $file) {
|
||||
$base = basename($file, '.php');
|
||||
$class = 'Commands\\' . $base;
|
||||
|
||||
if (!class_exists($class, true) || !is_a($class, 'Interfaces\\CommandInterface', true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$aliases = method_exists($class, 'aliases') ? $class::aliases() : [];
|
||||
foreach ($aliases as $alias) {
|
||||
$aliasOwners[strtolower((string) $alias)][] = $class;
|
||||
}
|
||||
|
||||
$owner = $ownership->ownerOf('src/Commands/' . $base . '.php');
|
||||
$origin = 'proyecto';
|
||||
if ($owner === 'core') {
|
||||
$origin = 'core';
|
||||
} elseif ($owner !== null) {
|
||||
$origin = $owner . '@' . ($lock->component($owner)['version'] ?? '?');
|
||||
}
|
||||
|
||||
$info[$base] = [
|
||||
'class' => $class,
|
||||
'describe' => $class::describe(),
|
||||
'aliases' => $aliases,
|
||||
'origin' => $origin,
|
||||
];
|
||||
}
|
||||
|
||||
$collisions = [];
|
||||
foreach ($aliasOwners as $alias => $owners) {
|
||||
$uniq = array_unique($owners);
|
||||
if (count($uniq) > 1) {
|
||||
$collisions[$alias] = $uniq;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($info as $base => $row) {
|
||||
foreach ($row['aliases'] as $alias) {
|
||||
$aKey = strtolower((string) $alias);
|
||||
foreach (array_keys($info) as $other) {
|
||||
if (strtolower($other) !== $aKey || $other === $base) {
|
||||
continue;
|
||||
}
|
||||
$owners = array_values(array_unique([...($collisions[$aKey] ?? []), $row['class'], $info[$other]['class']]));
|
||||
$collisions[$aKey] = $owners;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$rows = [['COMANDO', 'ALIASES', 'DESCRIPCIÓN', 'ORIGEN']];
|
||||
foreach ($info as $base => $row) {
|
||||
$marks = [];
|
||||
foreach ($row['aliases'] as $alias) {
|
||||
$marks[] = (isset($collisions[strtolower((string) $alias)]) ? $alias . '!' : (string) $alias);
|
||||
}
|
||||
$marked = $marks === [] ? '—' : implode(', ', $marks);
|
||||
if ($marked !== '—' && isset($collisions[strtolower($base)])) {
|
||||
$marked .= ' !';
|
||||
}
|
||||
|
||||
$rows[] = [strtolower($base), $marked, $row['describe'], $row['origin']];
|
||||
}
|
||||
|
||||
$this->console->table($rows);
|
||||
|
||||
foreach ($collisions as $alias => $owners) {
|
||||
$this->console->warn("alias '{$alias}' declarado o robado por: " . implode(', ', $owners));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user