Skip to content

Commit

Permalink
Split Command attribute to CommandName and CommandDescription
Browse files Browse the repository at this point in the history
  • Loading branch information
freost committed Oct 23, 2024
1 parent 715ba20 commit e8ee2c2
Show file tree
Hide file tree
Showing 18 changed files with 72 additions and 45 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ The major version bump is due to upping the required PHP version from `8.1` to `
* Added `FileSystem::clearCache()` method.
* Added `FileInfo::hasPermissions()` method that accepts an integer or a `Permissions` instance.
* Added `FileInfo::getPermissions()` method that returns a `Permissions` instance.
* Reactor command names and descriptions can now be registered with the `Command` attribute.
* Reactor command names can now be registered with the `CommandName` attribute.
* Reactor command descriptions can now be registered with the `CommandDescription` attribute.
* Reactor command arguments can now be registered with the `Arguments` attribute.

#### Changes
Expand All @@ -57,8 +58,8 @@ The major version bump is due to upping the required PHP version from `8.1` to `

#### Deprecations

* Deprecated the `Command::$command` property. Use the `Command` attribute instead.
* Deprecated the `Command::$description` property. Use the `Command` attribute instead.
* Deprecated the `Command::$command` property. Use the `CommandName` attribute instead.
* Deprecated the `Command::$description` property. Use the `CommandDescription` attribute instead.
* Deprecated the `Command::getCommand()` method.
* Deprecated the `Command::getDescription()` method.
* Deprecated the `Command::getArguments()` method. Use the `Arguments` attribute instead.
Expand Down
4 changes: 2 additions & 2 deletions src/mako/application/cli/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
use mako\file\Finder;
use mako\http\routing\Routes;
use mako\Mako;
use mako\reactor\attributes\Command;
use mako\reactor\attributes\CommandName;
use mako\reactor\CommandInterface;
use mako\reactor\Reactor;
use ReflectionClass;
Expand Down Expand Up @@ -175,7 +175,7 @@ protected function getApplicationCommands(): array
foreach ($finder->findImplementing(CommandInterface::class) as $commandClass) {
$reflection = new ReflectionClass($commandClass);

$attributes = $reflection->getAttributes(Command::class);
$attributes = $reflection->getAttributes(CommandName::class);

if (empty($attributes)) {
/** @var \mako\reactor\CommandInterface $command */
Expand Down
4 changes: 2 additions & 2 deletions src/mako/application/cli/commands/app/GenerateKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

namespace mako\application\cli\commands\app;

use mako\reactor\attributes\Command as CommandAttribute;
use mako\reactor\attributes\CommandDescription;
use mako\reactor\Command;
use mako\security\Key;

/**
* Command that generates an encryption key.
*/
#[CommandAttribute('app:generate-key', 'Generates a 256-bit encryption key.')]
#[CommandDescription('Generates a 256-bit encryption key.')]
class GenerateKey extends Command
{
/**
Expand Down
4 changes: 2 additions & 2 deletions src/mako/application/cli/commands/app/GeneratePreloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use mako\cli\output\Output;
use mako\file\FileSystem;
use mako\reactor\attributes\Arguments;
use mako\reactor\attributes\Command as CommandAttribute;
use mako\reactor\attributes\CommandDescription;
use mako\reactor\Command;

use function array_unique;
Expand All @@ -24,7 +24,7 @@
/**
* Command that generates an opcache preloader script.
*/
#[CommandAttribute('app:generate-preloader', 'Generates a opcache preloader script.')]
#[CommandDescription('Generates a opcache preloader script.')]
#[Arguments(
new Argument('-i|--ignore-core-classes', 'Should the default selection of core classes be ignored?', Argument::IS_BOOL),
new Argument('-o|--output-path', 'Path to where the preloder script should be written', Argument::IS_OPTIONAL),
Expand Down
4 changes: 2 additions & 2 deletions src/mako/application/cli/commands/app/GenerateSecret.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use mako\application\Application;
use mako\file\FileSystem;
use mako\reactor\attributes\Command as CommandAttribute;
use mako\reactor\attributes\CommandDescription;
use mako\reactor\Command;
use mako\security\Key;

Expand All @@ -18,7 +18,7 @@
/**
* Command that generates a new application secret.
*/
#[CommandAttribute('app:generate-secret', 'Generates a new application secret.')]
#[CommandDescription('Generates a new application secret.')]
class GenerateSecret extends Command
{
/**
Expand Down
4 changes: 2 additions & 2 deletions src/mako/application/cli/commands/app/ListRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use mako\http\routing\Router;
use mako\http\routing\Routes;
use mako\reactor\attributes\Arguments;
use mako\reactor\attributes\Command as CommandAttribute;
use mako\reactor\attributes\CommandDescription;
use mako\reactor\Command;
use mako\utility\Arr;

Expand All @@ -30,7 +30,7 @@
/**
* Command that lists all registered routes.
*/
#[CommandAttribute('app:routes', 'Lists all registered routes.')]
#[CommandDescription('Lists all registered routes.')]
#[Arguments(
new Argument('-f|--filter', 'Filter routes using the route action, name or path', Argument::IS_OPTIONAL),
new Argument('-d|--detailed', 'Show more information about the route', Argument::IS_BOOL | Argument::IS_OPTIONAL),
Expand Down
4 changes: 2 additions & 2 deletions src/mako/application/cli/commands/cache/Clear.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
use mako\cache\CacheManager;
use mako\cli\input\arguments\Argument;
use mako\reactor\attributes\Arguments;
use mako\reactor\attributes\Command as CommandAttribute;
use mako\reactor\attributes\CommandDescription;

/**
* Command that clears the cache.
*/
#[CommandAttribute('cache:clear', 'Clears the cache.')]
#[CommandDescription('Clears the cache.')]
#[Arguments(
new Argument('-c|--configuration', 'Configuration name', Argument::IS_OPTIONAL),
)]
Expand Down
4 changes: 2 additions & 2 deletions src/mako/application/cli/commands/cache/Remove.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
use mako\cache\CacheManager;
use mako\cli\input\arguments\Argument;
use mako\reactor\attributes\Arguments;
use mako\reactor\attributes\Command as CommandAttribute;
use mako\reactor\attributes\CommandDescription;

/**
* Command that removes the chosen key from the cache.
*/
#[CommandAttribute('cache:remove', 'Removes the chosen key from the cache.')]
#[CommandDescription('Removes the chosen key from the cache.')]
#[Arguments(
new Argument('-c|--configuration', 'Configuration name', Argument::IS_OPTIONAL),
new Argument('-k|--key', 'Cache key'),
Expand Down
4 changes: 2 additions & 2 deletions src/mako/application/cli/commands/migrations/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use mako\cli\input\arguments\Argument;
use mako\file\FileSystem;
use mako\reactor\attributes\Arguments;
use mako\reactor\attributes\Command as CommandAttribute;
use mako\reactor\attributes\CommandDescription;
use mako\reactor\Command;
use Throwable;

Expand All @@ -22,7 +22,7 @@
/**
* Command that creates a migration.
*/
#[CommandAttribute('migration:create', 'Creates a new migration.')]
#[CommandDescription('Creates a new migration.')]
#[Arguments(
new Argument('-d|--description', 'Migration description', Argument::IS_OPTIONAL),
new Argument('-p|--package', 'Package name', Argument::IS_OPTIONAL),
Expand Down
4 changes: 2 additions & 2 deletions src/mako/application/cli/commands/migrations/Down.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
use mako\application\cli\commands\migrations\traits\RollbackTrait;
use mako\cli\input\arguments\Argument;
use mako\reactor\attributes\Arguments;
use mako\reactor\attributes\Command as CommandAttribute;
use mako\reactor\attributes\CommandDescription;

/**
* Command that rolls back the last batch of migrations.
*/
#[CommandAttribute('migration:down', 'Rolls back the last batch of migrations.')]
#[CommandDescription('Rolls back the last batch of migrations.')]
#[Arguments(
new Argument('-b|--batches', 'Number of batches to roll back', Argument::IS_OPTIONAL | Argument::IS_INT),
new Argument('-d|--database', 'Sets which database connection to use', Argument::IS_OPTIONAL),
Expand Down
4 changes: 2 additions & 2 deletions src/mako/application/cli/commands/migrations/Reset.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
use mako\application\cli\commands\migrations\traits\RollbackTrait;
use mako\cli\input\arguments\Argument;
use mako\reactor\attributes\Arguments;
use mako\reactor\attributes\Command as CommandAttribute;
use mako\reactor\attributes\CommandDescription;

/**
* Command that rolls back the last batch of migrations.
*/
#[CommandAttribute('migration:reset', 'Resets the database schema.')]
#[CommandDescription('Resets the database schema.')]
#[Arguments(
new Argument('-d|--database', 'Sets which database connection to use', Argument::IS_OPTIONAL),
new Argument('-f|--force', 'Force the schema reset?', Argument::IS_BOOL),
Expand Down
4 changes: 2 additions & 2 deletions src/mako/application/cli/commands/migrations/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

use mako\cli\input\arguments\Argument;
use mako\reactor\attributes\Arguments;
use mako\reactor\attributes\Command as CommandAttribute;
use mako\reactor\attributes\CommandDescription;

use function count;
use function vsprintf;

/**
* Command that checks if there are any outstanding migrations.
*/
#[CommandAttribute('migration:status', 'Checks if there are any outstanding migrations.')]
#[CommandDescription('Checks if there are any outstanding migrations.')]
#[Arguments(
new Argument('-d|--database', 'Sets which database connection to use', Argument::IS_OPTIONAL),
new Argument('-e|--exit-code', 'Exits with 1 if there are outstanding migrations and 0 if there are none', Argument::IS_BOOL),
Expand Down
4 changes: 2 additions & 2 deletions src/mako/application/cli/commands/migrations/Up.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

use mako\cli\input\arguments\Argument;
use mako\reactor\attributes\Arguments;
use mako\reactor\attributes\Command as CommandAttribute;
use mako\reactor\attributes\CommandDescription;

/**
* Command that runs all outstanding migrations.
*/
#[CommandAttribute('migration:up', 'Runs all outstanding migrations.')]
#[CommandDescription('Runs all outstanding migrations.')]
#[Arguments(
new Argument('-d|--database', 'Sets which database connection to use', Argument::IS_OPTIONAL),
)]
Expand Down
4 changes: 2 additions & 2 deletions src/mako/application/cli/commands/server/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use mako\application\Application;
use mako\cli\input\arguments\Argument;
use mako\reactor\attributes\Arguments;
use mako\reactor\attributes\Command as CommandAttribute;
use mako\reactor\attributes\CommandDescription;
use mako\reactor\Command;

