Skip to content

Commit 6d5bf09

Browse files
authored
[feature] add ability to customize AutoName prefix (#23)
1 parent 82a6ada commit 6d5bf09

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,24 @@ class CreateUserCommand extends Command
151151
}
152152
```
153153

154+
By default, the command name's prefix is `app:`. You can override with your own
155+
prefix (or none at all):
156+
157+
```php
158+
use Symfony\Component\Console\Command\Command;
159+
use Zenstruck\Console\AutoName;
160+
161+
class CreateUserCommand extends Command
162+
{
163+
use AutoName; // command's name will be "create-user"
164+
165+
protected static function autoNamePrefix(): string
166+
{
167+
return '';
168+
}
169+
}
170+
```
171+
154172
### `ConfigureWithAttributes`
155173

156174
Use this trait to use the `Argument` and `Option` attributes to configure your command's

src/AutoName.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,16 @@ public static function getDefaultName(): string
2929
->snake()
3030
->replace('_', '-')
3131
->beforeLast('-command')
32-
->prepend('app:')
32+
->prepend(static::autoNamePrefix())
3333
->toString()
3434
;
3535
}
36+
37+
/**
38+
* Override to set your own prefix (or none).
39+
*/
40+
protected static function autoNamePrefix(): string
41+
{
42+
return 'app:';
43+
}
3644
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Zenstruck\Console\Tests\Fixture\Command;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Zenstruck\Console\AutoName;
7+
8+
/**
9+
* @author Kevin Bond <kevinbond@gmail.com>
10+
*/
11+
final class AutoNameNoPrefixCommand extends Command
12+
{
13+
use AutoName;
14+
15+
protected static function autoNamePrefix(): string
16+
{
17+
return '';
18+
}
19+
}

tests/Unit/AutoNameTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Component\Console\Command\Command;
77
use Zenstruck\Console\AutoName;
88
use Zenstruck\Console\Tests\Fixture\Command\AutoNameCommand;
9+
use Zenstruck\Console\Tests\Fixture\Command\AutoNameNoPrefixCommand;
910

1011
/**
1112
* @author Kevin Bond <kevinbond@gmail.com>
@@ -21,6 +22,15 @@ public function generates_name_automatically(): void
2122
$this->assertSame('app:auto-name', (new AutoNameCommand())->getName());
2223
}
2324

25+
/**
26+
* @test
27+
*/
28+
public function can_remove_prefix(): void
29+
{
30+
$this->assertSame('auto-name-no-prefix', AutoNameNoPrefixCommand::getDefaultName());
31+
$this->assertSame('auto-name-no-prefix', (new AutoNameNoPrefixCommand())->getName());
32+
}
33+
2434
/**
2535
* @test
2636
*/

0 commit comments

Comments
 (0)