Slide 1

Slide 1 text

Modernising the Legacy Marek Matulka UK

Slide 2

Slide 2 text

Marek Matulka Software Engineer SensioLabs UK @super_marek

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Legacy

Slide 5

Slide 5 text

What is legacy code? - old code - poor quality code - no environments - no error logging (or excessive logging) - no tests

Slide 6

Slide 6 text

It’s too hard - understand the logic - add new features - debug - fix bugs - adopt new technologies

Slide 7

Slide 7 text

How do we get out of legacy?

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Example

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Start with the goal

Slide 16

Slide 16 text

Why? Business goal

Slide 17

Slide 17 text

Why? Who? Who? Actors

Slide 18

Slide 18 text

Why? Who? Who? How? How? How? Impacts

Slide 19

Slide 19 text

Why? Who? Who? How? How? How? What? What? What? What? What? What? Deliverables

Slide 20

Slide 20 text

Why? Who? Who? How? How? How? What? What? What? What? What? What? Prioritise

Slide 21

Slide 21 text

Goal: Reduce drop-outs by 10% In order to see the total cost of order As a shopper I want to see postage cost Example Why? Who? How? What?

Slide 22

Slide 22 text

Write scenarios Feature: Shopper can see postage cost In order to see the total cost of order As a shopper I want to see a postage cost Scenario: Shopper can see a postage cost Given I have added a "Clean Code" book priced £34.50 to the basket And the postage cost for one book is £3.50 When I check the basket Then I should see the postage cost of £3.50 And the total of the basket should be £38.00

Slide 23

Slide 23 text

Implement scenarios

Slide 24

Slide 24 text

Given-When-Then Feature: Shopper can see postage cost In order to see the total cost of order As a shopper I want to see a postage cost Scenario: Shopper can see a postage cost Given I have added a "Clean Code" book priced £34.50 to the basket And the postage cost for one book is £3.50 When I check the basket Then I should see the postage cost of £3.50 And the total of the basket should be £38.00

Slide 25

Slide 25 text

(everything?) What to test?

Slide 26

Slide 26 text

Unit tests Acceptance tests Integration tests End-to-end

Slide 27

Slide 27 text

Unit tests Acceptance tests Integration tests End-to-end Manual Automated

Slide 28

Slide 28 text

Unit tests Acceptance tests Integration tests End-to-end Slowest Fastest

Slide 29

Slide 29 text

Why test?

Slide 30

Slide 30 text

Why test? - code level documentation - features described through examples - clean code - you’re not afraid to change your code - peace of mind

Slide 31

Slide 31 text

Why test? delivered features project’s life

Slide 32

Slide 32 text

Why use Symfony components?

Slide 33

Slide 33 text

Symfony Components The standard foundation on which the best PHP applications are built. - fully tested - decoupled - reusable - de-facto standard

Slide 34

Slide 34 text

Symfony Components Build a micro framework. Use components you need in your application.

Slide 35

Slide 35 text

standardised way to install components Composer

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

composer.json { "require": { "php": ">=5.6.0" }, "require-dev": { "phpspec/phpspec": "~2.0" }, "config": { "bin-dir": "bin" }, "autoload": {"psr-0": {"": "src"}} }

Slide 38

Slide 38 text

$ sudo dnf install composer

Slide 39

Slide 39 text

$ curl -sS https://getcomposer.org/installer | php

Slide 40

Slide 40 text

$ composer install

Slide 41

Slide 41 text

$ composer update

Slide 42

Slide 42 text

$ composer update package/name

Slide 43

Slide 43 text

$ composer require package/name

Slide 44

Slide 44 text

symfony/dependency-injection Dependency Injection

Slide 45

Slide 45 text

Dependency Injection In software engineering, dependency injection is a software design pattern that implements inversion of control for resolving dependencies. Dependency injection means giving an object its instance variables. Really. That's it. https://en.wikipedia.org/wiki/Dependency_injection

Slide 46

Slide 46 text

Dependency Injection PostageCalculator PostageRepository

Slide 47

Slide 47 text

Dependency Inversion PostageCalculator PostageRepository

Slide 48

Slide 48 text

Dependency Inversion PostageCalculator PostageRepository PostageSqliteRepository

Slide 49

Slide 49 text

