Slide 1

Slide 1 text

4 July 2015

Slide 2

Slide 2 text

@super_marek

Slide 3

Slide 3 text

at

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Background Magento platform: - old version - full of custom modifications - very basic CMS - limited budget

Slide 8

Slide 8 text

Requirement CMS - manage corporate website - manage other brands’ websites - build seasonal microsites - integrate with Magento store

Slide 9

Slide 9 text

http://photobucket.com/user/marcrobinsone/media/Drupal%20Comics/marcrobinsonegmailcom__drupalNinja.png.html

Slide 10

Slide 10 text

Why Drupal? - open source CMS - easy to install and manage - most required functionality available out of the box - easy to extend - easy to theme - fits the “limited budget” price tag

Slide 11

Slide 11 text

Why Drupal 7? - Drupal 8 was not stable yet!

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

What is Symfony?

Slide 15

Slide 15 text

is a powerful framework

Slide 16

Slide 16 text

Doctrine ORM Twig

Slide 17

Slide 17 text

set of decoupled components

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

Let’s build a micro-framework!

Slide 21

Slide 21 text

Kernel namespace Acme\Application; class Kernel { public function boot() { } }

Slide 22

Slide 22 text

symfony/dependency-injection symfony/config dependency injection

Slide 23

Slide 23 text

Dependency Injection component - a standardised and centralised way of constructing objects in our application

Slide 24

Slide 24 text

But! What is this DI thingy?

Slide 25

Slide 25 text

Dependency Injection Dependency Injection is where components are given their dependencies through their constructors, methods, or directly into fields.

Slide 26

Slide 26 text

