Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Console/src/Attribute/AsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ public function __construct(
public readonly string $name,
public readonly ?string $description = null,
public readonly ?string $help = null,
public readonly array $aliases = [],
Comment thread
roxblnfk marked this conversation as resolved.
) {}
}
1 change: 1 addition & 0 deletions src/Console/src/Configurator/Attribute/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function parse(\ReflectionClass $reflection): CommandDefinition
options: $this->parseOptions($reflection),
description: $attribute->description,
help: $attribute instanceof AsCommand ? $attribute->help : null,
aliases: $attribute instanceof AsCommand ? $attribute->aliases : [],
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function configure(Command $command, \ReflectionClass $reflection): void
$command->setName($result->name);
$command->setDescription($result->description ?? (string) $reflection->getConstant('DESCRIPTION'));
$command->setHelp((string) $result->help);
$command->setAliases($result->aliases);

foreach ($result->options as $option) {
$command->getDefinition()->addOption($option);
Expand Down
2 changes: 2 additions & 0 deletions src/Console/src/Configurator/CommandDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ public function __construct(
public readonly ?string $description = null,
/** @var ?non-empty-string */
public readonly ?string $help = null,
/** @var string[] */
Comment thread
gam6itko marked this conversation as resolved.
Outdated
public readonly array $aliases = [],
) {}
}
12 changes: 12 additions & 0 deletions src/Console/tests/AttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Spiral\Attributes\AttributeReader;
use Spiral\Attributes\ReaderInterface;
use Spiral\Tests\Console\Fixtures\Attribute\WithAliasesCommand;
use Spiral\Tests\Console\Fixtures\Attribute\WithDescriptionCommand;
use Spiral\Tests\Console\Fixtures\Attribute\WithHelpCommand;
use Spiral\Tests\Console\Fixtures\Attribute\WithNameCommand;
Expand Down Expand Up @@ -40,6 +41,17 @@ public function testCommandWithHelp(): void
self::assertSame('Some help message', $core->run(command: 'attribute-with-help')->getOutput()->fetch());
}

public function testCommandWithAliases(): void
{
$core = $this->getCore($this->getStaticLocator([
WithAliasesCommand::class,
]));

self::assertSame('awa,alias-for-with-aliases', $core->run(command: 'attribute-with-aliases')->getOutput()->fetch());
self::assertSame('awa,alias-for-with-aliases', $core->run(command: 'awa')->getOutput()->fetch());
self::assertSame('awa,alias-for-with-aliases', $core->run(command: 'alias-for-with-aliases')->getOutput()->fetch());
}

public function testCommandWithSymfonyAttribute(): void
{
$core = $this->getCore($this->getStaticLocator([
Expand Down
19 changes: 19 additions & 0 deletions src/Console/tests/Fixtures/Attribute/WithAliasesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Spiral\Tests\Console\Fixtures\Attribute;

use Spiral\Console\Attribute\AsCommand;
use Spiral\Console\Command;

#[AsCommand(name: 'attribute-with-aliases', aliases: ['awa', 'alias-for-with-aliases'])]
final class WithAliasesCommand extends Command
{
public function perform(): int
{
$this->write(\implode(',', $this->getAliases()));

return self::SUCCESS;
}
}
2 changes: 1 addition & 1 deletion src/Exceptions/src/Renderer/ConsoleRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ private function isColorsSupported(mixed $stream = STDOUT): bool

try {
if (\DIRECTORY_SEPARATOR === '\\') {
return (\function_exists('sapi_windows_vt100_support') && @\sapi_windows_vt100_support($stream))
return (\function_exists('sapi_windows_vt100_support') && @sapi_windows_vt100_support($stream))
|| \getenv('ANSICON') !== false
|| \getenv('ConEmuANSI') === 'ON'
|| \getenv('TERM') === 'xterm';
Expand Down
4 changes: 4 additions & 0 deletions src/Scaffolder/src/Command/CommandCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class CommandCommand extends AbstractCommand
#[Option(description: 'Optional, specify a custom namespace')]
private ?string $namespace = null;

#[Option(name: 'aliases', description: 'Command aliases')]
private array $aliases = [];

#[Option(name: 'argument', shortcut: 'a', description: 'Command arguments')]
private array $arguments = [];

Expand All @@ -40,6 +43,7 @@ public function perform(): int
$declaration = $this->createDeclaration(CommandDeclaration::class, [
'description' => $this->description,
'alias' => $this->alias ?? \strtolower((string) \preg_replace('/(?<!^)[A-Z]/', ':$0', $this->name)),
'aliases' => $this->aliases,
]);

foreach ($this->arguments as $argument) {
Expand Down
5 changes: 5 additions & 0 deletions src/Scaffolder/src/Declaration/CommandDeclaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function __construct(
?string $namespace = null,
private readonly ?string $alias = null,
private readonly ?string $description = null,
private readonly array $aliases = [],
) {
parent::__construct($config, $name, $comment, $namespace);
}
Expand Down Expand Up @@ -73,6 +74,10 @@ public function declare(): void
$commandDefinition['description'] = $this->description;
}

if ($this->aliases !== []) {
$commandDefinition['aliases'] = $this->aliases;
}

$this->class->addAttribute(AsCommand::class, $commandDefinition);

$this->class
Expand Down
20 changes: 20 additions & 0 deletions src/Scaffolder/tests/Command/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,26 @@ public function testScaffoldWithCustomNamespace(): void
self::assertStringContainsString('App\Custom\Command', $content);
}

public function testScaffoldWithAliases(): void
{
$this->className = $className = '\\Spiral\\Tests\\Scaffolder\\App\\Command\\AliasedCommand';

$this->console()->run('create:command', [
'name' => 'Aliased',
'--aliases' => ['al', 'aliased-cmd'],
]);

\clearstatcache();
self::assertTrue(\class_exists($className));

$reflection = new \ReflectionClass($className);

/** @var \Spiral\Console\Attribute\AsCommand $definition */
$definition = $reflection->getAttributes()[0]->newInstance();

self::assertSame(['al', 'aliased-cmd'], $definition->aliases);
}

public function testShowInstructionAfterInstallation(): void
{
$this->className = $className = '\\Spiral\\Tests\\Scaffolder\\App\\Command\\ArgumentCommand';
Expand Down
Loading