Skip to content

Commit 1ce8871

Browse files
committed
Adds command manager implementation for Spiral Framework
1 parent 31f9cb4 commit 1ce8871

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
},
1111
"require-dev": {
1212
"phpunit/phpunit": "^11.3",
13+
"psr/container": "^2.0",
1314
"spiral/boot": "^3.13",
15+
"spiral/console": "^3.13",
1416
"illuminate/support": "^11.0"
1517
},
1618
"autoload": {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LLM\Agents\Agent\SymfonyConsole\Integrations\Spiral;
6+
7+
use LLM\Agents\Agent\SymfonyConsole\CommandManagerInterface;
8+
use Psr\Container\ContainerInterface;
9+
use Spiral\Console\Console;
10+
use Symfony\Component\Console\Output\BufferedOutput;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
13+
final readonly class CommandManager implements CommandManagerInterface
14+
{
15+
public function __construct(
16+
private ContainerInterface $app,
17+
private array $enabledNamespaces = [
18+
'create:',
19+
'cycle:',
20+
'migrate',
21+
'route:list',
22+
],
23+
) {}
24+
25+
private function getConsole(): Console
26+
{
27+
return $this->app->get(Console::class);
28+
}
29+
30+
public function getCommandHelp(string $command): string
31+
{
32+
$output = new BufferedOutput();
33+
$this->call($command, ['--help' => true], $output);
34+
35+
return $output->fetch();
36+
}
37+
38+
public function getCommands(): array
39+
{
40+
$commands = $this->getConsole()->getApplication()->all();
41+
42+
$availableCommands = [];
43+
foreach ($this->enabledNamespaces as $namespace) {
44+
foreach ($commands as $name => $command) {
45+
if (\str_starts_with($name, $namespace)) {
46+
$availableCommands[$name] = $command;
47+
}
48+
}
49+
}
50+
51+
return $availableCommands;
52+
}
53+
54+
public function call(\Stringable|string $command, array $parameters = [], ?OutputInterface $output = null): int
55+
{
56+
return $this->getConsole()->run((string) $command, $parameters, $output)->getCode();
57+
}
58+
}

src/Integrations/Spiral/SymfonyConsoleBootloader.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function defineSingletons(): array
2222
{
2323
return [
2424
SymfonyConsoleAgentFactory::class => static fn() => new SymfonyConsoleAgentFactory('Spiral'),
25+
CommandManagerInterface::class => CommandManager::class,
2526
];
2627
}
2728

0 commit comments

Comments
 (0)