Slide 1

Slide 1 text

PRESENTED BY JOSHUA WARREN PRESENTED AT PHPWORLD 2015 Magento 2 DEPENDENCY INJECTION, INTERCEPTORS AND YOU

Slide 2

Slide 2 text

JoshuaWarren.com #phpworld

Slide 3

Slide 3 text

JoshuaWarren.com Magento 2 officially released yesterday! #phpworld

Slide 4

Slide 4 text

JoshuaWarren.com 100 slides in ~45 minutes lots of code focus on the concepts, download the slides later #phpworld

Slide 5

Slide 5 text

MY EXPERIENCE

Slide 6

Slide 6 text

JoshuaWarren.com My Experience PHP Developer Since 1999 Founded Creatuity in 2008 Focused on the Magento platform #phpworld

Slide 7

Slide 7 text

JoshuaWarren.com early adopter of both Magento 1 and Magento 2 #phpworld

Slide 8

Slide 8 text

JoshuaWarren.com Frequent Magento presenter #phpworld

Slide 9

Slide 9 text

JoshuaWarren.com Led the Creatuity team in building 3 Magento 2 extensions (more on the way!) #phpworld

Slide 10

Slide 10 text

JoshuaWarren.com Already migrating a few merchants from Magento 1 to Magento 2 #phpworld

Slide 11

Slide 11 text

JoshuaWarren.com Wrote Writing the book on Magento 2 #phpworld

Slide 12

Slide 12 text

JoshuaWarren.com Not a Magento employee, but working closely with the development, documentation & product teams on Magento 2 #phpworld

Slide 13

Slide 13 text

JoshuaWarren.com A quick note for any Magento 1 developers in the audience… #phpworld

Slide 14

Slide 14 text

JoshuaWarren.com #phpworld

Slide 15

Slide 15 text

JoshuaWarren.com …for PHP developers new to Magento. #phpworld

Slide 16

Slide 16 text

JoshuaWarren.com PHP developers are learning Magento 2 faster than many Magento 1 developers. #phpworld

Slide 17

Slide 17 text

JoshuaWarren.com Approach Magento 2 with a desire to learn and understand the underlying patterns. #phpworld

Slide 18

Slide 18 text

JoshuaWarren.com Don’t approach Magento 2 with the thought “how do I make my Magento 1 code work here” #phpworld

Slide 19

Slide 19 text

JoshuaWarren.com #phpworld

Slide 20

Slide 20 text

JoshuaWarren.com Shoshin is a Zen concept meaning “beginner’s mind” #phpworld

Slide 21

Slide 21 text

JoshuaWarren.com An attitude of openness, eagerness and lack of preconceptions. #phpworld

Slide 22

Slide 22 text

JoshuaWarren.com No matter your level of Magento 1 experience, approach Magento 2 with a beginner’s mind #phpworld

Slide 23

Slide 23 text

JoshuaWarren.com With that mind, let’s dive into Magento 2… #phpworld

Slide 24

Slide 24 text

MAGENTO 2 github.com/magento/magento2

Slide 25

Slide 25 text

JoshuaWarren.com Technologies #phpworld

Slide 26

Slide 26 text

JoshuaWarren.com #phpworld Composer composer create-project magento/product-community-edition -- stability="beta"

Slide 27

Slide 27 text

JoshuaWarren.com Each Magento 2 module is a separate Composer package #phpworld

Slide 28

Slide 28 text

JoshuaWarren.com PSR-0 thru PSR-4 #phpworld

Slide 29

Slide 29 text

JoshuaWarren.com Testing built in from the start. phpunit, selenium, JMeter, Jasmine #phpworld

Slide 30

Slide 30 text

JoshuaWarren.com HTML5, CSS3, LESS CSS Preprocessor, JQuery, RequireJS #phpworld

Slide 31

Slide 31 text

JoshuaWarren.com Components from Zend Framework 1, Zend Framework 2, Symfony #phpworld

Slide 32

Slide 32 text

JoshuaWarren.com Technical Architecture #phpworld

Slide 33

Slide 33 text

JoshuaWarren.com Presentation Layer, Service Layer, Domain Layer, Persistence Layer #phpworld

Slide 34

Slide 34 text

JoshuaWarren.com #phpworld

Slide 35

Slide 35 text

JoshuaWarren.com Presentation Layer - views, literally and figuratively #phpworld

Slide 36

Slide 36 text

JoshuaWarren.com Service Layer - an intermediary between the presentation and model layers #phpworld

