|
1 | 1 | # Creating a Command |
2 | 2 |
|
3 | | -Commands are created to live within add-ons, and are registered as part of the add-on process. |
| 3 | +Commands are created inside add-ons and registered through the add-on setup file. |
4 | 4 |
|
5 | | -## addon.setup.php |
| 5 | +[TOC] |
6 | 6 |
|
7 | | -In order to add commands to your addon, you should add the `commands` parameter as an associative array to your `addon.setup.php` file with the handle as the key, and the class of your command as the value. |
| 7 | +## Prerequisite: Existing Add-on |
| 8 | + |
| 9 | +Before creating a command, make sure you already have an add-on directory in `system/user/addons/[addon_short_name]`. If needed, create one first with [`make:addon`](cli/built-in-commands/make-addon.md). |
| 10 | + |
| 11 | +## Generate a Command with `make:command` |
| 12 | + |
| 13 | +The recommended workflow is to use the command generator: |
8 | 14 |
|
9 | 15 | ``` |
10 | | -return array( |
11 | | - 'author' => 'Awesome Developer', |
12 | | - 'author_url' => 'https://example.com/', |
13 | | - 'name' => 'My Amazing Module', |
14 | | - 'description' => 'Does amazing things', |
15 | | - 'version' => '1.0', |
16 | | - 'namespace' => 'Awesome\AmazingModule', |
17 | | - 'settings_exist' => true, |
18 | | - 'commands' => [ |
19 | | - 'amazing:run' => Awesome\AmazingModule\Commands\DoThings::class, |
20 | | - 'amazing:more-things' => 'Awesome\AmazingModule\Commands\DoMoreThings', |
21 | | - ] |
22 | | -); |
| 16 | +php eecli.php make:command "Sync Orders" --addon=my_example_addon --description="Sync orders from an API" --signature="sync-orders" |
23 | 17 | ``` |
24 | 18 |
|
25 | | -It is best practice to namespace your commands, so as not to conflict with other addons. |
| 19 | +You can also run it interactively: |
26 | 20 |
|
27 | | -## Anatomy of a Command |
| 21 | +``` |
| 22 | +php eecli.php make:command |
| 23 | +Let's build your command! |
| 24 | +Command name? Sync Orders |
| 25 | +What add-on do you want to add this to? my_example_addon |
| 26 | +Command description? Sync orders from an API |
| 27 | +Command signature? (i.e. make:magic) sync-orders |
| 28 | +Let's build! |
| 29 | +Your command has been created successfully! |
| 30 | +``` |
28 | 31 |
|
29 | | -Creating commands is simple. Each commands is built in a similar way as part of a custom add-on: |
| 32 | +NOTE: The generator prefixes the command signature with the add-on short name. |
| 33 | +For example, `sync-orders` becomes `my_example_addon:sync-orders`. |
30 | 34 |
|
31 | | -### Class Structure |
| 35 | +For all options, see [`make:command`](cli/built-in-commands/make-command.md). |
32 | 36 |
|
33 | | -Your class can have any name, and should be namespaced to your addon. When creating your class, make sure to include: |
34 | | -`use ExpressionEngine\Cli\Cli;` |
| 37 | +## What Gets Generated |
35 | 38 |
|
36 | | -You class should also extend the `Cli` class. |
| 39 | +`make:command` creates a command class file in your add-on: |
37 | 40 |
|
38 | 41 | ``` |
39 | | -use EllisLab\ExpressionEngine\Cli\Cli; |
| 42 | +system/user/addons/my_example_addon/Commands/CommandSyncOrders.php |
| 43 | +``` |
| 44 | + |
| 45 | +It also updates `addon.setup.php` by adding your command to the `commands` array: |
40 | 46 |
|
41 | | -class CommandHelloWorld extends Cli { |
42 | | -} |
| 47 | +``` |
| 48 | +return [ |
| 49 | + // ... |
| 50 | + 'namespace' => 'Vendor\MyExampleAddon', |
| 51 | + 'commands' => [ |
| 52 | + 'my_example_addon:sync-orders' => Vendor\MyExampleAddon\Commands\CommandSyncOrders::class, |
| 53 | + ], |
| 54 | +]; |
43 | 55 | ``` |
44 | 56 |
|
45 | | -### Required Variables |
| 57 | +## Anatomy of the Generated Class |
46 | 58 |
|
47 | | -Each command is required to have a number of public variables that are required for finding and executing, as well as displaying pertinent information. |
| 59 | +The generated class extends `ExpressionEngine\Cli\Cli` and includes all required properties: |
48 | 60 |
|
49 | | -`$name`: The name of your Command. |
50 | | -`$description`: The basic gist of what your command does. This should be limited to one line |
51 | | -`$summary`: This is a more detailed explanation of what your command does or how to use it. This is displayed in the `--help` calls. |
52 | | -`$usage`: A oneline explanation of how to use your command. |
53 | | -`$commandOptions = []`: An array of available arguments and options, along with their description, as a key:value pair. ie. `'verbose,v' => 'Show all output'` |
| 61 | +```php |
| 62 | +<?php |
54 | 63 |
|
55 | | -In addition, the `handle` function is required, and does all of the work when the command is run. |
| 64 | +namespace Vendor\MyExampleAddon\Commands; |
56 | 65 |
|
57 | | -``` |
58 | | -use EllisLab\ExpressionEngine\Cli\Cli; |
59 | | -
|
60 | | -class CommandHelloWorld extends Cli { |
61 | | -
|
62 | | - /** |
63 | | - * name of command |
64 | | - * @var string |
65 | | - */ |
66 | | - public $name = 'Hello World'; |
67 | | -
|
68 | | - /** |
69 | | - * Public description of command |
70 | | - * @var string |
71 | | - */ |
72 | | - public $description = 'The most basic of commands'; |
73 | | -
|
74 | | - /** |
75 | | - * Summary of command functionality |
76 | | - * @var [type] |
77 | | - */ |
78 | | - public $summary = 'This is a sample command used to test the CLI'; |
79 | | -
|
80 | | - /** |
81 | | - * How to use command |
82 | | - * @var string |
83 | | - */ |
84 | | - public $usage = 'php eecli.php hello'; |
85 | | -
|
86 | | - /** |
87 | | - * options available for use in command |
88 | | - * @var array |
89 | | - */ |
90 | | - public $commandOptions = [ |
91 | | - 'verbose,v' => 'Hello world, but longer', |
92 | | - ]; |
93 | | -
|
94 | | - /** |
95 | | - * Run the command |
96 | | - * @return mixed |
97 | | - */ |
98 | | - public function handle() |
99 | | - { |
| 66 | +use ExpressionEngine\Cli\Cli; |
100 | 67 |
|
101 | | - // This is where the magic happens |
| 68 | +class CommandSyncOrders extends Cli |
| 69 | +{ |
| 70 | + public $name = 'Sync Orders'; |
| 71 | + public $signature = 'my_example_addon:sync-orders'; |
| 72 | + public $description = 'Sync orders from an API'; |
| 73 | + public $summary = 'Sync orders from an API'; |
| 74 | + public $usage = 'php eecli.php my_example_addon:sync-orders'; |
| 75 | + public $commandOptions = []; |
102 | 76 |
|
| 77 | + public function handle() |
| 78 | + { |
| 79 | + $this->info('Hello World!'); |
103 | 80 | } |
104 | | - |
105 | 81 | } |
106 | | -``` |
| 82 | +``` |
| 83 | + |
| 84 | +You can now replace the `handle()` contents with your command logic and add options to `$commandOptions` as needed. |
| 85 | + |
| 86 | +## Advanced: Manual Registration |
| 87 | + |
| 88 | +Manual registration is optional, but if you add command classes yourself, register them in `addon.setup.php` under the `commands` key using `::class` values: |
| 89 | + |
| 90 | +```php |
| 91 | +'commands' => [ |
| 92 | + 'my_example_addon:sync-orders' => Vendor\MyExampleAddon\Commands\CommandSyncOrders::class, |
| 93 | +], |
| 94 | +``` |
0 commit comments