Chapter 9 of the Symfony for PHP developers track

The tavern's inner workings

6 exercises · 1,700 XP · about 3 h 30 min

This chapter works on: Kernel eventskernel.request#[AsEventListener]RequestEventClockInterfaceDomain eventEventDispatcherInterfacedispatchListener priorityDoctrine listeners#[AsEntityListener]prePersistSluggerInterfaceService decoration#[AsDecorator]#[AutowireDecorated]CacheCacheInterfaceEnvironment variables#[Autowire(env:)]int: and csv: processors.env.env.testDebuggingEvents

The chapter's exercises

  1. Closed on Mondays

    Exercise 1 · 270 XP · 5 objectives checked by tests

    On Mondays, Bruna goes to the market and Gorm counts the barrels: the tavern is closed. Until now, Pippin hung a sign on the door, but the website keeps taking reservations. We need a rule that applies to every dining room page, without touching each controller.

    Kernel eventskernel.request#[AsEventListener]RequestEventClockInterface

  2. The tavern bell

    Exercise 2 · 270 XP · 5 objectives checked by tests

    When Bruna invents a dish, the whole tavern must know. Pippin has already written the code to invite the guilds to taste it (InviterLesGuildes), and Gorm insists on being told first. But the kitchen controller tells nobody.

    Domain eventEventDispatcherInterfacedispatchListener priority

  3. The slug that writes itself

    Exercise 3 · 270 XP · 5 objectives checked by tests

    A dish's slug (dwarf-stew, in /dish/dwarf-stew) used to be computed in the kitchen controller. But dishes also arrive through the console commands, the Friday feast, the tests… and every time, someone has to remember the slug. Pippin forgot once: dish saved, SQL error, evening ruined.

    Doctrine listeners#[AsEntityListener]prePersistSluggerInterface

  4. The wind oracle

    Exercise 4 · 280 XP · 5 objectives checked by tests

    The aviary now shows today's winds toward three lands, read by the tower mage. His forecasts are accurate, but he takes his time reading the stars and he bills every consultation. Every visit to the aviary means three consultations. Open the preview and reload the page: watch the bill climb.

    Service decoration#[AsDecorator]#[AutowireDecorated]CacheCacheInterface

  5. The tavern's settings

    Exercise 5 · 270 XP · 5 objectives checked by tests

    Gorm is off traveling for a month: Bruna takes over the tavern. She would rather close on Wednesdays, and her ravens fly to other lands. But everything is hard-coded: '1' for Monday, 'Gorm' for the innkeeper, three lands in the aviary controller. To change a setting, you would have to change the code and deploy again.

    Environment variables#[Autowire(env:)]int: and csv: processors.env.env.test

  6. 🐉 Boss: the tavern turned upside down

    Exercise 6 · 340 XP · 5 objectives checked by tests

    Pippin spent the week “making the tavern more robust”. He read half of the Symfony documentation and applied bits of it everywhere. The result:

    DebuggingEventsDoctrine listenersService decorationCache