Slide 37

Slide 37 text

JoshuaWarren.com Service layer provides a stable, backwards-compatible interface and forms the foundation for dependency injection. #phpworld

Slide 38

Slide 38 text

JoshuaWarren.com Domain layer - business logic, including models. Contains the implementation of service contracts. #phpworld

Slide 39

Slide 39 text

JoshuaWarren.com Persistence Layer - resource models that perform CRUD operations on database tables. #phpworld

Slide 40

Slide 40 text

JoshuaWarren.com Some models use a single table, others continue to use the Entity-Attribute-Value design pattern used in Magento 1. #phpworld

Slide 41

Slide 41 text

JoshuaWarren.com Design Patterns #phpworld

Slide 42

Slide 42 text

JoshuaWarren.com Loose Coupling #phpworld

Slide 43

Slide 43 text

JoshuaWarren.com Dependency Injection #phpworld

Slide 44

Slide 44 text

JoshuaWarren.com Service Contracts #phpworld

Slide 45

Slide 45 text

JoshuaWarren.com Interceptors #phpworld

Slide 46

Slide 46 text

JoshuaWarren.com Semantic Versioning #phpworld

Slide 47

Slide 47 text

JoshuaWarren.com Start your Magento 2 journey learning the basics of these design patterns #phpworld

Slide 48

Slide 48 text

JoshuaWarren.com We are going to take a closer look at two patterns today: dependency injection and interceptors #phpworld

Slide 49

Slide 49 text

DEPENDENCY INJECTION Sorry - no cool photo here, because I don’t like needles…

Slide 50

Slide 50 text

JoshuaWarren.com DI is exactly what it sounds like - injecting dependencies into the objects that need them. #phpworld

Slide 51

Slide 51 text

JoshuaWarren.com It sounds complicated, but it’s not. #phpworld

Slide 52

Slide 52 text

JoshuaWarren.com Hollywood Principle: “Don’t call us, we’ll call you” #phpworld

Slide 53

Slide 53 text

JoshuaWarren.com With DI, instead of building an object in your class, it’s passed in via your constructor. #phpworld

Slide 54

Slide 54 text

JoshuaWarren.com DI is designed to reduce dependencies and promote loose coupling #phpworld

Slide 55

Slide 55 text

JoshuaWarren.com DI makes unit testing much easier #phpworld

Slide 56

Slide 56 text

JoshuaWarren.com DI allows for mocking - replacing things like the MySQL adapter with a mock object during testing #phpworld

Slide 57

Slide 57 text

JoshuaWarren.com Magento 2 uses the Constructor Injection pattern of DI #phpworld

Slide 58

Slide 58 text

JoshuaWarren.com This replaces the usage of the Mage class from Magento 1. #phpworld

Slide 59

Slide 59 text

JoshuaWarren.com DI in Magento 2 is Automatic Dependency Injection #phpworld

Slide 60

Slide 60 text

JoshuaWarren.com Objects do not need to locate the object or value in which it depends - it’s automatic. #phpworld

Slide 61

Slide 61 text

JoshuaWarren.com Magento’s object manager uses PHP’s reflection features to automatically instantiate needed objects #phpworld

Slide 62

Slide 62 text

JoshuaWarren.com DI in Magento 2 is handled via XML files #phpworld

Slide 63

Slide 63 text

JoshuaWarren.com #phpworld di.xml 
 
 
 Magento_SamplePaymentProvider::form/payinstore.phtml 
 
 
 


Slide 64

Slide 64 text

JoshuaWarren.com I highly recommend Alan Storm’s article “Magento 2’s Automatic Dependency Injection” #phpworld

Slide 65

Slide 65 text

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']);
 }

Slide 66

Slide 66 text

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']);
 }

Slide 67

Slide 67 text

JoshuaWarren.com DI simplifies testing, maintenance and readability #phpworld

Slide 68

Slide 68 text

JoshuaWarren.com DI.XML is also where plugins following our next pattern to discuss are declared. #phpworld

Slide 69

Slide 69 text

INTERCEPTORS

Slide 70

Slide 70 text

JoshuaWarren.com Plugin system based on the interceptor pattern #phpworld

Slide 71

Slide 71 text

JoshuaWarren.com Calls to almost any module can be intercepted and altered #phpworld

Slide 72

Slide 72 text

JoshuaWarren.com Vast improvement over the rewrite pattern in Magento 1 - no more rewrite conflicts #phpworld

