Skip to content

Commit

Permalink
Better way to extract Command list
Browse files Browse the repository at this point in the history
  • Loading branch information
oallain committed Sep 9, 2023
1 parent e4f3bf7 commit 1a36a44
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 42 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
"dealerdirect/phpcodesniffer-composer-installer": true,
"symfony/thanks": true,
"phpro/grumphp": true,
"phpstan/extension-installer": true
"phpstan/extension-installer": true,
"ergebnis/composer-normalize": true
}
},
"scripts": {
Expand Down
47 changes: 7 additions & 40 deletions src/Parser/CommandParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
namespace Synolia\SyliusSchedulerCommandPlugin\Parser;

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\HttpKernel\KernelInterface;
use Webmozart\Assert\Assert;

Expand All @@ -24,47 +22,16 @@ public function __construct(private KernelInterface $kernel, array $excludedName
public function getCommands(): array
{
$application = new Application($this->kernel);
$application->setAutoExit(false);

$input = new ArrayInput(
[
'command' => 'list',
'--format' => 'json',
],
);

$stream = fopen('php://memory', 'w+');

if ($stream === false) {
throw new \Exception('PHP Memory stream not available');
}

$output = new StreamOutput($stream);
$application->run($input, $output);
rewind($output->getStream());

return $this->extractCommandsFromJson((string) stream_get_contents($output->getStream()));
}

private function extractCommandsFromJson(string $string): array
{
if ($string === '') {
return [];
}

$node = \json_decode($string, null, 512, \JSON_THROW_ON_ERROR);
$commandsList = [];

if (null === $node || !\is_array($node->namespaces)) {
return [];
}

foreach ($node->namespaces as $namespace) {
if (!in_array($namespace->id, $this->excludedNamespaces, true)) {
foreach ($namespace->commands as $command) {
$commandsList[$namespace->id][$command] = $command;
}
$commands = $application->all();
foreach ($commands as $command) {
$name = $command->getName() ?? '';
$namespace = \explode(':', $name)[0];
if (in_array($namespace, $this->excludedNamespaces, true)) {
continue;
}
$commandsList[$namespace][$command->getName()] = $name;
}

return $commandsList;
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ services:

Synolia\SyliusSchedulerCommandPlugin\Parser\CommandParser:
arguments:
$excludedNamespaces: ['_global']
$excludedNamespaces: ['_complete']

app.grid_field.scheduled_command.url:
class: Synolia\SyliusSchedulerCommandPlugin\Grid\FieldType\ScheduledCommandUrlType
Expand Down

0 comments on commit 1a36a44

Please # to comment.