SlideShare ist ein Scribd-Unternehmen logo
1 von 31
Киев
             15 сентября 2012




Magento DI
Software Entropy

Pull-approach and its problems

Push-approach

Magento 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->_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));
    }
}
class Mage_Cms_Model_PageTest extends PHPUnit_Framework_TestCase
{
   public function setUp()
   {
     $this->_page = new Mage_Cms_Model_Page();
   }
}
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());
    }
}
Mage::getXXX() ===
Push-approach
class Mage_Core_Model_Abstract
{
   public function __construct(array $data = array())
   {
     parent::__construct($data);
     $this->_construct();
   }
}
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();
    }
}
Mage::getXXX()


Mage_Some_Class::__construct()
   Magento_ObjectManager
Magento Object Manager
interface Magento_ObjectManager
{
    public function get($className, $arguments);

    public function create($className, $arguments);
}
ZendDi
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();
    }
}
<config>
  <global>
    <di>
       <Mage_Core_Model_Cache>
          <parameters>
            <cacheDir>/path/to/cache/dir</cacheDir>
          </parameters>
       </Mage_Core_Model_Cache>
    </di>
  </global>
</config>
<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>
  </di>
</adminhtml>
<api>
  <di>
     <preferences>
       <Some_Interface>Some_Api_Implementation</Some_Interface>
     </preferences>
  </di>
</api>
INJECTABLES               -   NON-INJECTABLES
 Varien_Db_Adapter
                               Mage_Catalog_Model_Product
 Mage_Core_Model_Cache
                               Mage_Wishlist_Model_Wishlist
 Mage_Core_Model_Config
class Varien_Data_Collection
{
   //...
    public function getNewEmptyItem()
   {
       return Mage::getModel($this->_itemObjectClass);
   }
    //...
}
class Varien_Data_Collection
{
   public function __construct(Magento_ObjectFactory $factory)
   {
     $this->_itemFactory = $factory;
   }

    //...
    public function getNewEmptyItem()
    {
        return $this->_itemFactory->create();
    }
    //...
}
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);
    }
}
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);
        }
    }
}
class Mage_Review_Model_Observer
{
   public function __construct(Mage_Review_Model_Resource_Review $review)
   {
     $this->_reviewResource = $reviewResource;
   }

    //...
}
<di>
  <Mage_Review_Model_Observer>
     <parameters>
       <review>
          Mage_Review_Model_Resource_Review_Proxy
       </review>
     </parameters>
  </ Mage_Review_Model_Observer >
</di>
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();
    }
}

Weitere ähnliche Inhalte

Was ist angesagt?

Aug 3-2012 - StiltSoft - 10 features for JIRA admin
Aug 3-2012 - StiltSoft - 10 features for JIRA adminAug 3-2012 - StiltSoft - 10 features for JIRA admin
Aug 3-2012 - StiltSoft - 10 features for JIRA admin
Teamlead
 
Special Events: Beyond Custom Events
Special Events: Beyond Custom EventsSpecial Events: Beyond Custom Events
Special Events: Beyond Custom Events
Brandon Aaron
 
jQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a TreejQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a Tree
adamlogic
 
Introducing jQuery
Introducing jQueryIntroducing jQuery
Introducing jQuery
Wildan Maulana
 

Was ist angesagt? (20)

Building complex User Interfaces with Sitecore and React
Building complex User Interfaces with Sitecore and ReactBuilding complex User Interfaces with Sitecore and React
Building complex User Interfaces with Sitecore and React
 
WooCommerce CRUD and Data Store by Akeda Bagus
WooCommerce CRUD and Data Store by Akeda BagusWooCommerce CRUD and Data Store by Akeda Bagus
WooCommerce CRUD and Data Store by Akeda Bagus
 
AngularJS Services
AngularJS ServicesAngularJS Services
AngularJS Services
 
