technical insights into magento 2: what it means for developers

45

Upload: session-digital

Post on 14-Jan-2017

235 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Technical insights into Magento 2: What it means for developers
Page 2: Technical insights into Magento 2: What it means for developers

MAGENTO 2WHAT TO EXPECT AS A

DEVELOPER

Page 3: Technical insights into Magento 2: What it means for developers

JAMES COWIETECHNICAL TEAM LEAD SESSION

DIGITALT/@JCOWIE GH/JAMESCOWIE

Page 4: Technical insights into Magento 2: What it means for developers

WHAT'S CHANGED SINCE

MAGENTO 1

Page 5: Technical insights into Magento 2: What it means for developers
Page 6: Technical insights into Magento 2: What it means for developers

> 2009 Namespaces> 2009 Traits

> 2012 Composer> 2012 Typehinting> 2013 Generators

Page 7: Technical insights into Magento 2: What it means for developers

THE WORLD OFENGINEERING HAS CHANGED

Page 8: Technical insights into Magento 2: What it means for developers

> Server Architecture> Test Driven Development

> Behaviour Driven Development> Domain Driven Design

Page 9: Technical insights into Magento 2: What it means for developers

MONOLITHIC

Page 10: Technical insights into Magento 2: What it means for developers

COMPOSER

Page 11: Technical insights into Magento 2: What it means for developers

composer create-project --stability=beta --no-install magento/project-community-edition M2Test

Page 12: Technical insights into Magento 2: What it means for developers

{ "name": "magento/project-community-edition", "type": "project", "require": { "magento/product-community-edition": "0.74.0-beta12" }, "require-dev": { }}

Page 13: Technical insights into Magento 2: What it means for developers

COMPOSER CAN DO MORE

Page 14: Technical insights into Magento 2: What it means for developers

SIMPLE INSTALL PACKAGES composer require "league/period"

Page 15: Technical insights into Magento 2: What it means for developers

USE THE PACKAGE 1 /** 2 * @var League\Period\Period; 3 */ 4 protected $_datePeriod; 5 6 /** 7 * @param \League\Period\Period; $datePeriod 8 */ 9 public function __construct(\League\Period\Period $datePeriod)10 {11 $this->_datePeriod = $datePeriod;12 }

Page 16: Technical insights into Magento 2: What it means for developers

VERSIONING1.0.2

Page 17: Technical insights into Magento 2: What it means for developers

EXACT MATCH"magento/product-community-edition":

“1.0.1”

Page 18: Technical insights into Magento 2: What it means for developers

RANGES"magento/product-community-edition":

“>=1.0, <2”

Page 19: Technical insights into Magento 2: What it means for developers

WILDCARD"magento/product-community-edition":

“1.0.*”

Page 20: Technical insights into Magento 2: What it means for developers

NEXT SIGNIFICANT RELEASE"magento/product-community-edition": “~1.2”

Page 21: Technical insights into Magento 2: What it means for developers

SEMANTIC VERSIONING2

2 The full specification can be found at the http://semver.org website

Page 22: Technical insights into Magento 2: What it means for developers

PACKAGING MODULES FOR REUSE

Page 23: Technical insights into Magento 2: What it means for developers

HOW TO PACKAGE A MAGENTO 2 MODULE{ "name": "SessionDigital/Magento2Module", "description": "Magento 2 module to show how to package a module", "extra": { "map": [ [ "*", "SessionDigital/Magento2Module" ] ] }}

Page 24: Technical insights into Magento 2: What it means for developers

USE AUTOLOADING{ "name": "SessionDigital/Magento2Module", "description": "N/A", "require": { "php": "~5.5.0|~5.6.0|~7.0.0", "magento/framework": "100.0.0" }, "type": "magento2-module", "version": "1.0.0", "license": [ "OSL-3.0", "AFL-3.0" ], "autoload": { "files": [ "registration.php" ], "psr-4": { "SessionDigital\\Magento2Module\\": "" } }}

Page 25: Technical insights into Magento 2: What it means for developers

REGISTRATION.PHP<?php\Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'SessionDigital_Magento2Module', __DIR__);

Page 26: Technical insights into Magento 2: What it means for developers

DECOUPLE FROM THE

FRAMEWORK

Page 27: Technical insights into Magento 2: What it means for developers

WHAT ARE THE BENEFITS

> Clean Code > Reusable packages

