Skip to content

Commit 4cc7bb7

Browse files
committed
updated command
Signed-off-by: bidi <bidi@apidemia.com>
1 parent aa1647f commit 4cc7bb7

10 files changed

Lines changed: 35 additions & 21 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Dates before 4.0.0 are the commit dates of the tagged commits.
99
### Changed
1010

1111
- Minimum PHP version is now 8.3. Supported versions: 8.3, 8.4, 8.5 and 8.6.
12+
- `laminas/laminas-cli` is required as `^1.16.0`.
1213
- A command's lock now uses its resolved name, so aliases and abbreviations share one lock. Unknown commands are not locked.
1314
- Lock errors are written to the error output with quiet verbosity, so they are shown with `-q` (use `--silent` to hide them).
1415
- A missing or unwritable lock directory, or a lock file that cannot be opened, throws `Dot\Cli\Exception\RuntimeException` instead of a `TypeError`.
@@ -18,16 +19,17 @@ Dates before 4.0.0 are the commit dates of the tagged commits.
1819
- The factories throw `Dot\Cli\Exception\InvalidConfigException` (an `Exception` and a PSR-11 `ContainerExceptionInterface`) instead of `Exception`.
1920
- `ApplicationFactory::__invoke()` returns `Dot\Cli\Application`.
2021
- The `MESSAGE_*` constants are typed (`public const string`).
21-
- `config/autoload/cli.global.php` uses the command name as the key instead of `DemoCommand::getDefaultName()`, which Symfony Console 8 removed.
22-
- `DemoCommand::$defaultName` is typed as `string`. `DemoCommand` keeps it and `configure()` as a demo of extending a command; Symfony Console reads the name and description from the `#[AsCommand]` attribute.
23-
- Development tooling: PHPUnit 12 with strict `failOn*` flags, PHPStan level 8, GitHub Actions run PHP 8.3 to 8.6 with `actions/checkout@v7`, `actions/cache@v6` and `codecov/codecov-action@v7`, and the Laminas backward-compatibility check is disabled for this major version.
22+
- `config/autoload/cli.global.php` uses `DemoCommand::DEFAULT_NAME` as the key instead of `DemoCommand::getDefaultName()`, which Symfony Console 8 removed.
23+
- The `#[AsCommand]` attribute of `DemoCommand` reads its name from `DemoCommand::DEFAULT_NAME`. `DemoCommand` keeps `configure()` as a demo of extending a command.
24+
- Development tooling: PHPUnit 12 with strict `failOn*` flags, PHPStan level 8, GitHub Actions run PHP 8.3 to 8.6 with `actions/checkout@v7`, `actions/cache@v6` and `codecov/codecov-action@v7`.
2425
- Documentation for 4.0 is available in `docs/book/v4`, including an upgrade guide from 3.x.
2526

2627
### Added
2728

2829
- `Dot\Cli\Exception\ExceptionInterface`, `Dot\Cli\Exception\RuntimeException` and `Dot\Cli\Exception\InvalidConfigException`.
2930
- `ApplicationFactory::EVENT_DISPATCHER` and `ApplicationFactory::LEGACY_EVENT_DISPATCHER`.
3031
- `psr/container`, `symfony/console` and `symfony/event-dispatcher` as direct requirements.
32+
- A conflict with `symfony/event-dispatcher-contracts` versions before 2.5.3 and from 3.0 to 3.4. In those versions, `EventDispatcherInterface::dispatch()` has an implicitly nullable parameter, which PHP 8.4 and newer report as deprecated.
3133
- Tests with a minimal PSR-11 container and a real temporary directory, covering lock contention, aliases, abbreviations, quiet output and the event dispatcher keys.
3234

3335
### Deprecated
@@ -38,6 +40,7 @@ Dates before 4.0.0 are the commit dates of the tagged commits.
3840

3941
- PHP 8.2 support.
4042
- `laminas/laminas-servicemanager` as a requirement. dot-cli only uses PSR-11; applications that use Laminas Service Manager require it themselves.
43+
- `DemoCommand::$defaultName`. Use `DemoCommand::DEFAULT_NAME`.
4144
- `mikey179/vfsstream` as a development requirement. Lock tests use a real temporary directory.
4245

