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

43f3e06bd5b99f880eb350b2aece74bf?s=47 Deve L. Oper
November 12, 2019

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

Talk was given at the OXID eSales Partner's Day 2019 in Freiburg. Topic is how to make use of the events introduced in OXID eShop 6.2 for shop modules.

43f3e06bd5b99f880eb350b2aece74bf?s=128

Deve L. Oper

November 12, 2019
Tweet

Transcript

  1. 3.

    ©2019 OXID eSales AG - vertraulich CLASS INHERITANCE CHAIN for

    OXID eShop 6 extends from extends from extends from
  2. 4.

    ©2019 OXID eSales AG - vertraulich OXNEW BUILDS IT Inheritance

    chain with (any style) modules extends from extends from
  3. 5.

    ©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
  4. 7.

    ©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
  5. 8.

    ©2019 OXID eSales AG - vertraulich WE HAD REACHED THE

    END OF THE CLASS CHAIN oxNew just won‘t do
  6. 9.

    ©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
  7. 10.

    ©2019 OXID eSales AG - vertraulich WORKS WITH MANY MODULES

    Propagate events shop shop event module B module A event event event ...
  8. 11.

    ©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
  9. 12.

    ©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
  10. 13.

    ©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
  11. 15.

    ©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..
  12. 16.

    ©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.
  13. 17.

    ©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
  14. 18.

    ©2019 OXID eSales AG - vertraulich EXAMPLE – STEP 3

    Register the event subscriber in the DI container > Add services.yaml file to module root path
  15. 20.

    ©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.
  16. 21.

    ©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