Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Magento 2 Dependency Injection, Interceptors, a...

Magento 2 Dependency Injection, Interceptors, and You - php[world] 2015

Magento 2 introduces dependency injection and interceptors, two approaches to creating a more flexible, extendable architecture. These concepts are new to many PHP developers, but they are critical to understanding and taking advantage of the power and flexibility of Magento 2. Let's take a high-level look at these concepts, and then dive into some real examples of how to utilize these techniques in your development work with Magento 2.

Joshua Warren

November 18, 2015
Tweet

More Decks by Joshua Warren

Other Decks in Programming

Transcript

  1. PRESENTED BY JOSHUA WARREN PRESENTED AT PHPWORLD 2015 Magento 2

    DEPENDENCY INJECTION, INTERCEPTORS AND YOU
  2. JoshuaWarren.com 100 slides in ~45 minutes lots of code focus

    on the concepts, download the slides later #phpworld
  3. JoshuaWarren.com Led the Creatuity team in building 3 Magento 2

    extensions (more on the way!) #phpworld
  4. JoshuaWarren.com Not a Magento employee, but working closely with the

    development, documentation & product teams on Magento 2 #phpworld
  5. JoshuaWarren.com Approach Magento 2 with a desire to learn and

    understand the underlying patterns. #phpworld
  6. JoshuaWarren.com Don’t approach Magento 2 with the thought “how do

    I make my Magento 1 code work here” #phpworld
  7. JoshuaWarren.com Some models use a single table, others continue to

    use the Entity-Attribute-Value design pattern used in Magento 1. #phpworld
  8. JoshuaWarren.com We are going to take a closer look at

    two patterns today: dependency injection and interceptors #phpworld
  9. JoshuaWarren.com DI is exactly what it sounds like - injecting

    dependencies into the objects that need them. #phpworld
  10. JoshuaWarren.com With DI, instead of building an object in your

    class, it’s passed in via your constructor. #phpworld
  11. JoshuaWarren.com DI allows for mocking - replacing things like the

    MySQL adapter with a mock object during testing #phpworld
  12. JoshuaWarren.com Objects do not need to locate the object or

    value in which it depends - it’s automatic. #phpworld
  13. JoshuaWarren.com #phpworld di.xml <config xmlns:xsi=“[…]” xsi:noNamespaceSchemaLocation=“[…]”>
 <virtualType name="Magento\SamplePaymentProvider\Block\Form\Payinstore" type="Magento\Payment\Block\Form" shared="false">

    <arguments>
 <argument name="data" xsi:type="array">
 <item name="template" xsi:type=“string"> Magento_SamplePaymentProvider::form/payinstore.phtml </item>
 </argument>
 </arguments>
 </virtualType>
 </config>
  14. JoshuaWarren.com #phpworld Without Dependency Injection public function getFormattedPrice($sku)
 {
 $db

    = new DBHandler;
 $row = $db->query('SELECT price FROM products WHERE sku = ?', $sku);
 $formatter = new PriceFormatter;
 return $formatter->asDollars($row['price']);
 }
  15. JoshuaWarren.com #phpworld With Dependency Injection public function getFormattedPrice($sku, $db, $formatter)


    {
 $row = $db->query('SELECT price FROM products WHERE sku = ?', $sku);
 return $formatter->asDollars($row['price']);
 }
  16. JoshuaWarren.com #phpworld ‘Around’ Interceptor class Plugin
 {
 public function aroundSave(\Magento\Catalog\Model\Product

    $subject, \Closure $proceed)
 {
 $this->doSomethingBeforeProductIsSaved();
 $returnValue = $proceed();
 if ($returnValue) {
 $this->postProductToFacebook();
 }
 return $returnValue;
 }
 }
  17. JoshuaWarren.com Check out the magento 2 sample modules repo for

    a module that demonstrates interception #phpworld
  18. JoshuaWarren.com #phpworld DI.xml <type name="Magento\SampleInterception\Model\Intercepted\ChildBefore">
 <plugin name="Magento_SampleInterception::demoPluginBefore" type="Magento\SampleInterception\Plugin\PluginBefore" />
 </type>


    <type name="Magento\SampleInterception\Model\Intercepted\ChildAfter">
 <plugin name="Magento_SampleInterception::demoPluginAfter" type="Magento\SampleInterception\Plugin\PluginAfter" />
 </type>
 <type name="Magento\SampleInterception\Model\Intercepted\ChildAround">
 <plugin name="Magento_SampleInterception::demoPluginAround" type="Magento\SampleInterception\Plugin\PluginAround" />
 </type>
 <type name="Magento\SampleInterception\Model\Intercepted\ChildInherit">
 <plugin name="Magento_SampleInterception::demoPluginInheritance" type="Magento\SampleInterception\Plugin\ParentPlugin" />
 </type>

  19. JoshuaWarren.com #phpworld Plugin/PluginAround.php public function aroundBaseMethodUppercase(ChildAround $subject, \Closure $proceed, $interceptedInput)


    {
 $argument = "(around: before base method) $interceptedInput (/around: before base method)";
 $result = $proceed($argument);
 return "(around: after base method) $result (/around: after base method)";
 }
  20. JoshuaWarren.com We’re all waiting to see what happens with sort

    order conflicts in real world usage #phpworld
  21. JoshuaWarren.com If not, when installing extensions, developers will need to

    modify their sort order to resolve conflicts. #phpworld
  22. JoshuaWarren.com In order to run the sample modules, you will

    have to add a registration.php file. #phpworld
  23. Dev Docs Team Team of hard-working technical writers (not developers)

    Writing documentation for a system that has yet to be used ‘in the wild’ Very eager for feedback and input - they don’t know what documentation you need Very open to pull requests of documentation or just open an issue on Github with feedback + requests JoshuaWarren.com #phpworld
  24. JoshuaWarren.com As a freelancer… Learning Magento 2 Set aside time

    in your week to learn the design patterns Magento 2 uses Work through the sample code the Magento 2 team has provided Begin experimenting with developing with Magento 2 Do not try to learn ‘on the job’ - be careful accepting M2 work before you’re ready #phpworld
  25. JoshuaWarren.com As an in-house developer for a merchant… Learning Magento

    2 Determine when your business is likely to migrate to Magento 2 First 2-4 weeks of your Magento 2 migration schedule should be learning Magento 2 Learn the patterns before you start! #phpworld
  26. JoshuaWarren.com As an agency or industry partner… Learning Magento 2

    Create a tiger team of developers focused on Magento 2 Allow those developers time in the day to learn Magento 2 Those developers should implement your first Magento 2 projects That team then helps the rest of your team through the learning curve #phpworld
  27. JoshuaWarren.com When do I need to be Magento 2 ready?

    Learning Magento 2 Magento 2 will be released late enough in the year that most merchants won’t begin using it immediately. Merchants will also wait until their mission-critical extensions are available on Magento 2. Start learning it now - but don’t panic! #phpworld