Slide 1

Slide 1 text

EVENTS IN DER PRAXIS how to make use of events in OXID eShop

Slide 2

Slide 2 text

©2019 OXID eSales AG - vertraulich THE GOOD OLD OXNEW How modules use(d) to work

Slide 3

Slide 3 text

©2019 OXID eSales AG - vertraulich CLASS INHERITANCE CHAIN for OXID eShop 6 extends from extends from extends from

Slide 4

Slide 4 text

©2019 OXID eSales AG - vertraulich OXNEW BUILDS IT Inheritance chain with (any style) modules extends from extends from

Slide 5

Slide 5 text

©2019 OXID eSales AG - vertraulich > we can extend (almost) everything > we can break (almost) everything > sometimes we only notice it is broken when other modules are around > some classes we cannot extend (non-leaf classes like the BaseModel for example) IT WORKS but there‘s room for improvement

Slide 6

Slide 6 text

©2019 OXID eSales AG - vertraulich OXID ESHOP 6 Introducing events

Slide 7

Slide 7 text

©2019 OXID eSales AG - vertraulich > Extract the reverse proxy (Varnish) from EE shop code into module > Write a module using NGINX as reverse proxy > No BC breaks please as it should to go minor AS PO I WANT TO ... So let‘s just move the reverse proxy into a module

Slide 8

Slide 8 text

©2019 OXID eSales AG - vertraulich WE HAD REACHED THE END OF THE CLASS CHAIN oxNew just won‘t do

Slide 9

Slide 9 text

©2019 OXID eSales AG - vertraulich The purpose of the `Internal` namespace is to have a clearly defined public API. One of the means to achieve this is the usage of the Symfony DI container to manage services in the `Internal` namespace. The implementations themselves are shielded by interfaces, so the interfaces in the Internal namespace might be considered as part of the public API of the OXID eShop. IT‘S ABOUT TIME ANYWAY Start building a clearly defined public API shop shop module event event

Slide 10

Slide 10 text

©2019 OXID eSales AG - vertraulich WORKS WITH MANY MODULES Propagate events shop shop event module B module A event event event ...

Slide 11

Slide 11 text

©2019 OXID eSales AG - vertraulich SHOPAWARE AbstractShopAwareEventSubscriber does the trick shop 1 – module A event module A event shop 1 – module A module B shop 2– module B event event shop 2 – module B

Slide 12

Slide 12 text

©2019 OXID eSales AG - vertraulich > Much more secure way to enhance shop functionality than the traditional way of enhancing classes. > Register an event subscriber that may react on the event instead of getting inside the class chain. > The events should stay the same over a much longer period of time than code. > Make modules future proof, use events whenever possible. > The framework used for this is the Symfony event handling mechanism. EVENTS The Pros

Slide 13

Slide 13 text

©2019 OXID eSales AG - vertraulich AVAILABLE EVENTS OXID eShop 6.2 Module Events ✓ BeforeModuleDeactivationEvent ✓ FinalizingModuleActivationEvent ✓ FinalizingModuleDeactivationEvent ✓ SettingChangedEvent ✓ ShopConfigurationChangedEvent DatabaseEvents ✓ AfterModelDeleteEvent ✓ AfterModelInsertEvent ✓ AfterModelUpdateEvent ✓ BeforeModelDeleteEvent ✓ BeforeModelUpdateEvent DI Container Events ✓ ProjectYamlChangedEvent ✓ ServicesYamlConfigurationErrorEvent ShopGeneralEvents ✓ AfterRequestProcessedEvent ✓ AllCookiesRemovedEvent ✓ ApplicationExitEvent ✓ BasketChangedEvent ✓ BeforeHeadersSendEvent ✓ BeforeSessionStartEvent View Events ✓ ThemeSettingChangedEvent ✓ ViewRenderedEvent

Slide 14

Slide 14 text

©2019 OXID eSales AG - vertraulich LET‘S LOOK AT SOME CODE Modules and events

Slide 15

Slide 15 text

©2019 OXID eSales AG - vertraulich 1. Choose the event(s) to subscribe to 2. Write the event subscriber 3. Register the event subscriber in the DI container SUBSCRIBE MODULE TO EVENTS Three steps, that‘s all 1..2..3..

Slide 16

Slide 16 text

©2019 OXID eSales AG - vertraulich EXAMPLE – STEP 1 Choose the event(s) to subscribe to > I want to log every time some shop model did change (insert, update, delete). > Subscribe to following DatabaseEvents: ▪ AfterModelDeleteEvent ▪ AfterModelInsertEvent ▪ AfterModelUpdateEvent NOTE: The oxNew way would require us to chain extend each shop model class because we cannot extend the BaseModel in a module.

Slide 17

Slide 17 text

©2019 OXID eSales AG - vertraulich Subscriber class: > Extend from AbstractShopAwareEventSubscriber > Constructor ▪ Inject psr logger (LoggerInterface) > handler method ▪ Take event, handle it, pass on event > static method ▪ getSubscribedEvents EXAMPLE – STEP 2 Write the event subscriber

Slide 18

Slide 18 text

©2019 OXID eSales AG - vertraulich EXAMPLE – STEP 3 Register the event subscriber in the DI container > Add services.yaml file to module root path

Slide 19

Slide 19 text

©2019 OXID eSales AG - vertraulich HERE WE GO Believe me, it works shopaware

Slide 20

Slide 20 text

©2019 OXID eSales AG - vertraulich NGINX AS REVERSE PROXY MODULE Sneak Peek Two parts: > Decide which output to cache or not to cache ▪ Subscribe to ShopGeneralEvents and ViewRenderedEvent. ▪ At the right moment manipulate headers to signal NGINX how to handle response. > (Partially) invalidate the cache on changes ▪ Subscribe to DatabaseEvents and some of the ModuleEvents. ▪ Check the event’s payload and decide if cache needs to be invalidated.

Slide 21

Slide 21 text

©2019 OXID eSales AG - vertraulich You will find information about available events and the example how to use them in the OXID eShop Developer Documentation for OXD eShop 6.2 in section Module resources/Events. GETTING HELP Available Documentation

Slide 22

Slide 22 text

©2019 OXID eSales AG - vertraulich Smart E-Commerce Solutions oxid-esales.com