Dependency Inversion PostageCalculator PostageRepository PostageSqliteRepository PostageDoctrineRepository

Slide 50

Slide 50 text

DI as a boundary Legacy code Quality de- coupled code DI

Slide 51

Slide 51 text

Install dependency injection $ composer require symfony/dependency-injection $ composer require symfony/config

Slide 52

Slide 52 text

Create container.php load('services.yml'); $container->compile();

Slide 53

Slide 53 text

Create container.php load('services.yml'); $container->compile();

Slide 54

Slide 54 text

Create container.php load('services.yml'); $container->compile();

Slide 55

Slide 55 text

Create container.php load('services.yml'); $container->compile();

Slide 56

Slide 56 text

Create container.php load('services.yml'); $container->compile();

Slide 57

Slide 57 text

Create container.php load('services.yml'); $container->compile();

Slide 58

Slide 58 text

# app/config/services.yml services: acme.postage.repository: class: Acme\Infrastructure\Sqlite\PostageSqliteRepository acme.postage.calculator: class: Acme\Basket\PostageCalculator arguments: - @acme.postage.repository Create services.yml

Slide 59

Slide 59 text

# app/config/services.yml services: acme.postage.repository: class: Acme\Infrastructure\Sqlite\PostageSqliteRepository acme.postage.calculator: class: Acme\Basket\PostageCalculator arguments: - @acme.postage.repository Create services.yml

Slide 60

Slide 60 text

# app/config/services.yml services: acme.postage.repository: class: Acme\Infrastructure\Sqlite\PostageSqliteRepository acme.postage.calculator: class: Acme\Basket\PostageCalculator arguments: - @acme.postage.repository Create services.yml

Slide 61

Slide 61 text

Use container require '../_inc/db.php'; require '../app/container.php'; // ... $postageCalculator = $container->get('acme.postage.calculator'); if ($postage = $postageCalculator->calculateForBasket($_SESSION['basket'])) { $postage = $postage->toView(); echo sprintf("

Estimated postage for %d item(s): £%1.02f.

", $postage['quantity'], $postage['price'] ); } // ...

Slide 62

Slide 62 text

Use container require '../_inc/db.php'; require '../app/container.php'; // ... $postageCalculator = $container->get('acme.postage.calculator'); if ($postage = $postageCalculator->calculateForBasket($_SESSION['basket'])) { $postage = $postage->toView(); echo sprintf("

Estimated postage for %d item(s): £%1.02f.

", $postage['quantity'], $postage['price'] ); } // ...

Slide 63

Slide 63 text

Use container require '../_inc/db.php'; require '../app/container.php'; // ... $postageCalculator = $container->get('acme.postage.calculator'); if ($postage = $postageCalculator->calculateForBasket($_SESSION['basket'])) { $postage = $postage->toView(); echo sprintf("

Estimated postage for %d item(s): £%1.02f.

", $postage['quantity'], $postage['price'] ); } // ...

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

Application Kernel

Slide 66

Slide 66 text

container.php load('services.yml'); $container->compile();

Slide 67

Slide 67 text

container.php load('services.yml'); $container->compile();

Slide 68

Slide 68 text

container = new ContainerBuilder(); $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/app/config')); $loader->load('services.yml'); $this->container->compile(); } public function getContainer() { return $this->container; } }

Slide 69

Slide 69 text

container.php boot(); $container = $kernel->getContainer();

Slide 70

Slide 70 text

Singleton Kernel

Slide 71

Slide 71 text

boot(); } return static::$instance; } protected function __construct() {} private function __clone() {} private function __wakeup() {} }

Slide 72

Slide 72 text

boot(); } return static::$instance; } protected function __construct() {} private function __clone() {} private function __wakeup() {} }

Slide 73

Slide 73 text

$di = Kernel::getInstance()->getContainer(); $service = $di->get('acme.service.name');

Slide 74

Slide 74 text

Dependency Injection with Drupal 7?

Slide 75

Slide 75 text

container.php boot(); variable_set('di_container', $kernel->getContainer());

Slide 76

Slide 76 text

container.php boot(); variable_set('di_container', $kernel->getContainer());

Slide 77

Slide 77 text

public/sites/default/settings.php

Slide 78

Slide 78 text