> Testable> Easier to read

> Easier to maintain

Page 28: Technical insights into Magento 2: What it means for developers

THE NAKEDMAGENTO MODULE

Page 29: Technical insights into Magento 2: What it means for developers

DEPENDENCY INJECTIONIN MAGENTO 2

Page 30: Technical insights into Magento 2: What it means for developers

public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Catalog\Model\Design $catalogDesign, \Magento\Catalog\Model\Session $catalogSession, \Magento\Framework\Registry $coreRegistry, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator, PageFactory $resultPageFactory, \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory, Resolver $layerResolver, CategoryRepositoryInterface $categoryRepository \Magento\Framework\App\Action\Context $context, \Magento\Catalog\Model\Design $catalogDesign, \Magento\Catalog\Model\Session $catalogSession, \Magento\Framework\Registry $coreRegistry, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator ) { ... }

Page 31: Technical insights into Magento 2: What it means for developers

CODE SMELL

Page 32: Technical insights into Magento 2: What it means for developers

> replaces Mage:: god class> Dependency Injection can be overused.

> Enables composition.

Page 33: Technical insights into Magento 2: What it means for developers

BEFORE DEPENDENCY INJECTION 1 <?php 2 class Sample 3 { 4 protected $logger; 5 6 public function doSomething() 7 { 8 $this->logger = new \Logger(); 9 $logger->doSomething();10 }11 }

Page 34: Technical insights into Magento 2: What it means for developers

MAGENTO 2 DI 1 <?php 2 class SampleDi { 3 protected $logger; 4 public function __construct(\Logger $logger) { 5 $this->logger = $logger; 6 } 7 8 public function doSomething() { 9 $this->logger->doSomething();10 }11 }

Page 35: Technical insights into Magento 2: What it means for developers

WHAT ARE THE BENEFITS ?

> Responsibility > Swap concrete implementation

> Mock the dependency

Page 36: Technical insights into Magento 2: What it means for developers

ITS NOT JUST OBJECTS<type name="Magento\Cms\Model\Wysiwyg\Images\Storage"> <arguments> <argument name="extensions" xsi:type="array"> <item name="allowed" xsi:type="array"> <item name="jpg" xsi:type="number">1</item> <item name="jpeg" xsi:type="number">1</item> <item name="png" xsi:type="number">1</item> <item name="gif" xsi:type="number">1</item> </item> </argument> </arguments>…

Page 37: Technical insights into Magento 2: What it means for developers

DI.XML3

<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="/lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">

<type name="Magento\Framework\View\TemplateEngineFactory"> <arguments> <argument name="engines" xsi:type="array"> <item name="twig" xsi:type="string">SchumacherFM\Twig\Framework\View\TemplateEngine\Twig</item> </argument> </arguments> </type></config>

3 Cyrill Schumacher's Twig extension

Page 38: Technical insights into Magento 2: What it means for developers

SERVICE CONTRACTS> PHP Interfaces

> Public API of the class> Design by contract 1

1 Design by contract

Page 39: Technical insights into Magento 2: What it means for developers

CUSTOMER REPOSITORY SERVICE CONTRACT<?phpnamespace Magento\Customer\Api;interface CustomerRepositoryInterface{ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $passwordHash = null); public function get($email, $websiteId = null); public function getById($customerId); public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria); public function delete(\Magento\Customer\Api\Data\CustomerInterface $customer); public function deleteById($customerId);}

Page 40: Technical insights into Magento 2: What it means for developers

ANNOTATIONS /** * Create customer. * * @api * @param \Magento\Customer\Api\Data\CustomerInterface $customer * @param string $passwordHash * @return \Magento\Customer\Api\Data\CustomerInterface * @throws \Magento\Framework\Exception\InputException If bad input is provided * @throws \Magento\Framework\Exception\State\InputMismatchException If the provided email is already used * @throws \Magento\Framework\Exception\LocalizedException */ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $passwordHash = null);

Page 41: Technical insights into Magento 2: What it means for developers
Page 42: Technical insights into Magento 2: What it means for developers

Untested code is incomplete code

-- Ben Marks @ Magento

Page 43: Technical insights into Magento 2: What it means for developers

HTTP://DEVDOCS.MAGENTO.COM

Page 44: Technical insights into Magento 2: What it means for developers
Page 45: Technical insights into Magento 2: What it means for developers

THANK YOU...