Inject via constructor class Attendance { /** * @var Storage */ private $storage; public function __construct(Storage $storage) { $this->storage = $storage; } // ... }

Slide 27

Slide 27 text

Inject via setter class Attendance { /** * @var Storage */ private $storage; public function setStorage(Storage $storage) { $this->storage = $storage; } // ... }

Slide 28

Slide 28 text

Inject via public property class Attendance { /** * @var Storage */ public $storage; // ... } $attendance->storage = $storage;

Slide 29

Slide 29 text

Pros & cons of DI Pros: - clear definition of what objects are required - modular, decoupled architecture - makes testing code trivial Cons: - an overkill for small or short lived projects

Slide 30

Slide 30 text

Code Example class Mailer { public function sendEmail($subject, $body) { // Send email } } class Order { public function sendConfirmation() { $email = new Mailer(); $email->sendEmail($subject, $body); } }

Slide 31

Slide 31 text

The other way round class Order { /** * @var Mailer */ private $mailer; public function __construct(Mailer $mailer) { $this->mailer = $mailer; } public function sendConfirmation() { $this->mailer->sendEmail($subject, $body); } }

Slide 32

Slide 32 text

Interfaces to the rescue! interface OrderConfirmationSender { public function sendMessage($subject, $body); } class EmailMessageService implements OrderConfirmationSender { public function sendMessage($subject, $body) { // send email } }

Slide 33

Slide 33 text

Interfaces to the rescue! interface OrderConfirmationSender { public function sendMessage($subject, $body); } class EmailMessageService implements OrderConfirmationSender { private $mailer; public function __construct(Mailer $mailer) { $this->mailer = $mailer; } public function sendMessage($subject, $body) { $this->mailer->sendEmail($subject, $body); } }

Slide 34

Slide 34 text

Dependency Injection class Order { /** * @var OrderConfirmationSender */ private $confirmationSender; public function __construct(OrderConfirmationSender $confirmationSender) { $this->confirmationSender = $confirmationSender; } public function sendConfirmation() { $this->confirmationSender->sendMessage($subject, $body); } }

Slide 35

Slide 35 text

Dependency Inversion Order OrderConfirmationSender (interface)

Slide 36

Slide 36 text

Dependency Inversion Order OrderConfirmationSender (interface) EmailMessageService TextMessageService

Slide 37

Slide 37 text

Microframework!

Slide 38

Slide 38 text

composer.json { "require": { "php": ">=5.4.0", "symfony/dependency-injection": "~2.5", "symfony/config": "~2.5" }, "autoload": { "psr-0": { "": "src" } } }

Slide 39

Slide 39 text

Installing dependencies $ composer install Loading composer repositories with package information Installing dependencies (including require-dev) - Installing symfony/dependency-injection (v2.5.1) Loading from cache - Installing symfony/filesystem (v2.5.1) Loading from cache - Installing symfony/config (v2.5.1) Loading from cache symfony/dependency-injection suggests installing symfony/yaml () symfony/dependency-injection suggests installing symfony/proxy-manager-bridge (Generate service proxies to lazy load them) Writing lock file Generating autoload files $

Slide 40

Slide 40 text

Installing dependencies $ composer install - will install all dependencies as locked in composer. lock file (if exists) $ composer update - will attempt to update all dependencies to the latest versions as set in composer.json

Slide 41

Slide 41 text

Kernel public function boot() { $this->container = new ContainerBuilder(); $this->container->compile(); }

Slide 42

Slide 42 text

Kernel public function boot() { $this->container = new ContainerBuilder(); $loader = new YamlFileLoader($this->container, new FileLocator('app/config')); $loader->load('parameters.yml'); $this->container->compile(); }

Slide 43

Slide 43 text

Kernel public function boot() { $this->container = new ContainerBuilder(); $loader = new YamlFileLoader($this->container, new FileLocator('app/config')); $loader->load('parameters.yml'); $loader = new XmlFileLoader($this->container, new FileLocator('app/config')); $loader->load('services.xml'); $this->container->compile(); }

Slide 44

Slide 44 text

Kernel public function boot() { $this->container = new ContainerBuilder(); $loader = new YamlFileLoader($this->container, new FileLocator('app/config')); $loader->load('parameters.yml'); $loader = new XmlFileLoader($this->container, new FileLocator('app/config')); $loader->load('services.xml'); $this->container->registerExtension(new ApplicationExtension()); $this->container->compile(); }

Slide 45

Slide 45 text

Kernel public function boot() { $this->container = $this->buildContainer(); } /** * @return ContainerBuilder */ private function buildContainer() { $container = new ContainerBuilder(); $loader = new YamlFileLoader($container, new FileLocator('app/config')); $loader->load('parameters.yml'); $loader = new XmlFileLoader($container, new FileLocator('app/config')); $loader->load('services.xml'); $container->registerExtension(new ApplicationExtension()); $container->compile(); return $container; }

Slide 46

Slide 46 text

Kernel public function boot() { if (file_exists($this->cachedContainerFileName)) { require $this->cachedContainerFileName; $this->container = new ProjectServiceContainer(); } else { $container = $this->buildContainer(); $dumper = new PhpDumper($container); file_put_contents($this->cachedContainerFileName, $dumper->dump()); $this->container = $container; } }

Slide 47

Slide 47 text

Kernel public function boot() { if (file_exists($this->cachedContainerFileName)) { require $this->cachedContainerFileName; $this->container = new ProjectServiceContainer(); } else { $container = $this->buildContainer(); if ('dev' !== $container->getParameter('environment')) { $dumper = new PhpDumper($container); file_put_contents($this->cachedContainerFileName, $dumper->dump()); } $this->container = $container; } }

Slide 48

Slide 48 text

parameters.yml application: database: name: acme-dev username: acme-dev password: S0m3R4nD0mP455 host: localhost port: ~ soap_client: wsdl: https://api.store.local/v1/?wsdl http_auth_username: acme-dev http_auth_password: S0m3R4nD0mP455 username: acme-dev-user api_key: R4nD0mAp1K3y product_hydrator: product_url_snippet: http://store.local/product/{sku} product_image_url: http://store.local/media/catalog/product/uploads/

Slide 49

Slide 49 text

services.xml

Slide 50

Slide 50 text

symfony/console console commands

Slide 51

Slide 51 text

Installing dependencies $ composer require symfony/console ~2.5 ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) - Installing symfony/console (v2.5.1) Loading from cache symfony/console suggests installing symfony/event-dispatcher () symfony/console suggests installing symfony/process () symfony/console suggests installing psr/log (For using the console logger) Writing lock file Generating autoload files $

Slide 52

Slide 52 text

composer.json "require": { "php": ">=5.4.0", "symfony/dependency-injection": "~2.5", "symfony/config": "~2.5", "symfony/console": "~2.5" },

Slide 53

Slide 53 text

Import Handler class ImportAndPersistActionHandler { /** * @param ProductImporter $importer * @param ProductPersister $persister */ public function __construct(ProductImporter $importer, ProductPersister $persister) { $this->importer = $importer; $this->persister = $persister; } public function importAndPersistProducts() { $productsList = $this->importer->importProducts(); $this->persister->saveAll($productsList); } }

Slide 54

Slide 54 text

Console command class ImportAndPersistCommand extends Command { public function __construct(ImportAndPersistActionHandler $action) { $this->action = $action; parent::__construct(); } protected function configure() { $this->setName('products:import'); } protected function execute(InputInterface $input, OutputInterface $output) { $startTime = microtime(true); $this->action->importAndPersistProducts(); $output->writeln( sprintf("Products import completed in %1.02fs.", microtime(true) - $startTime) ); } }

Slide 55

Slide 55 text

app/console #!/usr/bin/php boot(); $application = new Application(); $application->addCommands([ $kernel->getContainer()->get('acme.command.import_products'), $kernel->getContainer()->get('acme.command.delete_products'), $kernel->getContainer()->get('acme.command.depot_locate'), ]); $application->run();

Slide 56

Slide 56 text

symfony/filesystem persisting products

Slide 57

Slide 57 text

XML Writer class ProductXmlWriter implements ProductPersister { /** * @param Filesystem $filesystem * @param string $fileName */ public function __construct(Filesystem $filesystem, string $fileName) { $this->filesystem = $filesystem; $this->fileName = $fileName; } /** * @param ProductList $productList */ public function saveAll(ProductList $productList) { $this->filesystem->dumpFile($this->fileName, $this->renderXml($productList)); } }

Slide 58

Slide 58 text

Why write XML? - fetching data from Magento and dumping it into xml file is quick - xml can be imported by Drupal’s feed importer

Slide 59

Slide 59 text

Why it didn’t work? - Feed importer was damn slow - 10k products imported in 4.5h - Scheduling import with Elysia Cron module was very flaky - Scheduled feed importer would add products all over again - resources hungry!

Slide 60

Slide 60 text

Direct save to the db class DatabaseProductPersister implements ProductPersister { private $repository; /** * @param ProductRepository $repository */ public function __construct(ProductRepository $repository) { $this->repository = $repository; } public function saveAll(ProductList $productList) { foreach ($productList as $product) { $this->repository->save($product); } } }

Slide 61

Slide 61 text

Why direct save to the db? - Drupal promises data structure won’t ever change - It’s fast - 40k products in 2 minutes - It works!

Slide 62

Slide 62 text

composer.json "require": { "php": ">=5.4.0", "symfony/dependency-injection": "~2.5", "symfony/config": "~2.5", "symfony/console": "~2.5", "drush/drush": "7.*@dev", "symfony/finder": "~2.5", "symfony/filesystem": "~2.6", "doctrine/dbal": "~2.5", "rhumsaa/uuid": "~2.8" },

Slide 63

Slide 63 text

container.php boot(); $conf['di_container'] = $kernel->getContainer();

Slide 64

Slide 64 text

database.php $databases = [ 'default' => [ 'default' => [ 'database' => $conf['di_container']->getParameter('database.name'), 'username' => $conf['di_container']->getParameter('database.username'), 'password' => $conf['di_container']->getParameter('database.password'), 'host' => $conf['di_container']->getParameter('database.host'), 'port' => $conf['di_container']->getParameter('database.port'), 'driver' => 'mysql', 'prefix' => '', ], ], ];

Slide 65

Slide 65 text

settings.php

Slide 66

Slide 66 text

depot_locator.module function get_depot_json($postcode) { global $conf; $depotEntity = $conf['di_container'] ->get('acme.depot_locator')->locateDepot($postcode); $depot = $depotEntity->toArray(); set_depot_cookies($depot); return ['depot' => $depot]; }

Slide 67

Slide 67 text

Project folder structure

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

as a CMS platform Drupal is great!

Slide 71

Slide 71 text

Cherry pick what you need! Symfony Components

Slide 72

Slide 72 text

Try it yourself! Decoupled code

Slide 73

Slide 73 text

Right tool Choose wisely!

Slide 74

Slide 74 text

https://speakerdeck.com/super_marek @super_marek

Slide 75

Slide 75 text

No content