depot_locator.module get('acme.depot_locator')->locateDepot($postcode); $depot = $depotEntity->toArray(); set_depot_cookies($depot); return ['depot' => $depot]; }

Slide 79

Slide 79 text

depot_locator.module get('acme.depot_locator')->locateDepot($postcode); $depot = $depotEntity->toArray(); set_depot_cookies($depot); return ['depot' => $depot]; }

Slide 80

Slide 80 text

symfony/console Add console commands

Slide 81

Slide 81 text

Install console component $ composer require symfony/console

Slide 82

Slide 82 text

class LocateDepotCommand extends Command { /** @var DepotLocator */ private $depotLocator; public function __construct(DepotLocator $depotLocator) { $this->depotLocator = $depotLocator; parent::__construct(); } protected function configure() { $this->setName('depot:locate') ->addArgument('postcode', InputArgument::REQUIRED); } protected function execute(InputInterface $input, OutputInterface $output) { $this->depotLocator->locateDepot($input->getArgument('postcode')); $output->writeln('Nearest depot is located at: ' . $depot->getAddress()); } }

Slide 83

Slide 83 text

app/console #!/usr/bin/php addCommands([ $container->get('acme.command.depot_locate'), ]); $application->run();

Slide 84

Slide 84 text

$ app/console depot:locate wc1a1dg

Slide 85

Slide 85 text

twig/twig Twig

Slide 86

Slide 86 text

What is twig?

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

Install twig $ composer require twig/twig

Slide 90

Slide 90 text

How to use twig? render('index.html', ['variable' => 'value']);

Slide 91

Slide 91 text

# app/config/services.yml parameters: twig.paths: - app/Resources/views services: twig.template_loader: class: Twig_Loader_Filesystem arguments: - %twig.paths% twig.renderer: class: Twig_Environment arguments: - @twig.template_loader Enabling twig in DI container

Slide 92

Slide 92 text

# app/config/services.yml parameters: twig.paths: - app/Resources/views services: twig.template_loader: class: Twig_Loader_Filesystem arguments: - %twig.paths% twig.renderer: class: Twig_Environment arguments: - @twig.template_loader Enabling twig in DI container

Slide 93

Slide 93 text

# app/config/services.yml parameters: twig.paths: - app/Resources/views services: twig.template_loader: class: Twig_Loader_Filesystem arguments: - %twig.paths% twig.renderer: class: Twig_Environment arguments: - @twig.template_loader Enabling twig in DI container

Slide 94

Slide 94 text

# app/config/services.yml parameters: twig.paths: - app/Resources/views services: twig.template_loader: class: Twig_Loader_Filesystem arguments: - %twig.paths% twig.renderer: class: Twig_Environment arguments: - @twig.template_loader Enabling twig in DI container

Slide 95

Slide 95 text

Use twig $basketViewData = $container->get('acme.basket.view') ->toView($_SESSION['basket']); $basketView = $container->get('twig.renderer') ->render( 'basket.twig.html', $basketViewData ); echo $basketView;

Slide 96

Slide 96 text

symfony/http-foundation Decouple from global state

Slide 97

Slide 97 text

Http application’s flow Controller Request Response

Slide 98

Slide 98 text

Install HttpFoundation component $ composer require symfony/http-foundation

Slide 99

Slide 99 text

De-coupling from global state # /public/basket.php get('acme.basket_view.controller') ->viewAction($request); $response->send();

Slide 100

Slide 100 text

De-coupling from global state # /public/basket.php get('acme.basket_view.controller') ->viewAction($request); $response->send();

Slide 101

Slide 101 text

De-coupling from global state # /public/basket.php get('acme.basket_view.controller') ->viewAction($request); $response->send();

Slide 102

Slide 102 text

De-coupling from global state # /public/basket.php get('acme.basket_view.controller') ->viewAction($request); $response->send();

Slide 103

Slide 103 text

Going forward

Slide 104

Slide 104 text

Try it yourself! https://github.com/mareg/legacy-summercamp-2015 - four exercises: - add dependency injection - add twig - decouple from global state - controller as service

Slide 105

Slide 105 text

Questions? Slides: speakerdeck.com/super_marek Feedback: joind.in/talk/view/15567 Connect on twitter: @super_marek