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

Legacy

Slide 4

Slide 4 text

What is legacy code? - someone else’s code - your old code - an old framework you don’t know - or too coupled to the framework - built over years

Slide 5

Slide 5 text

What is legacy code? Old code where... - most of it is procedural - no namespaces used - no code convention - no separation of concerns

Slide 6

Slide 6 text

What is legacy code? Old code where... - PHP mixed with HTML, CSS - overused Global variables - collection of include files - *.inc files in public folder

Slide 7

Slide 7 text

What is legacy code? Old code where... - no concept of environments - no error reporting - no logging - no version control system used

Slide 8

Slide 8 text

What is legacy code? Any code... - without tests

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

How do we get out of legacy?

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Start with the goal

Slide 15

Slide 15 text

Why? Business goal

Slide 16

Slide 16 text

Why? Who? Who? Actors

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 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 21

Slide 21 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 22

Slide 22 text

Implement scenarios

Slide 23

Slide 23 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 24

Slide 24 text

(everything?) What to test?

Slide 25

Slide 25 text

Unit tests Acceptance tests Integration tests End-to-end

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Why test?

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

Why test? delivered features project’s life

Slide 31

Slide 31 text

Why use Symfony components?

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

standarised way to install components Composer

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

$ sudo dnf install composer

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

$ composer install

Slide 40

Slide 40 text

$ composer update

Slide 41

Slide 41 text

$ composer update package/name

Slide 42

Slide 42 text

$ composer require package/name

Slide 43

Slide 43 text

symfony/dependency-injection Dependency Injection

Slide 44

Slide 44 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 45

Slide 45 text

Dependency Injection PostageCalculator PostageRepository

Slide 46

Slide 46 text

Dependency Inversion PostageCalculator PostageRepository

Slide 47

Slide 47 text

Dependency Inversion PostageCalculator PostageRepository PostageSqliteRepository

Slide 48

Slide 48 text

Dependency Inversion PostageCalculator PostageRepository PostageSqliteRepository PostageDoctrineRepository

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

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

# 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 57

Slide 57 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 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

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 60

Slide 60 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 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

No content

Slide 63

Slide 63 text

Application Kernel

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

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

Slide 66

Slide 66 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 67

Slide 67 text

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

Slide 68

Slide 68 text

Singleton Kernel

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

Dependency Injection with Drupal 7?

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

public/sites/default/settings.php

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

symfony/console Add console commands

Slide 78

Slide 78 text

Install console component $ composer require symfony/console

Slide 79

Slide 79 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 80

Slide 80 text

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

Slide 81

Slide 81 text

$ app/console depot:locate wc1a1dg

Slide 82

Slide 82 text

twig/twig Twig

Slide 83

Slide 83 text

What is twig?

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

Install twig $ composer require twig/twig

Slide 87

Slide 87 text

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

Slide 88

Slide 88 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 89

Slide 89 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 90

Slide 90 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 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

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

Slide 93

Slide 93 text

symfony/http-foundation Decouple from global state

Slide 94

Slide 94 text

Http application’s flow Controller Request Response

Slide 95

Slide 95 text

Install HttpFoundation component $ composer require symfony/http-foundation

Slide 96

Slide 96 text

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

Slide 97

Slide 97 text

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

Slide 98

Slide 98 text

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

Slide 99

Slide 99 text

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

Slide 100

Slide 100 text

Going forward

Slide 101

Slide 101 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 102

Slide 102 text

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