Slide 1

Slide 1 text

SensioLabs Better Console Applications

Slide 2

Slide 2 text

Christopher Hertel Software Developer at SensioLabs Symfony User Group Berlin @el_stoffel

Slide 3

Slide 3 text

Console Applications

Slide 4

Slide 4 text

Console Commands

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

CLI SAPI

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

SensioLabs Console Component

Slide 10

Slide 10 text

symfony/console ~ 85.000.000 Downloads

Slide 11

Slide 11 text

CLI Application Framework

Slide 12

Slide 12 text

$ composer req symfony/console Installation

Slide 13

Slide 13 text

Application

Slide 14

Slide 14 text

app

Slide 15

Slide 15 text

chmod +x ./app

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Command

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

More Dependencies Use Symfony Flex

Slide 20

Slide 20 text

$ composer create-project \ symfony/skeleton my-cli-app Installation

Slide 21

Slide 21 text

bin/console

Slide 22

Slide 22 text

php bin/console hello

Slide 23

Slide 23 text

Tool-Tip Collision $ composer req nunomaduro/collision

Slide 24

Slide 24 text

Tool-Tip Collision

Slide 25

Slide 25 text

SensioLabs Application Types

Slide 26

Slide 26 text

Jobs

Slide 27

Slide 27 text

• running in background • no interaction • controlled by server • e.g. queue workers

Slide 28

Slide 28 text

Helper

Slide 29

Slide 29 text

• running in foreground • interaction possible • controlled by user • e.g. generator or debugging CMDs

Slide 30

Slide 30 text

Tools

Slide 31

Slide 31 text

• combining both • w/ or w/o interaction • used while development & CI • e.g. Composer, PHPUnit

Slide 32

Slide 32 text

We rarely build tools

Slide 33

Slide 33 text

Simple Question: Who executes this command?

Slide 34

Slide 34 text

SensioLabs Example

Slide 35

Slide 35 text

Billing Run

Slide 36

Slide 36 text

• Generate Invoices • Charge payment • Generate PDF • Mail to customer • Export postal data

Slide 37

Slide 37 text

• Console Command • Executed monthly • Executed by developer

Slide 38

Slide 38 text