Slide 73

Slide 73 text

JoshuaWarren.com #phpworld di.xml 
 
 
 


Slide 74

Slide 74 text

JoshuaWarren.com Sort order defines order if multiple plugins intercept the same item #phpworld

Slide 75

Slide 75 text

JoshuaWarren.com Possible to intercept before, after and around a function #phpworld

Slide 76

Slide 76 text

JoshuaWarren.com #phpworld ‘Before’ Interceptor class Plugin
 {
 public function beforeSetName(\Magento\Catalog\Model\Product $subject, $name)
 {
 return array('(' . $name . ')');
 }
 }

Slide 77

Slide 77 text

JoshuaWarren.com #phpworld ‘After’ Interceptor class Plugin
 {
 public function afterGetName(\Magento\Catalog\Model\Product $subject, $result)
 {
 return '|' . $result . '|';
 }
 }

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

JoshuaWarren.com Check out the magento 2 sample modules repo for a module that demonstrates interception #phpworld

Slide 80

Slide 80 text

JoshuaWarren.com #phpworld DI.xml 
 
 
 
 
 
 
 
 
 
 
 


Slide 81

Slide 81 text

JoshuaWarren.com #phpworld Plugin/PluginBefore.php public function beforeBaseMethodUppercase(ChildBefore $subject, $interceptedInput)
 {
 return ["(before) $interceptedInput (/before)"];
 }

Slide 82

Slide 82 text

JoshuaWarren.com #phpworld Plugin/PluginAfter.php public function afterBaseMethodUppercase(ChildAfter $subject, $interceptedOutput)
 {
 return "(after) $interceptedOutput (/after)";
 }

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

JoshuaWarren.com Interceptors are a replacement for rewrites #phpworld

Slide 85

Slide 85 text

JoshuaWarren.com Interceptors supplement, but not replace, events and observers #phpworld

Slide 86

Slide 86 text

JoshuaWarren.com Sort order conflicts #phpworld

Slide 87

Slide 87 text

JoshuaWarren.com Please don’t define a sort order of 1 unless you really need it #phpworld

Slide 88

Slide 88 text

JoshuaWarren.com We’re all waiting to see what happens with sort order conflicts in real world usage #phpworld

Slide 89

Slide 89 text

JoshuaWarren.com Ideally, the largest extension authors will coordinate their sort order for common conflicts #phpworld

Slide 90

Slide 90 text

JoshuaWarren.com If not, when installing extensions, developers will need to modify their sort order to resolve conflicts. #phpworld

Slide 91

Slide 91 text

LEARNING MORE Don’t end up like this guy ->

Slide 92

Slide 92 text

JoshuaWarren.com devdocs.magento.com - ‘How Do I?’ magento.stackexchange.com/questions/tagged/ magento2 https://github.com/magento/magento2-samples #phpworld

Slide 93

Slide 93 text

JoshuaWarren.com Read through the sample modules. #phpworld

Slide 94

Slide 94 text

JoshuaWarren.com The sample modules don’t currently work (see issue #42) #phpworld

Slide 95

Slide 95 text

JoshuaWarren.com In order to run the sample modules, you will have to add a registration.php file. #phpworld

Slide 96

Slide 96 text

JoshuaWarren.com Otherwise, they’re still a great resource to see dependency injection and interceptors in action. #phpworld

Slide 97

Slide 97 text

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

Slide 98

Slide 98 text

Dev Docs Team JoshuaWarren.com #phpworld

Slide 99

Slide 99 text

JoshuaWarren.com Magento U Courses Fundamentals of Magento 2 Development Front-end Course #phpworld

Slide 100

Slide 100 text

JoshuaWarren.com AlanStorm.com AlanKent.me CoderOnCode.com github.com/creatuity/LearningMagento2 #phpworld

Slide 101

Slide 101 text

JoshuaWarren.com Upcoming events: Magento Imagine - April 2016 #phpworld

Slide 102

Slide 102 text

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

Slide 103

Slide 103 text

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

Slide 104

Slide 104 text

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

Slide 105

Slide 105 text

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

Slide 106

Slide 106 text

JoshuaWarren.com Programming With Magento 2 coming to amazon.com & phparch.com #phpworld

Slide 107

Slide 107 text

Keep in Touch! @JoshuaSWarren JoshuaWarren.com Mage2DevBook.com joind.in/14790

Slide 108

Slide 108 text

JoshuaWarren.com #phpworld