Click to edit Master title style – 7 Ibiza, June 4 2011

Click to edit Master title style
Ibiza, June 4th – 7th 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
• Coupling measures the likelihood of a
change or fault in one module affecting
another module
Module Purpose/Behavior
•
•
•
•
Module introduces new feature
Module injects into existing functionality
Module integrates with existing feature
Module extends/overlaps existing
functionality
Magento Developers Paradise
Modules Coupling While
Injecting Into the Functionality
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
Hard-Coded Call Implementation
Framework
•
•
Module A
Module B
Call
Module A highly relies
on Module B
Module B
can’t be removed
Conditional Call Implementation
Framework
•
•
Module A
Module B
Call
if (exists(‘Module B’)) {
call(‘Module B’);
}
•
•
Module C
•
Module existence
checking
Module B now
can be removed
Module A still knows
about Module B
Rename Module B –
change Module A
Add new module –
change Module A
Maintaining the List of Dependents
Framework
•
•
•
Module A
Register
Module B
•
Notify
Module C
Notify
Register
It’s observer pattern
Module B
relies on Module A
New modules can
register within the
Module A without
changing anything
Rename Module A –
change dependent
modules
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
Low Modules Coupling Using Pub/Sub Pattern
Module B
Subscribe
Subscribe
Module A
•
Messages Manager
Publish
Framework
•
•
•
Call
Module C
Call
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
Magento Events
• Magento events – object-oriented
implementation of the pub/sub pattern
• Event dispatching calls – points of
integration
• Subscription through config.xml
Low Modules Coupling with Magento Events
Framework
Merged config.xml
Config Manager
Module A
Module B
config.xml
config.xml
Call
Mage::dispatchEvent
Subscribe
Events Manager
Achieved Coupling with Magento Events
Coupling depends on the implementation of the particular event handler
Coupling Type
Description
Message coupling (low)
Event doesn’t pass any data
Data coupling
Simple event parameters, each is used
Stamp coupling
Event data structure contains fields which may or
may not be used
Common coupling
Usage of the global data registry; Accessing data
not only through methods
Content coupling (high)
Not applicable to the pub/sub
Possible Improvements in Magento Events
• Strict event data types
• Convention over configuration
• Events naming convention
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
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
Events Subscription in Configuration
<events>
<catalog_product_load_after>
<observers>
<inventory>
<class>cataloginventory/observer</class>
<method>addInventoryData</method>
</inventory>
</observers>
</catalog_product_load_after>
class Mage_CatalogInventory_Model_Observer
{
public function addInventoryData($observer)
{
// ...
}
•
•
Mapping events to
handling routines
Declaration of handlers
for each area
No Events Subscription in Configuration
<config>
<global>
<events/>
</global>
<frontend>
<events/>
</frontend>
<adminhtml>
<events/>
</adminhtml>
</config>
class Mage_CatalogInventory_Model_Observer
{
public function catalog_product_load_after($observer)
{
// ...
}
•
•
•
Good default mapping
Single observer class
per module
Method name equals
event name
Event Naming Convention
lowerCamelCase event name to correspond to the method name
Event Name Part
Description
1. Module
Name of the module, which introduces an event.
2. Layer
Application layer where an event appears.
Allowed values: model, view, controller, service.
3. Entity
Entity (domain) to which event has relation.
4. Action
By default action should be equal to the method name,
which triggers an event.
[5. Suffix]
Optional. Allowed values: before, after
Magento Developers Paradise
Modules Coupling While
Integrating with Existing Feature
Sample Task – Cron Feature
• Module Mage_Cron introduces time-based
jobs scheduling feature
• Any module should be able to schedule its
jobs
High Modules Coupling
Framework
Mage_Cron
Schedule Job
My_Module
Get Schedule & Run Job
Removing Mage_Cron breaks My_Module
Magento Configuration Files
• Configuration files “merging” from all
enabled modules
• Configuration allows integration with:
– system-wide feature
– feature provided by the module
Low Modules Coupling Using Configuration
Mage_Cron
config.xml
Get Schedule
Merged config.xml
Config Manager
My_Module
config.xml
Run Job
Removing Mage_Cron doesn’t break anything
Schedule Job
Framework
Configuration Usage in Magento
•
•
•
•
•
•
Cache management
Index management
Product types
Translation files
Layout files
…
Magento Developers Paradise
Summary
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
Magento Developers Paradise
Thank You!
Questions & Answers
Sergey Shymko
[email protected]