4346
### Fixed

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Upgrading from 3.x? Read the [upgrade guide](https://docs.dotkernel.org/dot-cli/
3434
## Requirements
3535

3636
- **PHP**: 8.3, 8.4, 8.5 or 8.6
37-
- **laminas/laminas-cli**: >= 1.15
37+
- **laminas/laminas-cli**: >= 1.16
3838
- **symfony/console** and **symfony/event-dispatcher**: 7.4 or 8.x
3939
- A PSR-11 container that reads `factories` and `aliases` configuration, for example `laminas/laminas-servicemanager` (require it in your application)
4040

@@ -68,7 +68,7 @@ This is the config file you will add your commands to, using the command name as
6868

6969
```php
7070
'commands' => [
71-
'demo:command' => Dot\Cli\Command\DemoCommand::class,
71+
Dot\Cli\Command\DemoCommand::DEFAULT_NAME => Dot\Cli\Command\DemoCommand::class,
7272
],
7373
```
7474

‎composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "dotkernel/dot-cli",
33
"type": "library",
4-
"description": "Dotkernel component for creating console applications based on laminas-cli",
4+
"description": "Dotkernel component for creating console applications based on laminas-cli, for any PSR-11 container",
55
"license": "MIT",
66
"homepage": "https://github.com/dotkernel/dot-cli",
77
"keywords": [

‎config/autoload/cli.global.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'version' => '1.0.0',
1111
'name' => 'Dotkernel CLI',
1212
'commands' => [
13-
DemoCommand::$defaultName => DemoCommand::class,
13+
DemoCommand::DEFAULT_NAME => DemoCommand::class,
1414
],
1515
],
1616
FileLockerInterface::class => [

‎docs/book/v4/configuration.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ Add your commands under the `commands` key, using the command name as the key an
2020

2121
```php
2222
'commands' => [
23-
'demo:command' => DemoCommand::class,
24-
'example:command' => ExampleCommand::class,
23+
DemoCommand::DEFAULT_NAME => DemoCommand::class,
24+
ExampleCommand::DEFAULT_NAME => ExampleCommand::class,
2525
],
2626
```
2727

28-
Use the name from the command's `#[AsCommand]` attribute.
28+
The key must be the name from the command's `#[AsCommand]` attribute.
29+
Keep the name in a `DEFAULT_NAME` class constant and use it in both places, see [Usage](usage.md).
30+
A string such as `'demo:command'` also works.
2931
Do not use `DemoCommand::getDefaultName()`: Symfony Console 8 removed that method, and Symfony Console 7.4 reports it as deprecated.
3032

3133
Command classes that are registered in the container are fetched from it.

‎docs/book/v4/installation.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Requirements
44

55
- PHP 8.3, 8.4, 8.5 or 8.6
6-
- `laminas/laminas-cli` 1.15 or newer
6+
- `laminas/laminas-cli` 1.16 or newer
77
- `symfony/console` and `symfony/event-dispatcher` 7.4 or 8.x
88
- A PSR-11 container that reads `factories` and `aliases` configuration, for example `laminas/laminas-servicemanager`
99

‎docs/book/v4/upgrading.md‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Most applications only need to update the `commands` keys in `config/autoload/cl
99
| --- | --- | --- |
1010
| PHP 8.2 is no longer supported | Composer refuses to install 4.0 | Use PHP 8.3 or newer, or stay on 3.x |
1111
| `laminas/laminas-servicemanager` is no longer required | `Class "Laminas\ServiceManager\ServiceManager" not found` if your application relied on dot-cli to install it | Run `composer require laminas/laminas-servicemanager` in your application |
12+
| `DemoCommand::$defaultName` was replaced by the `DemoCommand::DEFAULT_NAME` constant | A subclass that reads `static::$defaultName` fails with `Access to undeclared static property` | Read `DemoCommand::DEFAULT_NAME` |
1213
| The shipped config no longer uses `DemoCommand::getDefaultName()` as a key | A config key such as `DemoCommand::getDefaultName()` fails with `Call to undefined method` on Symfony Console 8 | Use the command name as the key, see [below](#command-keys) |
1314
| The event dispatcher service key is now `Laminas\Cli\SymfonyEventDispatcher` | None yet: the old key still works, but it is deprecated | Rename the service key, see [below](#event-dispatcher) |
1415
| A service under the event dispatcher key must implement `Symfony\Component\EventDispatcher\EventDispatcherInterface` | `Dot\Cli\Exception\InvalidConfigException` when the application is created | Register a Symfony event dispatcher under that key |
@@ -34,13 +35,17 @@ After:
3435

3536
```php
3637
'commands' => [
37-
'demo:command' => DemoCommand::class,
38+
DemoCommand::DEFAULT_NAME => DemoCommand::class,
3839
],
3940
```
4041

41-
If one of your own commands still sets `protected static $defaultName`, move the name to the `#[AsCommand]` attribute.
42+
The `DemoCommand::DEFAULT_NAME` constant replaces the `DemoCommand::$defaultName` property.
43+
A subclass of `DemoCommand` that reads `static::$defaultName` fails with `Access to undeclared static property`.
44+
Read `DemoCommand::DEFAULT_NAME` instead.
45+
46+
If one of your own commands still sets `protected static $defaultName`, replace it with a `DEFAULT_NAME` constant and use the constant in the `#[AsCommand]` attribute.
4247
Symfony Console 7.4 and 8 do not read the property, so it no longer names the command.
43-
`DemoCommand` keeps it, typed as `string`, together with `configure()`, only to demonstrate both, see [Usage](usage.md).
48+
See [Usage](usage.md) for an example.
4449

4550
## Event dispatcher
4651

‎docs/book/v4/usage.md‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# Usage
22

33
Use `src/Command/DemoCommand.php` as an example when creating a new command.
4-
Set the name and description in the `#[AsCommand]` attribute: Symfony Console reads them from there, so a command needs neither a `$defaultName` property nor a `configure()` method to set them.
4+
Set the name and description in the `#[AsCommand]` attribute: Symfony Console reads them from there, so a command does not need a `configure()` method to set them.
5+
Keep the name in a `DEFAULT_NAME` class constant, and use the constant in the attribute and in `config/autoload/cli.global.php`.
6+
Then the command name is written in one place only.
57

68
`DemoCommand` also implements `configure()` to show where a command can be extended by chaining methods, for example `setHelp()`, `addArgument()` and `addOption()`.
79
For the demo, its `configure()` sets the same name and description as the attribute.
810
Values set in `configure()` override the attribute, so if you keep both in your own command, keep them identical.
9-
Symfony Console 7.4 and 8 do not read the `$defaultName` property: it is only there for the demo.
1011

1112
```php
1213
<?php
@@ -21,11 +22,13 @@ use Symfony\Component\Console\Input\InputInterface;
2122
use Symfony\Component\Console\Output\OutputInterface;
2223

2324
#[AsCommand(
24-
name: 'app:example',
25+
name: ExampleCommand::DEFAULT_NAME,
2526
description: 'Example command description.',
2627
)]
2728
class ExampleCommand extends Command
2829
{
30+
public const string DEFAULT_NAME = 'app:example';
31+
2932
protected function execute(InputInterface $input, OutputInterface $output): int
3033
{
3134
$output->writeln('Hello from app:example');
@@ -39,7 +42,7 @@ Then register it in `config/autoload/cli.global.php`:
3942

4043
```php
4144
'commands' => [
42-
'app:example' => App\Command\ExampleCommand::class,
45+
App\Command\ExampleCommand::DEFAULT_NAME => App\Command\ExampleCommand::class,
4346
],
4447
```
4548

‎src/Command/DemoCommand.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010
use Symfony\Component\Console\Output\OutputInterface;
1111

1212
#[AsCommand(
13-
name: 'demo:command',
13+
name: DemoCommand::DEFAULT_NAME,
1414
description: 'Demo command description.',
1515
)]
1616
class DemoCommand extends Command
1717
{
18-
public static string $defaultName = 'demo:command';
18+
public const string DEFAULT_NAME = 'demo:command';
1919

2020
protected function configure(): void
2121
{
2222
$this
23-
->setName(self::$defaultName)
23+
->setName(self::DEFAULT_NAME)
2424
->setDescription('Demo command description.');
2525
}
2626

‎test/Command/DemoCommandTest.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function testHasNameAndDescription(): void
1616
$command = new DemoCommand();
1717

1818
$this->assertSame('demo:command', $command->getName());
19+
$this->assertSame(DemoCommand::DEFAULT_NAME, $command->getName());
1920
$this->assertSame('Demo command description.', $command->getDescription());
2021
}
2122

0 commit comments

Comments
 (0)