Skip to content

Commit c9a42bb

Browse files
authored
docs: improve example command (#65)
1 parent e7d168f commit c9a42bb

File tree

1 file changed

+55
-12
lines changed

1 file changed

+55
-12
lines changed

.docs/README.md

+55-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
- [Setup](#usage)
66
- [Configuration](#configuration)
7-
- [Example command](#command)
7+
- [Example command](#example-command)
88
- [Entrypoint](#entrypoint)
99

1010
## Setup
@@ -29,15 +29,16 @@ console:
2929
version: '1.0'
3030
catchExceptions: true / false
3131
autoExit: true / false
32-
url: https://contributte.org
32+
url: https://example.com
3333
lazy: false
3434
```
3535

36-
In SAPI (CLI) mode there is no http request and thus no URL address. This is an inconvenience you have to solve by yourself - via the `console.url` option.
36+
In SAPI (CLI) mode, there is no HTTP request and thus no URL address.
37+
You have to set base URL on your own so that link generator works. Use `console.url` option:
3738

3839
```neon
3940
console:
40-
url: https://contributte.org
41+
url: https://example.com
4142
```
4243

4344
### Helpers
@@ -102,29 +103,71 @@ services:
102103
tags: [console.command: app:foo]
103104
```
104105

105-
## Command
106+
## Example command
106107

107-
### Create command
108+
In case of having `console.php` as entrypoint (see below), this would add a user with username `john.doe` to database:
108109

109-
```php
110+
> `php console.php user:add john.doe`
110111
112+
```php
111113
namespace App\Console;
112114

113115
use Symfony\Component\Console\Command\Command;
116+
use Symfony\Component\Console\Input\InputArgument;
114117
use Symfony\Component\Console\Input\InputInterface;
115118
use Symfony\Component\Console\Output\OutputInterface;
116119

117-
final class FooCommand extends Command
120+
final class AddUserCommand extends Command
118121
{
119122

123+
private UsersModel $usersModel;
124+
125+
/**
126+
* Pass dependencies with constructor injection
127+
*/
128+
public function __construct(UsersModel $usersModel)
129+
{
130+
parent::__construct(); // don't forget parent call as we extends from Command
131+
$this->usersModel = $usersModel;
132+
}
133+
120134
protected function configure(): void
121135
{
122-
$this->setName('foo');
136+
// choose command name
137+
$this->setName('user:add')
138+
// description (optional)
139+
->setDescription('Adds user with given username to database')
140+
// arguments (maybe required or not)
141+
->setArgument('username', InputArgument::REQUIRED, 'User\'s username');
142+
// you can list options as well (refer to symfony/console docs for more info)
123143
}
124144

125-
protected function execute(InputInterface $input, OutputInterface $output): void
145+
/**
146+
* Don't forget to return 0 for success or non-zero for error
147+
*/
148+
protected function execute(InputInterface $input, OutputInterface $output): int
126149
{
127-
// Some magic..
150+
// retrieve passed arguments/options
151+
$username = $input->getArgument('username');
152+
153+
// you can use symfony/console output
154+
$output->writeln(\sprintf('Adding user %s…', $username));
155+
156+
try {
157+
// do your logic
158+
$this->usersModel->add($username);
159+
// styled output as well
160+
$output->writeln('<success>✔ Successfully added</success>');
161+
return 0;
162+
163+
} catch (\Exception $e) {
164+
// handle error
165+
$output->writeln(\sprintf(
166+
'<error>❌ Error occurred: </error>',
167+
$e->getMessage(),
168+
));
169+
return 1;
170+
}
128171
}
129172

130173
}
@@ -134,7 +177,7 @@ final class FooCommand extends Command
134177

135178
```neon
136179
services:
137-
- App\Console\FooCommand
180+
- App\Console\AddUserCommand
138181
```
139182

140183
Maybe you will have to flush the `temp/cache` directory.

0 commit comments

Comments
 (0)