Aug 3-2012 - StiltSoft - 10 features for JIRA admin
Aug 3-2012 - StiltSoft - 10 features for JIRA adminAug 3-2012 - StiltSoft - 10 features for JIRA admin
Aug 3-2012 - StiltSoft - 10 features for JIRA admin
 
AngularJS Routing
AngularJS RoutingAngularJS Routing
AngularJS Routing
 
WordPress plugin #3
WordPress plugin #3WordPress plugin #3
WordPress plugin #3
 
Mysocial databasequeries
Mysocial databasequeriesMysocial databasequeries
Mysocial databasequeries
 
Mysocial databasequeries
Mysocial databasequeriesMysocial databasequeries
Mysocial databasequeries
 
Special Events: Beyond Custom Events
Special Events: Beyond Custom EventsSpecial Events: Beyond Custom Events
Special Events: Beyond Custom Events
 
Special Events
Special EventsSpecial Events
Special Events
 
jQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a TreejQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a Tree
 
WordPress Capabilities Magic
WordPress Capabilities MagicWordPress Capabilities Magic
WordPress Capabilities Magic
 
Get AngularJS Started!
Get AngularJS Started!Get AngularJS Started!
Get AngularJS Started!
 
AngulrJS Overview
AngulrJS OverviewAngulrJS Overview
AngulrJS Overview
 
Optimizing Angular Performance in Enterprise Single Page Apps
Optimizing Angular Performance in Enterprise Single Page AppsOptimizing Angular Performance in Enterprise Single Page Apps
Optimizing Angular Performance in Enterprise Single Page Apps
 
Introducing jQuery
Introducing jQueryIntroducing jQuery
Introducing jQuery
 
Borrados
BorradosBorrados
Borrados
 
AngularJS - $http & $resource Services
AngularJS - $http & $resource ServicesAngularJS - $http & $resource Services
AngularJS - $http & $resource Services
 
Magento2&amp;java script (2)
Magento2&amp;java script (2)Magento2&amp;java script (2)
Magento2&amp;java script (2)
 
Php update and delet operation
Php update and delet operationPhp update and delet operation
Php update and delet operation
 

Ähnlich wie Magento Dependency Injection

ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
ZFConf Conference
 
Meet Magento Belarus debug Pavel Novitsky (eng)
Meet Magento Belarus debug Pavel Novitsky (eng)Meet Magento Belarus debug Pavel Novitsky (eng)
Meet Magento Belarus debug Pavel Novitsky (eng)
Pavel Novitsky
 

Ähnlich wie Magento Dependency Injection (20)

Magento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request FlowMagento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request Flow
 
Zend Framework 2 - Basic Components
Zend Framework 2  - Basic ComponentsZend Framework 2  - Basic Components
Zend Framework 2 - Basic Components
 
ZF2 for the ZF1 Developer
ZF2 for the ZF1 DeveloperZF2 for the ZF1 Developer
ZF2 for the ZF1 Developer
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony Apps
 
Growing up with Magento
Growing up with MagentoGrowing up with Magento
Growing up with Magento
 
Virtual Madness @ Etsy
Virtual Madness @ EtsyVirtual Madness @ Etsy
Virtual Madness @ Etsy
 
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
 
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy ApplicationsAdding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
 
