Киев             15 сентября 2012Magento DI
Software EntropyPull-approach and its problemsPush-approachMagento Object Manager
Software entropy
Software entropy cycle
Pull-approach
Mage::getModel(“Mage_Cms_Model_Page”)Mage::getSingleton(“Mage_Cms_Model_Page”)
class Mage_Cms_Model_Page{   public function __construct()   {     $this->_idFieldName = Mage::getResourceSingleton($this-...
class Mage_Cms_Model_PageTest extends PHPUnit_Framework_TestCase{   public function setUp()   {     $this->_page = new Mag...
class Mage_Cms_Model_PageTest extends PHPUnit_Framework_TestCase{   public function setUp()   {        Mage::setResourceSi...
Mage::getXXX() ===
Push-approach
class Mage_Core_Model_Abstract{   public function __construct(array $data = array())   {     parent::__construct($data);  ...
class Mage_Core_Model_Abstract{   public function __construct(     Mage_Core_Model_Event_Manager $eventDispatcher,     Mag...
Mage::getXXX()Mage_Some_Class::__construct()   Magento_ObjectManager
Magento Object Manager
interface Magento_ObjectManager{    public function get($className, $arguments);    public function create($className, $ar...
ZendDi
class Mage_Core_Model_Abstract{   public function __construct(     Mage_Core_Model_Event_Manager $eventDispatcher,     Mag...
<config>  <global>    <di>       <Mage_Core_Model_Cache>          <parameters>            <cacheDir>/path/to/cache/dir</ca...
<di>  <Magento_Data_Structure>     <shared>0</shared>  </Magento_Data_Structure></di>
public function __construct(Some_Interface $processor)
<adminhtml>  <di>     <preferences>       <Some_Interface>Some_Backend_Implementation</Some_Interface>     </preferences> ...
INJECTABLES               -   NON-INJECTABLES Varien_Db_Adapter                               Mage_Catalog_Model_Product M...
class Varien_Data_Collection{   //...    public function getNewEmptyItem()   {       return Mage::getModel($this->_itemObj...
class Varien_Data_Collection{   public function __construct(Magento_ObjectFactory $factory)   {     $this->_itemFactory = ...
class Mage_Catalog_Model_Product_Factory implements Magento_ObjectManager_Factory{   protected $_objectManager;    public ...
class Mage_Review_Model_Observer{   public function processDeletedProduct (Varien_Event_Observer $observer)   {     $produ...
class Mage_Review_Model_Observer{   public function __construct(Mage_Review_Model_Resource_Review $review)   {     $this->...
<di>  <Mage_Review_Model_Observer>     <parameters>       <review>          Mage_Review_Model_Resource_Review_Proxy       ...
class Mage_Review_Model_Resource_Review_Proxy   extends Mage_Review_Model_Resource_Review{   public function __construct(M...
Magento Dependency Injection
Upcoming SlideShare
Loading in …5
×

Magento Dependency Injection

922 views
824 views

Published on

Published in: Design
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
922
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
12
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Magento Dependency Injection

  1. 1. Киев 15 сентября 2012Magento DI
  2. 2. Software EntropyPull-approach and its problemsPush-approachMagento Object Manager
  3. 3. Software entropy
  4. 4. Software entropy cycle
  5. 5. Pull-approach
  6. 6. Mage::getModel(“Mage_Cms_Model_Page”)Mage::getSingleton(“Mage_Cms_Model_Page”)
  7. 7. class Mage_Cms_Model_Page{ public function __construct() { $this->_idFieldName = Mage::getResourceSingleton($this->_resourceName); } public function getAvailableStatuses() { $statuses = new Varien_Object(array( self::STATUS_ENABLED => Mage::helper(Mage_Cms_Helper_Data)->__(Enabled) self::STATUS_DISABLED => Mage::helper(Mage_Cms_Helper_Data) ->__(„Disabled) )); Mage::dispatchEvent(cms_page_get_available_statuses, array(statuses => $statuses)); }}
  8. 8. class Mage_Cms_Model_PageTest extends PHPUnit_Framework_TestCase{ public function setUp() { $this->_page = new Mage_Cms_Model_Page(); }}
  9. 9. class Mage_Cms_Model_PageTest extends PHPUnit_Framework_TestCase{ public function setUp() { Mage::setResourceSingleton(„Page_Resource‟, $this->getMock(„Page_Resource‟)); $this->_page = new Mage_Cms_Model_Page(); } public function testProcessDoesSomething() { Mage::setHelper(„Mage_Cms_Helper_Data‟, $this->getMock(„Mage_Cms_Helper_Data‟)); $this->assertSomething($this->_page->process()); }}
  10. 10. Mage::getXXX() ===
  11. 11. Push-approach
  12. 12. class Mage_Core_Model_Abstract{ public function __construct(array $data = array()) { parent::__construct($data); $this->_construct(); }}
  13. 13. class Mage_Core_Model_Abstract{ public function __construct( Mage_Core_Model_Event_Manager $eventDispatcher, Mage_Core_Model_Cache $cacheManager, Mage_Core_Model_Resource_Abstract $resource = null, Varien_Data_Collection_Db $resourceCollection = null, array $data = array() ){ $this->_eventDispatcher = $eventDispatcher; $this->_cacheManager = $cacheManager; $this->_resource = $resource; $this->_resourceCollection = $resourceCollection; parent::__construct($data); $this->__construct(); }}
  14. 14. Mage::getXXX()Mage_Some_Class::__construct() Magento_ObjectManager
  15. 15. Magento Object Manager
  16. 16. interface Magento_ObjectManager{ public function get($className, $arguments); public function create($className, $arguments);}
  17. 17. ZendDi
  18. 18. class Mage_Core_Model_Abstract{ public function __construct( Mage_Core_Model_Event_Manager $eventDispatcher, Mage_Core_Model_Cache $cacheManager, Mage_Core_Model_Resource_Abstract $resource = null, Varien_Data_Collection_Db $resourceCollection = null, array $data = array() ){ $this->_eventDispatcher = $eventDispatcher; $this->_cacheManager = $cacheManager; $this->_resource = $resource; $this->_resourceCollection = $resourceCollection; parent::__construct($data); $this->__construct(); }}
  19. 19. <config> <global> <di> <Mage_Core_Model_Cache> <parameters> <cacheDir>/path/to/cache/dir</cacheDir> </parameters> </Mage_Core_Model_Cache> </di> </global></config>
  20. 20. <di> <Magento_Data_Structure> <shared>0</shared> </Magento_Data_Structure></di>
  21. 21. public function __construct(Some_Interface $processor)
  22. 22. <adminhtml> <di> <preferences> <Some_Interface>Some_Backend_Implementation</Some_Interface> </preferences> </di></adminhtml><api> <di> <preferences> <Some_Interface>Some_Api_Implementation</Some_Interface> </preferences> </di></api>
  23. 23. INJECTABLES - NON-INJECTABLES Varien_Db_Adapter Mage_Catalog_Model_Product Mage_Core_Model_Cache Mage_Wishlist_Model_Wishlist Mage_Core_Model_Config
  24. 24. class Varien_Data_Collection{ //... public function getNewEmptyItem() { return Mage::getModel($this->_itemObjectClass); } //...}
  25. 25. class Varien_Data_Collection{ public function __construct(Magento_ObjectFactory $factory) { $this->_itemFactory = $factory; } //... public function getNewEmptyItem() { return $this->_itemFactory->create(); } //...}
  26. 26. class Mage_Catalog_Model_Product_Factory implements Magento_ObjectManager_Factory{ protected $_objectManager; public function __construct(Magento_ObjectManager $objectManager) { $this->_objectManager = $objectManager; } public function createFromArray(array $arguments = array()) { return $this->_objectManager->create(Mage_Catalog_Model_Product, $arguments); }}
  27. 27. class Mage_Review_Model_Observer{ public function processDeletedProduct (Varien_Event_Observer $observer) { $productId = $observer->getEvent()->getProduct()->getId(); if ($productId) { Mage::getResourceSingleton(Mage_Review_Model_Resource_Review) ->deleteReviewsByProductId($productId); } }}
  28. 28. class Mage_Review_Model_Observer{ public function __construct(Mage_Review_Model_Resource_Review $review) { $this->_reviewResource = $reviewResource; } //...}
  29. 29. <di> <Mage_Review_Model_Observer> <parameters> <review> Mage_Review_Model_Resource_Review_Proxy </review> </parameters> </ Mage_Review_Model_Observer ></di>
  30. 30. class Mage_Review_Model_Resource_Review_Proxy extends Mage_Review_Model_Resource_Review{ public function __construct(Magento_ObjectManager $objectManager) { $this->_objectManager = $objectManager; } public function deleteReviewByProductId($productId) { return $this->_objectManager ->get(Mage_Review_Model_Resource_Review ) ->deleteReviewByProductId(); }}

×