SlideShare une entreprise Scribd logo
1  sur  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();
    }
}

Contenu connexe

Tendances

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
 

Tendances (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
 

Similaire à 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
 

Similaire à 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)
 

Dernier

Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
amitlee9823
 
Editorial design Magazine design project.pdf
Editorial design Magazine design project.pdfEditorial design Magazine design project.pdf
Editorial design Magazine design project.pdf
tbatkhuu1
 
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
amitlee9823
 
Design Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptxDesign Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptx
TusharBahuguna2
 
Call Girls In Jp Nagar ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Jp Nagar ☎ 7737669865 🥵 Book Your One night StandCall Girls In Jp Nagar ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Jp Nagar ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
kumaririma588
 
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
 
怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证
怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证
怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证
eeanqy
 
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
amitlee9823
 

Dernier (20)

❤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 💦✅.
 
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
 
AMBER GRAIN EMBROIDERY | Growing folklore elements | Root-based materials, w...
AMBER GRAIN EMBROIDERY | Growing folklore elements |  Root-based materials, w...AMBER GRAIN EMBROIDERY | Growing folklore elements |  Root-based materials, w...
AMBER GRAIN EMBROIDERY | Growing folklore elements | Root-based materials, w...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
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
 
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
 
Editorial design Magazine design project.pdf
Editorial design Magazine design project.pdfEditorial design Magazine design project.pdf
Editorial design Magazine design project.pdf
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
 
💫✅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...
 
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 ...
 
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
 
Design Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptxDesign Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptx
 
Booking open Available Pune Call Girls Nanded City 6297143586 Call Hot India...
Booking open Available Pune Call Girls Nanded City  6297143586 Call Hot India...Booking open Available Pune Call Girls Nanded City  6297143586 Call Hot India...
Booking open Available Pune Call Girls Nanded City 6297143586 Call Hot India...
 
Call Girls In Jp Nagar ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Jp Nagar ☎ 7737669865 🥵 Book Your One night StandCall Girls In Jp Nagar ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Jp Nagar ☎ 7737669865 🥵 Book Your One night Stand
 
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
 
Sector 105, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 105, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 105, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 105, Noida Call girls :8448380779 Model Escorts | 100% verified
 
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 🔝...
 
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 ...
 
怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证
怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证
怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证
 
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
 

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(); } }