All code samples and slides available after presentation
petre$ bin/console demo:hello
Hello World!
petre$ bin/console demo:hello "Symfony Bucharest"
Hello Symfony Bucharest!
namespace SymfonyBucharest\UnderstandingConsoleBundle\Command;
use ...
/**
* Greet a person or group of people.
*/
class HelloWorldCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('demo:hello')
->setDescription('Greet a person or group of people')
->addArgument(
'who',
InputArgument::OPTIONAL,
'The person or group to greet',
'World'
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$who = $input->getArgument('who');
$output->writeln("Hello {$who}!");
}
}
$this
->setName('demo:hello')
->setDescription('Greet a person or group of people')
->addArgument(
'who', # Argument Identifier
InputArgument::OPTIONAL, # Required/Optional/Array
'The person or group to greet', # Description
'World' # Default Value
);
petre$ bin/console demo:hello --help
Usage:
demo:hello [<who>]
Arguments:
who The person or group to greet [default: "World"]
Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
-e, --env=ENV The Environment name. [default: "dev"]
--no-debug Switches off debug mode.
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Help:
Greet a person or group of people
$this->addOption(
'greeting', # Option Identifier
'g', # Option Shortcut
InputOption::VALUE_OPTIONAL, # Required/Optional/Array
'The greeting to use', # Description
'Hello' # Default Value
);
$who = $input->getArgument('who');
$greeting = $input->getOption('greeting');
$output->writeln("{$greeting} {$who}!");
configure()
execute()
petre$ bin/console demo:hello "Symfony București" --greeting "Salut"
Salut Symfony București!
petre$ bin/console demo:hello "Symfony București" -g "Salut"
Salut Symfony București!
Some simple styles are built-in
$output->writeln('<info>Information message</info>');
$output->writeln('<comment>A comment</comment>');
$output->writeln('<question>User interaction/Question</question>');
$output->writeln('<error>Errors, exceptions, all that good stuff</error>');
$yoloStyle = new OutputFormatterStyle('yellow', 'green', ['bold']);
$output->getFormatter()->setStyle('yolo', $yoloStyle);
$output->writeln('<yolo>Custom style</yolo>');
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion('<question>Continue?</question> ', false);
if (true === $helper->ask($input, $output, $question)) {
return 0;
} else {
return 1;
}
petre$ bin/console demo:helper:question
The answer to the Ultimate Question of Life, The Universe, and Everything is 42. Correct? no
You need to crunch the numbers again. See you in ten million years.
petre$ bin/console demo:helper:progress -vvv
7/10 [===================>--------] 70% 6 secs/9 secs 8.0 MiB
$progress = new ProgressBar($output);
$progress->start(10);
for ($iterator = 0; $iterator < 10; $iterator++) {
$progress->advance();
sleep(1);
}
petre$ bin/console demo:helper:table
+--------------------------------------+------------+-----------+---------------------------------+
| ID | First Name | Last Name | Company |
+--------------------------------------+------------+-----------+---------------------------------+
| b142579b-9cd1-350e-a8ec-cc43c29718a6 | Lottie | Yost | Murray, Hamill and Koch |
| 06af6dd6-a5f3-3bd4-bfed-1628e26efdfe | Guido | Boyle | Gislason-McGlynn |
| 3b84e7a2-19c3-3e60-948b-203d94ac6b21 | Esta | Von | Kiehn-McClure |
| e133b0f3-b603-30dd-9c01-bd5a763cf87d | Fern | Denesik | Conn, Langosh and Buckridge |
| 291fdadf-7f13-3667-9c7b-f671e7beb447 | Ilene | McKenzie | Botsford, Kautzer and Schneider |
| 76e85603-ad97-3219-baa2-4b934dd79adc | Aisha | Crooks | Hudson Group |
| c34fbdeb-bc69-3fbc-84a0-16994720e3e1 | Brandt | Graham | Metz Group |
| 5d76dd0f-112a-394e-80cc-0c97e79f0fff | Dion | Bergstrom | Luettgen Group |
| 238cd2dd-34b1-39c4-9eee-a476d304a26c | Makayla | Mohr | Hyatt Ltd |
| ceb8362f-e579-388c-a272-dd756408693c | Conor | Schneider | Schmidt Group |
+--------------------------------------+------------+-----------+---------------------------------+
$faker = Factory::create();
$table = (new Table($output))
->setHeaders([
'ID',
'First Name',
'Last Name',
'Company',
]);
for ($iterator = 0; $iterator < 10; $iterator++) {
$table->addRow([
$faker->uuid,
$faker->firstName,
$faker->lastName,
$faker->company,
]);
}
$table->render();
Growing Legacy Codebase
Many CLI scripts, all doing their own thing
Our App
I <3 reporting
I run for 3 days and import 10GB of data
Product
Controller
Web User
Product
Service
Product
Repository
Data Store
(DB, API, etc.)
API Consumer
Product API Controller
CLI User
Product
Console Command
Our App