use function dirname;
Expand All @@ -26,7 +26,7 @@
/**
* Server command.
*/
#[CommandAttribute('app:server', 'Starts the local development server.')]
#[CommandDescription('Starts the local development server.')]
#[Arguments(
new Argument('-a|--address', 'Address to run the server on', Argument::IS_OPTIONAL),
new Argument('-d|--docroot', 'Path to the document root', Argument::IS_OPTIONAL),
Expand Down
4 changes: 2 additions & 2 deletions src/mako/reactor/Reactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use mako\cli\output\Output;
use mako\common\traits\SuggestionTrait;
use mako\reactor\attributes\Arguments;
use mako\reactor\attributes\Command as CommandAttribute;
use mako\reactor\attributes\CommandDescription;
use mako\syringe\Container;
use ReflectionClass;

Expand Down Expand Up @@ -185,7 +185,7 @@ protected function displayReactorInfo(): void
*/
protected function getCommandDescription(ReflectionClass $class): string
{
$attributes = $class->getAttributes(CommandAttribute::class);
$attributes = $class->getAttributes(CommandDescription::class);

if (empty($attributes)) {
/** @var \mako\reactor\CommandInterface $command */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,19 @@
use Attribute;

/**
* Command attribute.
* Command description attribute.
*/
#[Attribute(Attribute::TARGET_CLASS)]
class Command
class CommandDescription
{
/**
* Constructor.
*/
public function __construct(
protected string $name,
protected string $description = '',
protected string $description
) {
}

/**
* Returns the command name.
*/
public function getName(): string
{
return $this->name;
}

/**
* Returns the command description.
*/
Expand Down
33 changes: 33 additions & 0 deletions src/mako/reactor/attributes/CommandName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
* @copyright Frederic G. Østby
* @license http://www.makoframework.com/license
*/

namespace mako\reactor\attributes;

use Attribute;

/**
* Command name attribute.
*/
#[Attribute(Attribute::TARGET_CLASS)]
class CommandName
{
/**
* Constructor.
*/
public function __construct(
protected string $name
) {
}

/**
* Returns the command name.
*/
public function getName(): string
{
return $this->name;
}
}
6 changes: 4 additions & 2 deletions tests/unit/reactor/ReactorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
use mako\cli\input\Input;
use mako\cli\output\Output;
use mako\reactor\attributes\Arguments;
use mako\reactor\attributes\Command as CommandAttribute;
use mako\reactor\attributes\CommandDescription;
use mako\reactor\attributes\CommandName;
use mako\reactor\Command;
use mako\reactor\Dispatcher;
use mako\reactor\Reactor;
Expand All @@ -26,7 +27,8 @@
// START CLASSES
// --------------------------------------------------------------------------

#[CommandAttribute('foo', 'Command description.')]
#[CommandName('foo')]
#[CommandDescription('Command description.')]
#[Arguments(
new Argument('arg2', 'Argument description.', Argument::IS_OPTIONAL),
new Argument('--option', 'Option description.'),
Expand Down

0 comments on commit e8ee2c2

Please sign in to comment.