|
1 | 1 | # Writing Commands |
| 2 | + |
| 3 | +Commands offer a way to encapsulate a piece of functionality that can be executed by yourself or other developers. They are a great way to automate repetitive tasks, such as setting up a new project, running tests, or deploying your app. |
| 4 | + |
| 5 | +Leaf MVC comes with a powerful command line interface and a bunch of helpful commands to get you started, however, you can also create your own commands to automate your own tasks. |
| 6 | + |
| 7 | +## Creating a command |
| 8 | + |
| 9 | +You can create a new command using the `g:command` command. This command will create a new command class in your `app/console` directory. |
| 10 | + |
| 11 | +```bash:no-line-numbers |
| 12 | +php leaf g:command cache:purge |
| 13 | +``` |
| 14 | + |
| 15 | +This will create a `CachePurgeCommand.php` file in your `app/console` directory. The file will contain a class that extends the `Command` class and implements the `handle()` method. |
| 16 | + |
| 17 | +```php |
| 18 | +<?php |
| 19 | + |
| 20 | +namespace App\Console; |
| 21 | + |
| 22 | +use Aloe\Command; |
| 23 | + |
| 24 | +class CachePurgeCommand extends Command |
| 25 | +{ |
| 26 | + protected static $defaultName = 'cache:purge'; |
| 27 | + public $description = 'cache:purge command\'s description'; |
| 28 | + public $help = 'cache:purge command\'s help'; |
| 29 | + |
| 30 | + protected function config() |
| 31 | + { |
| 32 | + $this |
| 33 | + ->setArgument('argument', 'optional', 'argument description') |
| 34 | + ->setOption('option', 'o', 'required', 'option description'); |
| 35 | + } |
| 36 | + |
| 37 | + protected function handle() |
| 38 | + { |
| 39 | + $this->comment( |
| 40 | + "cache:purge command's output {$this->argument('argument')} {$this->option('option')}" |
| 41 | + ); |
| 42 | + |
| 43 | + return 0; |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +Leaf MVC's aloe command line tool is built on top of Symfony's console component, so you can use the same methods and properties you would use in a Symfony command. You can read more about Symfony Console [here](https://symfony.com/doc/current/components/console.html). |
| 49 | + |
| 50 | +## Manually creating a command |
| 51 | + |
| 52 | +All commands created through the `g:command` command are stored in the `app/console` directory and are automatically loaded by Leaf MVC. However, you can also create a command manually by creating a new class that extends the `Command` class and implementing the `handle()` method just like the example above. |
| 53 | + |
| 54 | +```php |
| 55 | +<?php |
| 56 | + |
| 57 | +namespace App\Console; |
| 58 | + |
| 59 | +use Aloe\Command; |
| 60 | + |
| 61 | +class CachePurgeCommand extends Command |
| 62 | +{ |
| 63 | + protected static $defaultName = 'cache:purge'; |
| 64 | + public $description = 'cache:purge command\'s description'; |
| 65 | + public $help = 'cache:purge command\'s help'; |
| 66 | + |
| 67 | + protected function config() |
| 68 | + { |
| 69 | + $this |
| 70 | + ->setArgument('argument', 'optional', 'argument description') |
| 71 | + ->setOption('option', 'o', 'required', 'option description'); |
| 72 | + } |
| 73 | + |
| 74 | + protected function handle() |
| 75 | + { |
| 76 | + $this->comment( |
| 77 | + "cache:purge command's output {$this->argument('argument')} {$this->option('option')}" |
| 78 | + ); |
| 79 | + |
| 80 | + return 0; |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +You can register this component by heading over to the `app/console/commands.php` file and adding the command to the `register()` method. |
| 86 | + |
| 87 | +```php |
| 88 | +<?php |
| 89 | + |
| 90 | +namespace App\Console; |
| 91 | + |
| 92 | +class Commands |
| 93 | +{ |
| 94 | + /** |
| 95 | + * Register commands |
| 96 | + * |
| 97 | + * @param $console |
| 98 | + * @return void |
| 99 | + * |
| 100 | + */ |
| 101 | + public static function register($console): void |
| 102 | + { |
| 103 | + $console->register([ |
| 104 | + ExampleCommand::class, |
| 105 | + CachePurgeCommand::class |
| 106 | + ]); |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +## Command Arguments |
| 112 | + |
| 113 | +Command arguments are values that are passed to the command when it is run in the console. For example, if you have a command named `example` and you run it like this: |
| 114 | + |
| 115 | +```bash |
| 116 | +php leaf example argument |
| 117 | +``` |
| 118 | + |
| 119 | +The `argument` value is an argument that is passed to the command. You can access the argument in the `config()` method using the `setArgument()` method. It typically follows the same convention as symfony console's `addArgument` except that instead of passing in `InputArgument::state`, you just pass in the state as a string. For example, instead of `InputArgument::REQUIRED`, you just pass in `"required"`, any case is supported. |
| 120 | + |
| 121 | +```php |
| 122 | +protected function config() |
| 123 | +{ |
| 124 | + $this->setArgument('argument', 'required', 'argument description'); |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +You can access the argument in the `handle()` method using the `argument()` method. |
| 129 | + |
| 130 | +```php |
| 131 | +protected function handle() |
| 132 | +{ |
| 133 | + $this->comment("example command's output {$this->argument('argument')}"); |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +## Command Options |
| 138 | + |
| 139 | +Command options are values that are passed to the command when it is run in the console. For example, if you have a command named `example` and you run it like this: |
| 140 | + |
| 141 | +```bash |
| 142 | +php leaf example --option=value |
| 143 | +``` |
| 144 | + |
| 145 | +To add an option to your command, you can use the `setOption()` method in the `config()` method. It typically follows the same convention as symfony console's `addOption` except that instead of passing in `InputOption::state`, you just pass in the state as a string. For example, instead of `InputOption::VALUE_REQUIRED`, you just pass in `"required"`, any case is supported. |
| 146 | + |
| 147 | +```php |
| 148 | +protected function config() |
| 149 | +{ |
| 150 | + $this->setOption('option', 'o', 'required', 'option description'); |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +You can access the option in the `handle()` method using the `option()` method. |
| 155 | + |
| 156 | +```php |
| 157 | +protected function handle() |
| 158 | +{ |
| 159 | + $this->comment("example command's output {$this->option('option')}"); |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +## Command Input |
| 164 | + |
| 165 | +Aloe makes it easier to grab the Symfony input object from anywhere in your command. This means that you don't have to pass in the `$input` variable to the `handle()` method. Instead, you can use the `input()` method. |
| 166 | + |
| 167 | +```php |
| 168 | +public function handle() |
| 169 | +{ |
| 170 | + $input = $this->input(); |
| 171 | + $name = $input->getArgument('name'); |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | +## Command Output |
| 176 | + |
| 177 | +Aloe makes it easier to grab and output text to the console from anywhere in your command. Unlike with symfony console, you don't have to pass in the `$output` variable to the `handle()` method. Instead, you can use `ouput()`, `write()`, `writeln()`, `comment()`, `info()`, `error()`, `question()` and `link()` methods. |
| 178 | + |
| 179 | +### output() |
| 180 | + |
| 181 | +This method either outputs text in your console or returns the Symfony output object. If a value is passed into `output()`, it will write the value to the console. |
| 182 | + |
| 183 | +```php |
| 184 | +public function handle() |
| 185 | +{ |
| 186 | + $this->output('Hello World'); |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +If no value is passed into `output()`, it will return the Symfony output object. |
| 191 | + |
| 192 | +```php |
| 193 | +public function handle() |
| 194 | +{ |
| 195 | + $output = $this->output(); |
| 196 | + $output->writeln('This is output'); |
| 197 | +} |
| 198 | +``` |
| 199 | + |
| 200 | +### write() |
| 201 | + |
| 202 | +This method writes text to the console. It is the same as the `output()->write()` method. |
| 203 | + |
| 204 | +```php |
| 205 | +public function handle() |
| 206 | +{ |
| 207 | + $this->write('Hello World'); |
| 208 | +} |
| 209 | +``` |
| 210 | + |
| 211 | +### writeln() |
| 212 | + |
| 213 | +This method writes text to the console and adds a new line. It is the same as the `output()->writeln()` method. |
| 214 | + |
| 215 | +```php |
| 216 | +public function handle() |
| 217 | +{ |
| 218 | + $this->writeln('Hello World'); |
| 219 | +} |
| 220 | +``` |
| 221 | + |
| 222 | +### comment() |
| 223 | + |
| 224 | +This method writes a comment styled message to the console and adds a new line. It is the same as the `output()->writeln()` method with the `SymfonyStyle::COMMENT` style. |
| 225 | + |
| 226 | +```php |
| 227 | +public function handle() |
| 228 | +{ |
| 229 | + $this->comment('Hello World'); |
| 230 | +} |
| 231 | +``` |
| 232 | + |
| 233 | +### info() |
| 234 | + |
| 235 | +This method writes an info styled message to the console and adds a new line. It is the same as the `output()->writeln()` method with the `SymfonyStyle::INFO` style. |
| 236 | + |
| 237 | +```php |
| 238 | +public function handle() |
| 239 | +{ |
| 240 | + $this->info('Hello World'); |
| 241 | +} |
| 242 | +``` |
| 243 | + |
| 244 | +### error() |
| 245 | + |
| 246 | +This method writes an error styled message to the console and adds a new line. It is the same as the `output()->writeln()` method with the `SymfonyStyle::ERROR` style. |
| 247 | + |
| 248 | +```php |
| 249 | +public function handle() |
| 250 | +{ |
| 251 | + $this->error('Hello World'); |
| 252 | +} |
| 253 | +``` |
| 254 | + |
| 255 | +### question() |
| 256 | + |
| 257 | +This method writes a question styled message to the console and adds a new line. It is the same as the `output()->writeln()` method with the `SymfonyStyle::QUESTION` style. |
| 258 | + |
| 259 | +```php |
| 260 | +public function handle() |
| 261 | +{ |
| 262 | + $this->question('Hello World'); |
| 263 | +} |
| 264 | +``` |
| 265 | + |
| 266 | +### link() |
| 267 | + |
| 268 | +This method writes a link to the console and adds a new line. |
| 269 | + |
| 270 | +```php |
| 271 | +public function handle() |
| 272 | +{ |
| 273 | + $this->link('https://leafphp.dev', 'Leaf PHP'); |
| 274 | +} |
| 275 | +``` |
| 276 | + |
| 277 | +## Command Questions |
| 278 | + |
| 279 | +Aloe makes it easier to ask questions in your command. You can use the `ask()`, `confirm()`, `askRaw()`, `autoComplete()`, `choice()` and `multiChoice()` methods. |
| 280 | + |
| 281 | +### ask() |
| 282 | + |
| 283 | +This method asks a question and returns the answer. It takes in 2 parameters: |
| 284 | + |
| 285 | +- the question to ask |
| 286 | +- the default answer (optional) |
| 287 | + |
| 288 | +```php |
| 289 | +public function handle() |
| 290 | +{ |
| 291 | + $name = $this->ask('What is your name?', 'Leaf'); |
| 292 | +} |
| 293 | +``` |
| 294 | + |
| 295 | +### askRaw() |
| 296 | + |
| 297 | +This is the same as the `ask()` method above, except that it does not trim the results that the user enters. Whatever the user enters is returned as is. |
| 298 | + |
| 299 | +```php |
| 300 | +public function handle() |
| 301 | +{ |
| 302 | + $name = $this->askRaw('What is your name?', 'Leaf'); |
| 303 | +} |
| 304 | +``` |
| 305 | + |
| 306 | +### autoComplete() |
| 307 | + |
| 308 | +This method allows you to ask a question and provide a list of values that the user can choose from. The user's answer will be auto-completed as they type if it matches one of the values in the list. It takes in 3 parameters: |
| 309 | + |
| 310 | +- the question to ask |
| 311 | +- the list of values to choose from |
| 312 | +- the default answer (optional) |
| 313 | + |
| 314 | +```php |
| 315 | +public function handle() |
| 316 | +{ |
| 317 | + $name = $this->autoComplete('What is your name?', ['Leaf', 'PHP'], 'Leaf'); |
| 318 | +} |
| 319 | +``` |
| 320 | + |
| 321 | +### choice() |
| 322 | + |
| 323 | +This method allows you to ask a question and provide a list of values that the user can choose from. The user must select one of the values in the list. It takes in 4 parameters: |
| 324 | + |
| 325 | +- the question to ask |
| 326 | +- the list of values to choose from |
| 327 | +- the error message to display if the user does not select one of the values in the list |
| 328 | +- the default answer (optional) |
| 329 | + |
| 330 | +```php |
| 331 | +public function handle() |
| 332 | +{ |
| 333 | + $name = $this->choice('What is your name?', ['Leaf', 'PHP'], 'Please select a name'); |
| 334 | +} |
| 335 | +``` |
| 336 | + |
| 337 | +### multiChoice() |
| 338 | + |
| 339 | +This method allows you to ask a question and provide a list of values that the user can choose from. The user must select one or more of the values in the list. It takes in 4 parameters: |
| 340 | + |
| 341 | +- the question to ask |
| 342 | +- the list of values to choose from |
| 343 | +- the error message to display if the user does not select one of the values in the list |
| 344 | +- the default answer (optional) |
| 345 | + |
| 346 | +```php |
| 347 | +public function handle() |
| 348 | +{ |
| 349 | + $name = $this->multiChoice('What is your name?', ['Leaf', 'PHP'], 'Please select a name'); |
| 350 | +} |
| 351 | +``` |
| 352 | + |
| 353 | +### confirm() |
| 354 | + |
| 355 | +This method asks a yes/no question and returns the answer. It takes in 2 parameters: |
| 356 | + |
| 357 | +- the question to ask |
| 358 | +- the default answer (optional) |
| 359 | + |
| 360 | +```php |
| 361 | +public function handle() |
| 362 | +{ |
| 363 | + $name = $this->confirm('Are you sure?', 'yes'); |
| 364 | +} |
| 365 | +``` |
| 366 | + |
| 367 | +### secret() |
| 368 | + |
| 369 | +This method asks a question but hides the keystrokes. It takes in 2 parameters: |
| 370 | + |
| 371 | +- the question to ask |
| 372 | +- use hidden fallback (optional) |
| 373 | + |
| 374 | +```php |
| 375 | +$password = $this->secret('Confirm your password'); |
| 376 | +``` |
0 commit comments