Skip to content

Commit

Permalink
add defaultCommand attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
verfriemelt-dot-org committed Dec 29, 2023
1 parent 5e79e53 commit 6b87d21
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
13 changes: 10 additions & 3 deletions _/AbstractKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use verfriemelt\wrapped\_\Cli\Console;
use verfriemelt\wrapped\_\Command\AbstractCommand;
use verfriemelt\wrapped\_\Command\Attributes\Command;
use verfriemelt\wrapped\_\Command\Attributes\DefaultCommand;
use verfriemelt\wrapped\_\Command\CommandArguments\ArgvParser;
use verfriemelt\wrapped\_\DI\ArgumentMetadataFactory;
use verfriemelt\wrapped\_\DI\ArgumentResolver;
Expand All @@ -29,6 +30,8 @@

abstract class AbstractKernel implements KernelInterface
{
protected const string DEFAULT_COMMAND = '_';

protected Router $router;

/** @var array<string,class-string> */
Expand All @@ -46,6 +49,8 @@ public function __construct()
$this->eventDispatcher = $this->container->get(EventDispatcher::class);

$this->initializeErrorHandling();

$this->loadCommands(__DIR__ . '/Command', __DIR__, __NAMESPACE__);
}

public function getContainer(): Container
Expand Down Expand Up @@ -137,16 +142,14 @@ public function handle(Request $request): Response

public function execute(Console $cli): never
{
$this->loadCommands(__DIR__ . '/Command', __DIR__, __NAMESPACE__);

$this->container->register(Console::class, $cli);
$arguments = $cli->getArgv()->all();

// scriptname
\array_shift($arguments);

// command name
$commandName = \array_shift($arguments);
$commandName = \array_shift($arguments) ?? self::DEFAULT_COMMAND;
assert(is_string($commandName));
$commandInstance = $this->container->get($this->commands[$commandName] ?? throw new RuntimeException("command {$commandName} not found"));

Expand Down Expand Up @@ -199,6 +202,10 @@ public function loadCommands(string $path, string $pathPrefix, string $namespace

foreach ($commands as $command) {
$reflection = new ReflectionClass($command);
$defaultAttribute = $reflection->getAttributes(DefaultCommand::class)[0] ?? null;
if ($defaultAttribute !== null) {
$this->commands[self::DEFAULT_COMMAND] = $command;
}

foreach ($reflection->getAttributes(Command::class) as $attribute) {
$instance = $attribute->newInstance();
Expand Down
10 changes: 10 additions & 0 deletions _/Command/Attributes/DefaultCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace verfriemelt\wrapped\_\Command\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
final readonly class DefaultCommand {}
2 changes: 2 additions & 0 deletions _/Command/HelpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
use ReflectionClass;
use verfriemelt\wrapped\_\Cli\Console;
use verfriemelt\wrapped\_\Command\Attributes\Command;
use verfriemelt\wrapped\_\Command\Attributes\DefaultCommand;
use verfriemelt\wrapped\_\Command\CommandArguments\Argument;
use verfriemelt\wrapped\_\Command\CommandArguments\ArgvParser;
use verfriemelt\wrapped\_\DI\Container;
use Override;
use RuntimeException;

#[DefaultCommand]
#[Command('help')]
final class HelpCommand extends AbstractCommand
{
Expand Down

0 comments on commit 6b87d21

Please sign in to comment.