Web-less Console: Standalone Apps in Symfony 8.2

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →

Web-less Console: Standalone Apps in Symfony 8.2

The Console component runs almost everywhere PHP does: Composer, PHPStan, most frameworks and CMSes, and your own applications. Until recently, using it outside the full-stack framework meant giving up dependency injection and lazy services, or dragging the entire HTTP layer into a tool that never serves a single request.

Symfony 7.3 redesigned commands to be simpler, more powerful and more expressive. Symfony 8.2 goes further with a DI-powered, HTTP-less foundation: a real container, real service wiring, no web stack anywhere in sight.

Come see what modern Symfony commands look like today, and how standalone CLI applications finally become first-class citizens of the framework.

Robin Chalas

November 30, 2026

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

54 slides
  1. A real OSS company 3 Tons 4 7K+ Symfony core

    maintainers of projects delivered contributions shipped upstream SymfonyCon Warsaw speakers
  2. Before 7.3 class MoveTalkCommand extends Command { public function __construct(private

    Schedule $schedule) { parent::__construct(); } protected function configure(): void { $this ->setName('talk:move') ->addArgument('talk', InputArgument::REQUIRED) ->addArgument('room', InputArgument::REQUIRED) ->addOption('at', null, InputOption::VALUE_REQUIRED); } protected function execute(InputInterface $input, OutputInterface $output): int { $slug = $input->getArgument('talk'); $talk = $this->schedule->find($slug) ?? throw new InvalidArgumentException("No talk \"$slug\"."); $room = Room::tryFrom($input->getArgument('room')) ?? throw new InvalidArgumentException('Unknown room.'); $at = $input->getOption('at') ? \DateTimeImmutable::createFromFormat('H:i', $input->getOption('at')) : $talk->startsAt; $talk->moveTo($room, $at); // output… return Command::SUCCESS; } }
  3. Before 7.3 Command MoveTalkCommand extends __construct setHelperSet isEnabled getCode setDefinition

    addArgument setProcessTitle isHidden setHelp setAliases addUsage getSubscribedSignals 35 public methods ignoreValidationErrors getHelperSet run setCode getDefinition addOption getName setDescription getHelp getAliases getUsages handleSignal setApplication getApplication complete mergeApplicationDefinition getNativeDefinition setName setHidden getDescription getProcessedHelp getSynopsis getHelper
  4. Before 7.3 class MoveTalkCommand extends Command { public function __construct(private

    Schedule $schedule) { parent::__construct(); } protected function configure(): void { $this ->setName('talk:move') ->addArgument('talk', InputArgument::REQUIRED) ->addArgument('room', InputArgument::REQUIRED) ->addOption('at', null, InputOption::VALUE_REQUIRED); } protected function execute(InputInterface $input, OutputInterface $output): int { $slug = $input->getArgument('talk'); $talk = $this->schedule->find($slug) ?? throw new InvalidArgumentException("No talk \"$slug\"."); $room = Room::tryFrom($input->getArgument('room')) ?? throw new InvalidArgumentException('Unknown room.'); $at = $input->getOption('at') ? \DateTimeImmutable::createFromFormat('H:i', $input->getOption('at')) : $talk->startsAt; $talk->moveTo($room, $at); // output… return Command::SUCCESS; } }
  5. Before 7.3 class MoveTalkCommand extends Command { public function __construct(private

    Schedule $schedule) { parent::__construct(); } protected function configure(): void { $this ->setName('talk:move') ->addArgument('talk', InputArgument::REQUIRED) ->addArgument('room', InputArgument::REQUIRED) ->addOption('at', null, InputOption::VALUE_REQUIRED); } protected function execute(InputInterface $input, OutputInterface $output): int { $slug = $input->getArgument('talk'); $talk = $this->schedule->find($slug) ?? throw new InvalidArgumentException("No talk \"$slug\"."); $room = Room::tryFrom($input->getArgument('room')) ?? throw new InvalidArgumentException('Unknown room.'); $at = $input->getOption('at') ? \DateTimeImmutable::createFromFormat('H:i', $input->getOption('at')) : $talk->startsAt; $talk->moveTo($room, $at); // output… return Command::SUCCESS; } } lookup enum date
  6. 7.3: Invokable commands + #[AsCommand('talk:move')] - class MoveTalkCommand extends Command

    + final class MoveTalkCommand { public function __construct(private Schedule $schedule) { parent::__construct(); } + public function __construct(private Schedule $schedule) {} protected function configure(): void { … } protected function execute(InputInterface $input, OutputInterface $output): int + public function __invoke( + SymfonyStyle $io, + #[Argument] string $talk, + #[Argument] string $room, + #[Option] ?string $at = null, + ): int { … Yonel Ceruto
  7. 7.3: Invokable commands #[AsCommand('talk:move')] final class MoveTalkCommand { public function

    __construct(private Schedule $schedule) {} public function __invoke( SymfonyStyle $io, #[Argument] string $talk, #[Argument] string $room, #[Option] ?string $at = null, ): int { … } }
  8. 7.3: Invokable commands #[Argument] string $talk <talk> #[Argument] string $room

    <room> #[Option] ?string $at = null --at=AT $ bin/console talk:move --help Usage: talk:move [options] [--] <talk> <room> Options: --at=AT
  9. 7.4: Backed enums + - public function __invoke( SymfonyStyle $io,

    #[Argument] string $talk, #[Argument] string $room, #[Argument] Room $room, #[Option] ?string $at = null, ): int { $talk = $this->schedule->find($talk) ?? throw …; $room = Room::tryFrom($room) ?? throw …; $at = $at ? \DateTimeImmutable::createFromFormat('H:i', $at) : $talk->startsAt; Jérôme Tamarelle
  10. 8.1: Argument resolvers #[AsCommand('talk:move')] final class MoveTalkCommand { public function

    __construct(private Schedule $schedule) {} public function __invoke( SymfonyStyle $io, #[Argument] string $talk, + #[Argument] Talk $talk, #[Argument] Room $room, #[Option] ?string $at = null, + #[Option, MapDateTime(format: 'H:i')] ?\DateTimeImmutable $at = null, ): int { $talk = $this->schedule->find($talk) ?? throw …; $at = $at ? \DateTimeImmutable::createFromFormat('H:i', $at) : $talk->startsAt; + $talk->moveTo($room, $at ??= $talk->startsAt); Robin Chalas
  11. 8.1: Argument resolvers #[AsCommand('talk:move')] final class MoveTalkCommand { public function

    __invoke( SymfonyStyle $io, #[Argument] Talk $talk, #[Argument] Room $room, #[Option, MapDateTime(format: 'H:i')] ?\DateTimeImmutable $at = null, ): int { $talk->moveTo($room, $at ??= $talk->startsAt); $io->success("{$talk->title} → {$room->value}, {$at->format('H:i')}"); return Command::SUCCESS; } }
  12. 8.1: Argument resolvers "sensiolabs" BackedEnum · 100 Room::SensioLabs "14:00" DateTime

    · 100 DateTimeImmutable BuiltinType · 100 "web-less-console" TalkValueResolver · 0 Service · −50 Default · −100 Talk
  13. 8.1: Argument resolvers final class TalkValueResolver implements ValueResolverInterface { public

    function __construct(private Schedule $schedule) {} public function resolve(string $argumentName, InputInterface $input, ReflectionMember $member): iterable { if (Talk::class !== $member->getType()?->getName()) { return []; } $slug = $input->getArgument($member->getInputName()); return [$this->schedule->find($slug) ?? throw new InvalidArgumentException("No talk \"$slug\".")]; } }
  14. Testing public function testMovesTheTalk(): void { $talk = (new Schedule())->find('web-less-console');

    $command = new MoveTalkCommand(); $io = new SymfonyStyle(new ArrayInput([]), new NullOutput()); $command($io, $talk, Room::SensioLabs, new \DateTimeImmutable('14:00')); $this->assertSame(Room::SensioLabs, $talk->room); $this->assertSame('14:00', $talk->startsAt->format('H:i')); } $ vendor/bin/phpunit tests OK (1 test, 2 assertions)
  15. 8.1: Choice questions #[Argument, AskChoice('Which room?')] Room $room, $ bin/console

    talk:move keynote Which room?: [0] symfony [1] sensiolabs [2] smile-upsun >▍ Yonel Ceruto
  16. 8.1: Validation final class NewTalk { #[Argument, Ask('Title', constraints: [new

    Assert\NotBlank()])] public string $title; #[Option, Assert\Range(min: 1, max: 2)] public int $day = 1; #[Option] public Room $room = Room::Symfony; } public function __invoke(SymfonyStyle $io, #[MapInput] NewTalk $talk): int Yonel Ceruto Robin Chalas
  17. 8.1: Validation $ bin/console talk:add Title: $ bin/console talk:add \

    "Closing panel" --day=3 > [ERROR] This value should --day: This value should be not be blank. between 1 and 2. Title: > Closing panel [OK] Closing panel → day 1, symfony
  18. More in 8.1 Objects as default values Boolean defaults for

    negatable options #[Option] --yell / --no-yell Raw input, forwarded $input->getRawOptions() $input->unparse() \DateTimeImmutable $from = new \DateTimeImmutable() Robin Chalas Jesper Noordsij Théo Fidry
  19. The component alone // bin/standalone $schedule = new Schedule(); $app

    = new Application('agenda', '1.0'); $app->addCommand(new MoveTalkCommand($schedule)); $app->run();
  20. 8.1: A kernel without HTTP // src/Kernel.php use Symfony\Component\DependencyInjection\Kernel\AbstractKernel; use

    Symfony\Component\DependencyInjection\Kernel\KernelTrait; class Kernel extends AbstractKernel { use KernelTrait; }
  21. 8.1: ConsoleBundle // config/bundles.php return [ Symfony\Component\Console\ConsoleBundle::class => ['all' =>

    true], ]; ServicesBundle ConsoleBundle #[RequiredBundle] commands, lazy loading, autowiring, argument resolvers autoconfiguration Nicolas Grekas
  22. 8.1: Application with a container // bin/console require dirname(__DIR__).'/vendor/autoload_runtime.php'; return

    function (array $context) { $kernel = new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']); $kernel->boot(); return new Application('agenda', '1.0', $kernel->getContainer()); }; Nicolas Grekas
  23. From boot to __invoke() Runtime · Kernel::boot() Application::run() ContainerCommandLoader ·

    talk:move InvokableCommand::__invoke() ArgumentResolver::getArguments() TalkValueResolver::resolve() MoveTalkCommand::__invoke()
  24. Locking final class ScheduleCommands { use LockableTrait; #[AsCommand('publish')] public function

    publish(SymfonyStyle $io): int { if (!$this->lock()) { $io->warning('Already publishing.'); return Command::FAILURE; } // … } }
  25. 8.2: LockBundle // config/bundles.php return [ Symfony\Component\Console\ConsoleBundle::class => ['all' =>

    true], Symfony\Component\Lock\LockBundle::class => ['all' => true], ]; // config/packages/lock.php return App::config([ 'lock' => ['resources' => ['console' => 'flock']], ]);
  26. 8.2: LockBundle $ bin/console schedule publish $ bin/console schedule publish

    Publishing… [WARNING] Already publishing. [OK] Schedule published.
  27. 8.2: ValidationBundle // config/bundles.php return [ Symfony\Component\Console\ConsoleBundle::class => ['all' =>

    true], Symfony\Component\Lock\LockBundle::class => ['all' => true], Symfony\Component\Validator\ValidationBundle::class => ['all' => true], ];
  28. 8.2: Command groups #[AsCommand('schedule', options: […])] final class ScheduleCommands {

    #[AsCommand('list')] public function list(…): int schedule #[AsCommand('publish')] public function publish(…): int #[AsCommand('import')] public function import(…): int list publish import } Yonel Ceruto Nicolas Grekas
  29. 8.2: Lists of enums + public function list(SymfonyStyle $io, CommandChain

    $chain, #[Option] ?Room $room = null): int public function list(SymfonyStyle $io, CommandChain $chain, #[Option] Room ...$room): int $ bin/console schedule list --room=symfony --room=smile-upsun 09:15 symfony Opening keynote 10:10 symfony Web-less Console 17:00 smile-upsun Lightning talks Zoran Shefot Bogoevski Nicolas Grekas
  30. 8.2: Possible values in help #[Option] Room ...$room --room=ROOM Room::cases()

    [possible values: symfony, sensiolabs, smile-upsun] Room ...$room (multiple values allowed) $ bin/console schedule list --help Options: --room=ROOM [possible values: symfony, sensiolabs, smile-upsun] (multiple values allowed) Nicolas Grekas
  31. 8.2: Possible values in JSON $ bin/console list --format=json #[Option]

    Room ...$room "schedule:list" … "--room": { "accept_value": true, "is_value_required": true, "is_multiple": true, "is_deprecated": false, "description": "[possible values: symfony, sensiolabs, smile-upsun]" }
  32. 8.2: Deprecated options + + #[Option, MapDateTime(format: 'H:i')] ?\DateTimeImmutable $at

    = null, #[Option(deprecated: true), MapDateTime(format: 'H:i')] ?\DateTimeImmutable $at = null, #[Option, MapDateTime(format: 'H:i')] ?\DateTimeImmutable $start = null, $ bin/console talk:move web-less-console sensiolabs --at=14:00 The option "--at" is deprecated. [OK] Web-less Console → sensiolabs, 14:00 Florian Kasper
  33. 8.2: Several files #[AsCommand('import')] public function import( SymfonyStyle $io, #[Argument,

    Ask('Drop the files to import:')] InputFile ...$files, ): int $ bin/console schedule import Drop the files to import: // Hit Enter to finish 📎 composer.json (application/json, 488.0 B) 📎 README.md (text/plain, 907.0 B) Imported composer.json Imported README.md Yonel Ceruto Robin Chalas
  34. The whole app src/Command/MoveTalkCommand.php src/Kernel.php class Kernel extends AbstractKernel {

    use KernelTrait; } config/bundles.php return [ ConsoleBundle::class => ['all' => true], LockBundle::class => ['all' => true], ValidationBundle::class => ['all' => true], ]; #[AsCommand('talk:move')] final class MoveTalkCommand { public function __invoke( SymfonyStyle $io, #[Argument] Talk $talk, #[Argument] Room $room, #[Option, MapDateTime(format: 'H:i')] ?\DateTimeImmutable $at = null, ): int { $talk->moveTo($room, $at ??= $talk->startsAt); $io->success(…); return Command::SUCCESS; } }
  35. “Writing code once was never the big deal with software

    and it still isn’t.” Daniel Stenberg · Actual curl rewrites in rust
  36. “Writing code once was never the big deal with software

    and it still isn’t.” Daniel Stenberg · Actual curl rewrites in rust “A dependency is more than code.” Sebastian Bergmann · Cheap to write, expensive to depend on
  37. Thank you Yonel Ceruto Nicolas Grekas Jérôme Tamarelle Dobroslav Tokoš

    Daniel Leech Zoran Shefot Bogoevski Florian Kasper Jesper Noordsij Théo Fidry Robin Chalas

More Decks by Robin Chalas

Other Decks in Programming