1 / 29

Ibiza, June 4 th – 7 th 2011

Ibiza, June 4 th – 7 th 2011 . Magento Developers Paradise. Developing Loosely Coupled Modules with Magento. Sergey Shymko Magento 2 Team. Introduction and Terminology. Coupling or dependency is the degree to which each program module relies on each one of the other modules

idalia
Download Presentation

Ibiza, June 4 th – 7 th 2011

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ibiza, June 4th – 7th2011

  2. Magento Developers Paradise Developing Loosely Coupled Modules with Magento Sergey Shymko Magento 2 Team

  3. Introduction and Terminology • Coupling or dependency is the degree to which each program module relies on each one of the other modules • Coupling measures the likelihood of a change or fault in one module affecting another module

  4. Module Purpose/Behavior • Module introduces new feature • Module injects into existing functionality • Module integrates with existing feature • Module extends/overlaps existing functionality

  5. Magento Developers Paradise Modules Coupling WhileInjecting Into the Functionality

  6. General Task of Injecting into Functionality • Module A exists and already implements the functionality • Module B expands the Module A • Module B should be triggered at the specific point of the Module A execution

  7. Hard-Coded Call Implementation Framework • Module A highly relies on Module B • Module Bcan’t be removed Module A Module B Call

  8. Conditional Call Implementation Framework • Module existence checking • Module B nowcan be removed • Module A still knows about Module B • Rename Module B – change Module A • Add new module – change Module A Module A Module B Call if (exists(‘Module B’)) { call(‘Module B’); } Module C

  9. Maintaining the List of Dependents Framework • It’s observer pattern • Module Brelies on Module A • New modules can register within the Module A without changing anything • Rename Module A – change dependent modules Module A Module B Register Notify Module C Notify Register

  10. Publish/Subscribe Messaging Pattern • Pub/sub – universal decoupling solution • senders of messages do not program the messages to be sent directly to specific receivers • subscribers express interest in messages without knowledge of publishers

  11. Low Modules Coupling Using Pub/Sub Pattern Framework Messages Manager • Framework should take care of coupling • Modules don’t know about each other • Any module can be removed • New modules can subscribe to existing messages without changing anything Subscribe Subscribe Publish Module A Module B Call Module C Call

  12. Magento Events • Magento events – object-oriented implementation of the pub/sub pattern • Event dispatching calls – points of integration • Subscription through config.xml

  13. Low Modules Coupling with Magento Events Framework Config Manager Merged config.xml Events Manager Module A Module B Subscribe config.xml config.xml Call Mage::dispatchEvent

  14. Achieved Coupling with Magento Events • Coupling depends on the implementation of the particular event handler

  15. Possible Improvements in Magento Events • Strict event data types • Convention over configuration • Events naming convention

  16. Strict Event Data Types • Own event class for each unique parameters combination • Formal data structure declaration • Access restriction to event data • Events documentation auto-generation

  17. Convention over Configuration • Also known as coding by convention • Decreasing the number of decisions that developers need to make • Simplicity while keeping flexibility • Providing good defaults

  18. Events Subscription in Configuration <events> <catalog_product_load_after> <observers> <inventory> <class>cataloginventory/observer</class> <method>addInventoryData</method> </inventory> </observers> </catalog_product_load_after> • Mapping events to handling routines • Declaration of handlersfor each area classMage_CatalogInventory_Model_Observer { public function addInventoryData($observer) { // ... }

  19. No Events Subscription in Configuration <config> <global> <events/> </global> <frontend> <events/> </frontend> <adminhtml> <events/> </adminhtml> </config> • Good default mapping • Single observer class per module • Method name equals event name classMage_CatalogInventory_Model_Observer { public function catalog_product_load_after($observer) { // ... }

  20. Event Naming Convention • lowerCamelCase event name to correspond to the method name

  21. Magento Developers Paradise Modules Coupling While Integrating with Existing Feature

  22. Sample Task – Cron Feature • Module Mage_Cron introduces time-based jobs scheduling feature • Any module should be able to schedule its jobs

  23. High Modules Coupling Framework Mage_Cron My_Module Schedule Job Get Schedule & Run Job Removing Mage_Cron breaks My_Module

  24. Magento Configuration Files • Configuration files “merging” from all enabled modules • Configuration allows integration with: • system-wide feature • feature provided by the module

  25. Low Modules Coupling Using Configuration Framework Config Manager Merged config.xml Mage_Cron My_Module Get Schedule Schedule Job config.xml config.xml Run Job Removing Mage_Cron doesn’t break anything

  26. Configuration Usage in Magento • Cache management • Index management • Product types • Translation files • Layout files • …

  27. Magento Developers Paradise Summary

  28. Recommendations • Follow the low coupling principles – use events to communicate between modules • Extend only when no events are triggered • Report requests for adding new events • Don’t hesitate to trigger events in your third-party modules and extensions

  29. Magento Developers Paradise Thank You! Questions & Answers Sergey Shymko sergey.shymko@magento.com

More Related