Skip to content

Commit cbfda83

Browse files
committed
patch: self instanciation des commandes
1 parent d3273db commit cbfda83

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

src/Cli/Console/Console.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Composer\InstalledVersions;
1717
use Dimtrovich\Console\Application;
1818
use Psr\Log\LoggerInterface;
19+
use ReflectionClass;
1920

2021
use function Ahc\Cli\t;
2122

@@ -37,7 +38,7 @@ public function __construct(ContainerInterface $container)
3738
$this->app = Application::create(static::APP_NAME, static::APP_VERSION)
3839
->withContainer($container)
3940
->withLogger($container->get(LoggerInterface::class), static::CONSOLE_NAME)
40-
->withCommands($this->discoverCommands($container->get(LocatorInterface::class)))
41+
->withCommandInstances($this->discoverCommands($container))
4142
->withLocale(config('app.locale'))
4243
->withTheme(config('klinge.theme', 'monokai'))
4344
->withStyles(config('klinge.styles', []))
@@ -106,29 +107,34 @@ protected function files(LocatorInterface $locator): array
106107
* Parcourt les dossiers Commands/ et Cli/Commands/ pour trouver
107108
* toutes les classes qui étendent Command.
108109
*
109-
* @param LocatorInterface $locator Service de localisation de fichiers
110-
*
111-
* @return list<class-string<Command>> Liste des classes de commandes découvertes
110+
* @return list<Command> Liste des instances de commandes découvertes
112111
*/
113-
private function discoverCommands(LocatorInterface $locator): array
112+
private function discoverCommands(ContainerInterface $container): array
114113
{
115114
if ($this->discovered) {
116115
return [];
117116
}
118117

119-
$classes = [];
118+
/** @var LocatorInterface */
119+
$locator = $container->get(LocatorInterface::class);
120+
121+
$commands = [];
120122

121123
foreach ($this->files($locator) as $file) {
122-
$className = $locator->findQualifiedNameFromPath($file);
124+
if (false === $className = $locator->findQualifiedNameFromPath($file)) {
125+
continue;
126+
}
127+
128+
$class = new ReflectionClass($className);
123129

124-
if ($className && is_subclass_of($className, Command::class, true)) {
125-
$classes[] = $className;
130+
if ($class->isInstantiable() && $class->isSubclassOf(Command::class)) {
131+
$commands[] = $container->make($className);
126132
}
127133
}
128134

129135
$this->discovered = true;
130136

131-
return $classes;
137+
return $commands;
132138
}
133139

134140
/**

0 commit comments

Comments
 (0)