(PHPers Wrocław #5) How to write valuable unit test?
(PHPers Wrocław #5) How to write valuable unit test?(PHPers Wrocław #5) How to write valuable unit test?
(PHPers Wrocław #5) How to write valuable unit test?
 
Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5
 
Min-Maxing Software Costs
Min-Maxing Software CostsMin-Maxing Software Costs
Min-Maxing Software Costs
 
Lithium Best
Lithium Best Lithium Best
Lithium Best
 
Key Insights into Development Design Patterns for Magento 2 - Magento Live UK
Key Insights into Development Design Patterns for Magento 2 - Magento Live UKKey Insights into Development Design Patterns for Magento 2 - Magento Live UK
Key Insights into Development Design Patterns for Magento 2 - Magento Live UK
 
Magento Performance Toolkit
Magento Performance ToolkitMagento Performance Toolkit
Magento Performance Toolkit
 
Introduction to Zend Framework web services
Introduction to Zend Framework web servicesIntroduction to Zend Framework web services
Introduction to Zend Framework web services
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technology
 
Symfony2 - from the trenches
Symfony2 - from the trenchesSymfony2 - from the trenches
Symfony2 - from the trenches
 
Utilization of zend an ultimate alternate for intense data processing
Utilization of zend  an ultimate alternate for intense data processingUtilization of zend  an ultimate alternate for intense data processing
Utilization of zend an ultimate alternate for intense data processing
 
Meet Magento Belarus debug Pavel Novitsky (eng)
Meet Magento Belarus debug Pavel Novitsky (eng)Meet Magento Belarus debug Pavel Novitsky (eng)
Meet Magento Belarus debug Pavel Novitsky (eng)
 

KĂźrzlich hochgeladen

Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
amitlee9823
 
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
amitlee9823
 
Just Call Vip call girls diu Escorts ☎️9352988975 Two shot with one girl (diu )
Just Call Vip call girls diu Escorts ☎️9352988975 Two shot with one girl (diu )Just Call Vip call girls diu Escorts ☎️9352988975 Two shot with one girl (diu )
Just Call Vip call girls diu Escorts ☎️9352988975 Two shot with one girl (diu )
gajnagarg
 
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard ...
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard  ...Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard  ...
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard ...
nirzagarg
 
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
instagramfab782445
 
ab-initio-training basics and architecture
ab-initio-training basics and architectureab-initio-training basics and architecture
ab-initio-training basics and architecture
saipriyacoool
 
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
SUHANI PANDEY
 
Vip Mumbai Call Girls Borivali Call On 9920725232 With Body to body massage w...
Vip Mumbai Call Girls Borivali Call On 9920725232 With Body to body massage w...Vip Mumbai Call Girls Borivali Call On 9920725232 With Body to body massage w...
Vip Mumbai Call Girls Borivali Call On 9920725232 With Body to body massage w...
amitlee9823
 
Just Call Vip call girls dharamshala Escorts ☎️9352988975 Two shot with one g...
Just Call Vip call girls dharamshala Escorts ☎️9352988975 Two shot with one g...Just Call Vip call girls dharamshala Escorts ☎️9352988975 Two shot with one g...
Just Call Vip call girls dharamshala Escorts ☎️9352988975 Two shot with one g...
gajnagarg
 

KĂźrzlich hochgeladen (20)

Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
 
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
 
💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
 
Gamestore case study UI UX by Amgad Ibrahim
Gamestore case study UI UX by Amgad IbrahimGamestore case study UI UX by Amgad Ibrahim
Gamestore case study UI UX by Amgad Ibrahim
 
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
 
Just Call Vip call girls diu Escorts ☎️9352988975 Two shot with one girl (diu )
Just Call Vip call girls diu Escorts ☎️9352988975 Two shot with one girl (diu )Just Call Vip call girls diu Escorts ☎️9352988975 Two shot with one girl (diu )
Just Call Vip call girls diu Escorts ☎️9352988975 Two shot with one girl (diu )
 
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard ...
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard  ...Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard  ...
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard ...
 
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
 
ab-initio-training basics and architecture
ab-initio-training basics and architectureab-initio-training basics and architecture
ab-initio-training basics and architecture
 
8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available
8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available
8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available
 
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
 
Q4-W4-SCIENCE-5 power point presentation
Q4-W4-SCIENCE-5 power point presentationQ4-W4-SCIENCE-5 power point presentation
Q4-W4-SCIENCE-5 power point presentation
 
❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.
❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.
❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.
 
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
 
Vip Mumbai Call Girls Borivali Call On 9920725232 With Body to body massage w...
Vip Mumbai Call Girls Borivali Call On 9920725232 With Body to body massage w...Vip Mumbai Call Girls Borivali Call On 9920725232 With Body to body massage w...
Vip Mumbai Call Girls Borivali Call On 9920725232 With Body to body massage w...
 
WhatsApp Chat: 📞 8617697112 Call Girl Baran is experienced
WhatsApp Chat: 📞 8617697112 Call Girl Baran is experiencedWhatsApp Chat: 📞 8617697112 Call Girl Baran is experienced
WhatsApp Chat: 📞 8617697112 Call Girl Baran is experienced
 
Sweety Planet Packaging Design Process Book.pptx
Sweety Planet Packaging Design Process Book.pptxSweety Planet Packaging Design Process Book.pptx
Sweety Planet Packaging Design Process Book.pptx
 
Jordan_Amanda_DMBS202404_PB1_2024-04.pdf
Jordan_Amanda_DMBS202404_PB1_2024-04.pdfJordan_Amanda_DMBS202404_PB1_2024-04.pdf
Jordan_Amanda_DMBS202404_PB1_2024-04.pdf
 
8377087607, Door Step Call Girls In Majnu Ka Tilla (Delhi) 24/7 Available
8377087607, Door Step Call Girls In Majnu Ka Tilla (Delhi) 24/7 Available8377087607, Door Step Call Girls In Majnu Ka Tilla (Delhi) 24/7 Available
8377087607, Door Step Call Girls In Majnu Ka Tilla (Delhi) 24/7 Available
 
Just Call Vip call girls dharamshala Escorts ☎️9352988975 Two shot with one g...
Just Call Vip call girls dharamshala Escorts ☎️9352988975 Two shot with one g...Just Call Vip call girls dharamshala Escorts ☎️9352988975 Two shot with one g...
Just Call Vip call girls dharamshala Escorts ☎️9352988975 Two shot with one g...
 

Magento Dependency Injection

  • 1. Киев 15 сентября 2012 Magento DI
  • 2. Software Entropy Pull-approach and its problems Push-approach Magento Object Manager
  • 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. class Mage_Cms_Model_PageTest extends PHPUnit_Framework_TestCase { public function setUp() { $this->_page = new Mage_Cms_Model_Page(); } }
  • 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()); } }
  • 12. class Mage_Core_Model_Abstract { public function __construct(array $data = array()) { parent::__construct($data); $this->_construct(); } }
  • 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(); } }
  • 16. interface Magento_ObjectManager { public function get($className, $arguments); public function create($className, $arguments); }
  • 17.
  • 19. 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(); } }
  • 20. <config> <global> <di> <Mage_Core_Model_Cache> <parameters> <cacheDir>/path/to/cache/dir</cacheDir> </parameters> </Mage_Core_Model_Cache> </di> </global> </config>
  • 21. <di> <Magento_Data_Structure> <shared>0</shared> </Magento_Data_Structure> </di>
  • 23. <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>
  • 24. INJECTABLES - NON-INJECTABLES Varien_Db_Adapter Mage_Catalog_Model_Product Mage_Core_Model_Cache Mage_Wishlist_Model_Wishlist Mage_Core_Model_Config
  • 25. class Varien_Data_Collection { //... public function getNewEmptyItem() { return Mage::getModel($this->_itemObjectClass); } //... }
  • 26. class Varien_Data_Collection { public function __construct(Magento_ObjectFactory $factory) { $this->_itemFactory = $factory; } //... public function getNewEmptyItem() { return $this->_itemFactory->create(); } //... }
  • 27. 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); } }
  • 28. 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); } } }
  • 29. class Mage_Review_Model_Observer { public function __construct(Mage_Review_Model_Resource_Review $review) { $this->_reviewResource = $reviewResource; } //... }
  • 30. <di> <Mage_Review_Model_Observer> <parameters> <review> Mage_Review_Model_Resource_Review_Proxy </review> </parameters> </ Mage_Review_Model_Observer > </di>
  • 31. 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(); } }