use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\Question; use Symfony\Component\Stopwatch\Stopwatch; /** * Monthly Billing run for all active subscribers of our magazines. * * - Invoice gets generated * - Payment is executed * - Email is sent * - Magazine export is generated * * @author C■■■■■■■■■■ H■■■■■ */ class BillingRunCommand extends ContainerAwareCommand { protected function configure(): void { $this->setName('app:billing:run'); $this->addArgument('period', InputArgument::OPTIONAL, 'Billing Period', ''); } protected function execute(InputInterface $input, OutputInterface $output): int { if ('dev' === $this->getContainer()->getParameter('kernel.environment')) { $stopwatch = new Stopwatch(); $stopwatch->start('billing-run');

Slide 39

Slide 39 text

} protected function execute(InputInterface $input, OutputInterface $output): int { if ('dev' === $this->getContainer()->getParameter('kernel.environment')) { $stopwatch = new Stopwatch(); $stopwatch->start('billing-run'); } $period = \DateTimeImmutable::createFromFormat('m-Y', $input->getArgument('per if (false === $period) { $questionHelper = $this->getHelper('question'); $question = new Question('Which period do you want? (format: mm-yyyy)'); $question->setNormalizer(function ($period) { return \DateTimeImmutable::createFromFormat('m-Y', (string) $period); }); $question->setValidator(function ($period) { if (false === $period) { throw new \InvalidArgumentException('The given value was not a val } return $period; }); $period = $questionHelper->ask($input, $output, $question); } $output->writeln(sprintf('Start billing run for %s', $period->for

Slide 40

Slide 40 text

$period = $questionHelper->ask($input, $output, $question); } $output->writeln(sprintf('Start billing run for %s', $period->for $output->writeln('============================='.PHP_EOL); $customers = $this->fetchActiveCustomer(); $output->writeln(sprintf('Loaded %d customers to process'.PHP_EOL $invoices = $this->generateInvoice($output, $customers, $period); $this->payInvoices($output, $invoices); $this->sendInvoices($output, $invoices); $this->exportMagazines($output, $period, $invoices); $output->writeln(['', 'Done.', '']); if ('dev' === $this->getContainer()->getParameter('kernel.environment')) { $output->writeln((string) $stopwatch->stop('billing-run')); } return 0; } /** * @return Customer[] */ private function fetchActiveCustomer(): array {

Slide 41

Slide 41 text

/** * @return Invoice[] */ private function generateInvoice(OutputInterface $output, array $customers, \DateT { $entityManager = $this->getContainer()->get('doctrine.orm.default_entity_manag $output->writeln('Generate Invoices:'); $invoices = []; $progressBar = new ProgressBar($output, count($customers)); $progressBar->start(); foreach ($customers as $i => $customer) { $invoice = Invoice::forCustomer($customer, $period); $invoices[] = $invoice; $entityManager->persist($invoice); $progressBar->advance(); } $progressBar->finish(); $output->writeln(''); $output->writeln(sprintf('Generated %d invoices to pay'.PHP $entityManager->flush(); return $invoices; }

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

It's working

Slide 44

Slide 44 text

But … umm

Slide 45

Slide 45 text

Let's refactor

Slide 46

Slide 46 text

Testing!

Slide 47

Slide 47 text

ApplicationTester CommandTester

Slide 48

Slide 48 text

Helper to execute an Application or Command

Slide 49

Slide 49 text

Easily combined with KernelTestCase

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

SensioLabs Input Interaction

Slide 53

Slide 53 text

use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\Question; use Symfony\Component\Stopwatch\Stopwatch; /** * Monthly Billing run for all active subscribers of our magazines. * * - Invoice gets generated * - Payment is executed * - Email is sent * - Magazine export is generated * * @author C■■■■■■■■■■ H■■■■■ */ class BillingRunCommand extends ContainerAwareCommand { protected function configure(): void { $this->setName('app:billing:run'); $this->addArgument('period', InputArgument::OPTIONAL, 'Billing Period', ''); } protected function execute(InputInterface $input, OutputInterface $output): int { if ('dev' === $this->getContainer()->getParameter('kernel.environment')) { $stopwatch = new Stopwatch(); $stopwatch->start('billing-run');

Slide 54

Slide 54 text

} protected function execute(InputInterface $input, OutputInterface $output): int { if ('dev' === $this->getContainer()->getParameter('kernel.environment')) { $stopwatch = new Stopwatch(); $stopwatch->start('billing-run'); } $period = \DateTimeImmutable::createFromFormat('m-Y', $input->getArgument('per if (false === $period) { $questionHelper = $this->getHelper('question'); $question = new Question('Which period do you want? (format: mm-yyyy)'); $question->setNormalizer(function ($period) { return \DateTimeImmutable::createFromFormat('m-Y', (string) $period); }); $question->setValidator(function ($period) { if (false === $period) { throw new \InvalidArgumentException('The given value was not a val } return $period; }); $period = $questionHelper->ask($input, $output, $question); } $output->writeln(sprintf('Start billing run for %s', $period->for

Slide 55

Slide 55 text

class ExampleCommand extends Command { protected function configure(): void { // TODO: IMPLEMENT } protected function execute(InputInterface $input, OutputInterface $output): int { // TODO: IMPLEMENT } }

Slide 56

Slide 56 text

class ExampleCommand extends Command { protected function configure(): void { // TODO: IMPLEMENT } protected function initialize(InputInterface $input, OutputInterface $output): void { // TODO: IMPLEMENT } protected function interact(InputInterface $input, OutputInterface $output): void { // TODO: IMPLEMENT } protected function execute(InputInterface $input, OutputInterface $output): int { // TODO: IMPLEMENT } } Lazy Commands

Slide 57

Slide 57 text

protected function interact(InputInterface $input, OutputInterface $output) { $period = \DateTimeImmutable::createFromFormat('m-Y', (string) $input->getArgument('period')); if (false === $period) { $questionHelper = $this->getHelper('question'); $question = new Question('Which period do you want? (format: mm-yyyy)'); $question->setNormalizer(function ($period) { return \DateTimeImmutable::createFromFormat('m-Y', (string) $period); }); $question->setValidator(function ($period) { if (false === $period) { throw new \InvalidArgumentException('The given value was not a valid period, use mm-y } return $period; }); $period = $questionHelper->ask($input, $output, $question); } $input->setArgument('period', $period); }

Slide 58

Slide 58 text

protected function execute(InputInterface $input, OutputInterface $output): int { if ('dev' === $this->getContainer()->getParameter('kernel.environment')) { $stopwatch = new Stopwatch(); $stopwatch->start('billing-run'); } $period = $input->getArgument('period'); $output->writeln(sprintf('Start billing run for %s', $period->format(' $output->writeln('============================='.PHP_EOL); // ... protected function configure(): void { $this->setName('app:billing:run'); $this->addArgument('period', InputArgument::REQUIRED, 'Billing Period'); }

Slide 59

Slide 59 text

SensioLabs Console Events

Slide 60

Slide 60 text

•console.command •console.error •console.terminate

Slide 61

Slide 61 text

console. command console. terminate console. error Command Execution Command Lifecycle on error

Slide 62

Slide 62 text

protected function execute(InputInterface $input, OutputInterface $output): int { if ('dev' === $this->getContainer()->getParameter('kernel.environment')) { $stopwatch = new Stopwatch(); $stopwatch->start('billing-run'); } $period = $input->getArgument('period'); $output->writeln(sprintf('Start billing run for %s', $period->format( $output->writeln('============================='.PHP_EOL); // ... $output->writeln(['', 'Done.', '']); if ('dev' === $this->getContainer()->getParameter('kernel.environment')) { $output->writeln((string) $stopwatch->stop('billing-run')); } return 0; }

Slide 63

Slide 63 text

class StopwatchListener implements EventSubscriberInterface { private $stopwatch; public function __construct(Stopwatch $stopwatch) { $this->stopwatch = $stopwatch; } public static function getSubscribedEvents() { return [ ConsoleEvents::COMMAND => 'startStopwatch', ConsoleEvents::TERMINATE => 'stopStopwatch', ]; } public function startStopwatch(ConsoleCommandEvent $event): void { $this->stopwatch->start($event->getCommand()->getName()); } public function stopStopwatch(ConsoleTerminateEvent $event): void { $name = $event->getCommand()->getName();

Slide 64

Slide 64 text

} public static function getSubscribedEvents() { return [ ConsoleEvents::COMMAND => 'startStopwatch', ConsoleEvents::TERMINATE => 'stopStopwatch', ]; } public function startStopwatch(ConsoleCommandEvent $event): void { $this->stopwatch->start($event->getCommand()->getName()); } public function stopStopwatch(ConsoleTerminateEvent $event): void { $name = $event->getCommand()->getName(); if (!$this->stopwatch->isStarted($name)) { return; } $event->getOutput()->writeln((string) $this->stopwatch->stop($name)); } }

Slide 65

Slide 65 text

protected function execute(InputInterface $input, OutputInterface $output): int { $period = $input->getArgument('period'); $output->writeln(sprintf('Start billing run for %s', $period->format('m-Y'))); $output->writeln('============================='.PHP_EOL); $customers = $this->fetchActiveCustomer(); $output->writeln(sprintf('Loaded %d customers to process'.PHP_EOL, count($cust $invoices = $this->generateInvoice($output, $customers, $period); $this->payInvoices($output, $invoices); $this->sendInvoices($output, $invoices); $this->exportMagazines($output, $period, $invoices); $output->writeln(['', 'Done.', '']); return 0; }

Slide 66

Slide 66 text

SensioLabs Command == Glue Code

Slide 67

Slide 67 text

No business logic in a command

Slide 68

Slide 68 text

protected function execute(InputInterface $input, OutputInterface $output): int { $period = $input->getArgument('period'); $output->writeln(sprintf('Start billing run for %s', $period->format('m-Y'))); $output->writeln('============================='.PHP_EOL); $customers = $this->fetchActiveCustomer(); $output->writeln(sprintf('Loaded %d customers to process'.PHP_EOL, count($cust $invoices = $this->generateInvoice($output, $customers, $period); $this->payInvoices($output, $invoices); $this->sendInvoices($output, $invoices); $this->exportMagazines($output, $period, $invoices); $output->writeln(['', 'Done.', '']); return 0; }

Slide 69

Slide 69 text

private function generateInvoice(OutputInterface $output, array $customers, \DateTimeImmutable { $entityManager = $this->getContainer()->get('doctrine.orm.default_entity_manager'); $output->writeln('Generate Invoices:'); $invoices = []; $progressBar = new ProgressBar($output, count($customers)); $progressBar->start(); foreach ($customers as $i => $customer) { $invoice = Invoice::forCustomer($customer, $period); $invoices[] = $invoice; $entityManager->persist($invoice); $progressBar->advance(); } $progressBar->finish(); $output->writeln(''); $output->writeln(sprintf('Generated %d invoices to pay'.PHP_EOL, count($i $entityManager->flush(); return $invoices; }

Slide 70

Slide 70 text

Move business logic to service layer

Slide 71

Slide 71 text

class BillingRunCommand extends Command { private $billingRun; public function __construct(BillingRun $billingRun) { parent::__construct('app:billing:run'); $this->billingRun = $billingRun; } protected function configure(): void { $this->addArgument('period', InputArgument::REQUIRED, 'Billing Period'); } protected function execute(InputInterface $input, OutputInterface $output): int { $period = $input->getArgument('period'); $output->writeln(sprintf('Start billing run for %s', $period->format('m-Y' $output->writeln('============================='.PHP_EOL); $this->billingRun->start($period, $output); $output->writeln(['', 'Done.', '']); return 0; }

Slide 72

Slide 72 text

class BillingRun { private $entityManager; private $paymentProvider; private $mailer; private $exporter; public function __construct( EntityManagerInterface $entityManager, PaymentProvider $paymentProvider, Mailer $mailer, Exporter $exporter ) { $this->entityManager = $entityManager; $this->paymentProvider = $paymentProvider; $this->mailer = $mailer; $this->exporter = $exporter; } public function start(\DateTimeImmutable $period, OutputInterface $output): void { $customers = $this->fetchActiveCustomer(); $output->writeln(sprintf('Loaded %d customers to process'.PHP_EOL, count($c $invoices = $this->generateInvoice($output, $customers, $period); $this->payInvoices($output, $invoices); $this->sendInvoices($output, $invoices); $this->exportMagazines($output, $period, $invoices); }

Slide 73

Slide 73 text

Cleaner Dependencies

Slide 74

Slide 74 text

Easier Testing

Slide 75

Slide 75 text

Tool-Tip PHPBench $ composer req phpbench/phpbench

Slide 76

Slide 76 text

Tool-Tip PHPBench class TimeConsumerBench { /** * @Revs(1000) * @Iterations(5) */ public function benchConsume() { // ... } }

Slide 77

Slide 77 text

SensioLabs Output

Slide 78

Slide 78 text

Slide 79

Slide 79 text

BillingRun depends on Symfony\Component\Console

Slide 80

Slide 80 text

Really?

Slide 81

Slide 81 text

NOPE!

Slide 82

Slide 82 text

Decouple business logic from Framework

Slide 83

Slide 83 text

Tool-Tip Deptrac $ composer req sensiolabs-de/deptrac

Slide 84

Slide 84 text

Logging

Slide 85

Slide 85 text

Perfect for background jobs

Slide 86

Slide 86 text

class BillingRun { private $entityManager; private $paymentProvider; private $mailer; private $exporter; private $logger; public function __construct( EntityManagerInterface $entityManager, PaymentProvider $paymentProvider, Mailer $mailer, Exporter $exporter, LoggerInterface $logger ) { $this->entityManager = $entityManager; $this->paymentProvider = $paymentProvider; $this->mailer = $mailer; $this->exporter = $exporter; $this->logger = $logger; } public function start(\DateTimeImmutable $period): void { $customers = $this->fetchActiveCustomer(); $this->logger->info(sprintf('Loaded %d customers to process', count($customers))); $invoices = $this->generateInvoice($customers, $period); $this->payInvoices($invoices); $this->sendInvoices($invoices);

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

Verbosity Level

Slide 89

Slide 89 text

Quiet •-q or --quiet •no output •logging >= error

Slide 90

Slide 90 text

Normal •all output •logging >= warning

Slide 91

Slide 91 text

Verbose •-v •all output •logging >= notice

Slide 92

Slide 92 text

Very Verbose •-vv •all output •logging >= info

Slide 93

Slide 93 text

Debug •-vvv •all output •all logs + extended context

Slide 94

Slide 94 text

Output

Slide 95

Slide 95 text

ProgressBar?

Slide 96

Slide 96 text

easy way out callable

Slide 97

Slide 97 text

public function start(\DateTimeImmutable $period, callable $progress, callable $error): { $customers = $this->entityManager->getRepository(Customer::class)->findByActive(tru $customerNum = count($customers); $invoices = []; foreach ($customers as $i => $customer) { $invoice = Invoice::forCustomer($customer, $period); try { $this->paymentProvider->authorize($invoice); } catch (PaymentException $exception) { $error($exception); } $this->mailer->sendInvoice($invoice); $this->entityManager->persist($invoice); $invoices[] = $invoice; $progress(++$i, $customerNum); } $this->exporter->export($period, $invoices); $this->entityManager->flush(); }

Slide 98

Slide 98 text

protected function execute(InputInterface $input, OutputInterface $output): int { $period = $input->getArgument('period'); $output->writeln(sprintf('Start billing run for %s', $period->format('m-Y'))); $output->writeln('============================='.PHP_EOL); $progress = new ProgressBar($output); $onProgress = function (int $count, int $max) use ($output, $progress) { $this->onProgress($output, $progress, $count, $max); }; $onError = function (PaymentException $exception) use ($output) { $output->writeln(''.$exception->getMessage().''); }; $this->billingRun->start($period, $onProgress, $onError); $output->writeln(['', 'Done.', '']); return 0; }

Slide 99

Slide 99 text

Alternative BillingRun Events

Slide 100

Slide 100 text

Alternative BillingRun Observer

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

SensioLabs Features

Slide 103

Slide 103 text

SymfonyStyle

Slide 104

Slide 104 text

Helper Class on top of Input & Output

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

Sections NEW IN SYMFONY 4.1

Slide 107

Slide 107 text

$ bin/console app:billing:run 05-2018 Section Progress Section Errors

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

SensioLabs Thank You!

Slide 111

Slide 111 text

Feedback //joind.in/talk/36239

Slide 112

Slide 112 text

Questions?