Play PHP Like a Puzzle

Play PHP Like a Puzzle

PHP development used to involve lots of handcrafted code. Today it's more like playing a puzzle: Find the pieces that you need, put them together and voilà.

This game is about Composer: Do you know the most powerful puzzle pieces? But there's more. With PSR-7, Puli and standardized DI configuration, puzzlers have a new ace up their sleeves.

24d20907afea0f684e62d620b886af16?s=128

Bernhard Schussek

June 24, 2016
Tweet

Transcript

  1. Bernhard Schussek · webmozart.io 1/114 https://www.flickr.com/photos/emmacherry/2207748365 (CC BY-NC 2.0) Play

    PHP Like A Puzzle Bernhard Schussek (@webmozart) Dutch PHP Conference 2016, Amsterdam
  2. Bernhard Schussek · webmozart.io 3/114 Bernhard Schussek Symfony Core Developer

    since 2009
  3. Bernhard Schussek · webmozart.io 4/114 @webmozart

  4. Bernhard Schussek · webmozart.io 5/114 Bernhard Schussek Developer of Puli

    since 2014
  5. Bernhard Schussek · webmozart.io 6/114 Bernhard Schussek PHP Trainer and

    Coach Symfony Architecture Coding Practices webmozart.io
  6. Bernhard Schussek · webmozart.io 7/114 Successful Software Development

  7. Bernhard Schussek · webmozart.io 8/114 https://www.flickr.com/photos/sthomasphotos/10205212344 (CC BY-NC-ND 2.0) Speed

    of Development
  8. Bernhard Schussek · webmozart.io 9/114 https://www.flickr.com/photos/_flood_/6468131271 (CC BY-NC-ND 2.0) Quality

    of Development
  9. Bernhard Schussek · webmozart.io 10/114 Contributing Factors • Organizational Factors

    — developer skills — work/life balance — work atmosphere — … • Technical Factors — tools — ready-made solutions
  10. Bernhard Schussek · webmozart.io 11/114 https://www.flickr.com/photos/deanhochman/8519852257 (CC BY 2.0) Off-the-Shelve

    Solutions Standard Applications, SaaS, …
  11. Bernhard Schussek · webmozart.io 12/114 https://www.flickr.com/photos/52814185 (CC BY-NC-ND 2.0) Libraries

    Commercial, Open-Source
  12. Bernhard Schussek · webmozart.io 13/114 Libraries in PHP are well

    established
  13. Bernhard Schussek · webmozart.io 14/114 (since 2003) (since 2012) Distribution

    as “Packages”
  14. Bernhard Schussek · webmozart.io 15/114 Steps to Using a Package

  15. Bernhard Schussek · webmozart.io 16/114 Step 1: Search Candidates Packagist

  16. Bernhard Schussek · webmozart.io 17/114 Step 2: Research Quality Commits,

    Contributors, Activity, Tests, …
  17. Bernhard Schussek · webmozart.io 18/114 $ composer require league/tactician Using

    version ^1.0 for league/tactician ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) - Installing league/tactician (v1.0.2) Downloading: 100% Writing lock file Generating autoload files Step 3: Install in Your Project Composer
  18. Bernhard Schussek · webmozart.io 19/114 https://www.flickr.com/photos/sthomasphotos/10205212344 (CC BY-NC-ND 2.0) $speed++

  19. Bernhard Schussek · webmozart.io 20/114 Interoperable Packages

  20. Bernhard Schussek · webmozart.io 21/114 use Symfony\Component\EventDispatcher\EventDispatcher; $repository = new

    EventDispatcher(); PSR-1/4 Autoloading
  21. Bernhard Schussek · webmozart.io 22/114 use Monolog\Logger; use League\Tactician\Logger\LoggerMiddleware; $logger

    = new Logger('my_app'); $commandBus = new CommandBus([ new LoggerMiddleware($formatter, $logger), // ... ]); PSR-3 Logging
  22. Bernhard Schussek · webmozart.io 23/114 use Psr\Http\Message\ServerRequestInterface; function showPosts(ServerRequestInterface $request)

    { // ... } PSR-7 Http Messages
  23. Bernhard Schussek · webmozart.io 24/114 Reduce Coupling Between Packages

  24. Bernhard Schussek · webmozart.io 25/114 Choose the Best Package for

    your purpose
  25. Bernhard Schussek · webmozart.io 26/114 https://www.flickr.com/photos/superlekker/5917559189 (CC BY-NC 2.0)

  26. Bernhard Schussek · webmozart.io 27/114 https://www.flickr.com/photos/_flood_/6468131271 (CC BY-NC-ND 2.0) $quality++

  27. Bernhard Schussek · webmozart.io 28/114 https://www.flickr.com/photos/picturesfromwords/9539461080 (CC BY-NC 2.0) Modules

    https://www.flickr.com/photos/emmacherry/2207748365 (CC BY-NC 2.0)
  28. Bernhard Schussek · webmozart.io 29/114 Bundles Packages Modules Modules

  29. Bernhard Schussek · webmozart.io 30/114 Evolution of Packages

  30. Bernhard Schussek · webmozart.io 31/114 Plug and Play

  31. Bernhard Schussek · webmozart.io 32/114 Configuration for Standard Cases

  32. Bernhard Schussek · webmozart.io 33/114 Custom Code for Special Cases

  33. Bernhard Schussek · webmozart.io 34/114 Abilities of Module Systems •

    DI container configuration • database configuration • MVC support • file loading • web assets • migrations (automated upgrades) • …
  34. Bernhard Schussek · webmozart.io 35/114 https://www.flickr.com/photos/sthomasphotos/10205212344 (CC BY-NC-ND 2.0) $speed++

  35. Bernhard Schussek · webmozart.io 36/114 Modules are Not Interoperable

  36. Bernhard Schussek · webmozart.io 37/114 Need to Provide Bridges

  37. Bernhard Schussek · webmozart.io 38/114

  38. Bernhard Schussek · webmozart.io 39/114 Worse: Different Modules Maintained by

    Different Teams
  39. Bernhard Schussek · webmozart.io 40/114 Packagist Libraries Symfony Bundles Laravel

    Packages Drupal Modules Silex Providers
  40. Bernhard Schussek · webmozart.io 41/114 https://www.flickr.com/photos/rodenberger/12465588223 (CC BY 2.0) $quality--

  41. Bernhard Schussek · webmozart.io 42/114 https://www.flickr.com/photos/aftab/4153613907 (CC BY-NC 2.0) $speed--

  42. Bernhard Schussek · webmozart.io 43/114 https://www.flickr.com/photos/picturesfromwords/9539461080 (CC BY-NC 2.0) Goal:

    Interoperable Modules for PHP https://www.flickr.com/photos/emmacherry/2207748365 (CC BY-NC 2.0)
  43. Bernhard Schussek · webmozart.io 44/114 Movement N° 1: PSR-11 Standardized

    DI Configuration
  44. Bernhard Schussek · webmozart.io 45/114 David Négrier Matthieu Napoli

  45. Bernhard Schussek · webmozart.io 46/114 class MyServiceProvider implements ServiceProviderInterface {

    public function getServices() { return [ 'service_name' => function () { // create service } ]; } } Providing Services from Modules
  46. Bernhard Schussek · webmozart.io 47/114 'post_repository' => function () {

    return new PDOPostRepository(); } $container = new Pimple(); $container->addServiceProvider(new PDOServiceProvider()) $container->addServiceProvider(new MyServiceProvider()); $repo = $container->get('post_repository'); Example: PDOPostRepository
  47. Bernhard Schussek · webmozart.io 48/114 'post_repository' => function (ContainerInterface $c)

    { return new PDOPostRepository( $c->get('pdo') ); } Service Injection
  48. Bernhard Schussek · webmozart.io 49/114 'pdo' => function ($c, callable

    $getPrevious) { return new PDOLogger( $getPrevious(), $c->get('logger') ); } Service Decoration
  49. Bernhard Schussek · webmozart.io 50/114 'pdo.options' => function ($c, callable

    $getPrevious) { return array_replace($getPrevious(), [ 'database_name' => 'mydb', ]); } Service Parameters
  50. Bernhard Schussek · webmozart.io 51/114 'twig_extensions' => function ($c, callable

    $getPrevious return array_merge($getPrevious(), [ new MyTwigExtension(), ]); } Service Lists
  51. Bernhard Schussek · webmozart.io 52/114 PostRepository::class => function (ContainerInterface $c

    return new PDOPostRepository( $c->get(PDO::class) ); } $repo = $container->get(PostRepository::class); Interfaces as Service Names
  52. Bernhard Schussek · webmozart.io 53/114 Benefits of the Proposal

  53. Bernhard Schussek · webmozart.io 54/114 Simple

  54. Bernhard Schussek · webmozart.io 55/114 Interoperable

  55. Bernhard Schussek · webmozart.io 56/114 Tested with Existing Containers Symfony

    DI Adapter Laravel Adapter Pimple fork PHP-DI
  56. Bernhard Schussek · webmozart.io 57/114 Tech Demos Doctrine DBAL Module

    Doctrine Annotations Module Glide Module PSR-6 to Doctrine cache bridge module Slim-framework Module Stash Module Stratigility Module Twig Module Whoops PSR-7 Middleware Module
  57. Bernhard Schussek · webmozart.io 58/114 Simple

  58. Bernhard Schussek · webmozart.io 59/114 FIG Discussion http://bit.ly/28Xgc1D

  59. Bernhard Schussek · webmozart.io 60/114 Movement N° 2 Puli Modules

  60. Bernhard Schussek · webmozart.io 61/114 Bundles Packages Modules Modules ✘

    ✘ ✘ ✘
  61. Bernhard Schussek · webmozart.io 62/114 What is a Puli Module?

  62. Bernhard Schussek · webmozart.io 63/114 PHP Library ✔

  63. Bernhard Schussek · webmozart.io 64/114 Symfony Bundle✔

  64. Bernhard Schussek · webmozart.io 65/114 Your Application✔

  65. Bernhard Schussek · webmozart.io 66/114 PHP Code Resource Files puli.json

    Module Configuration composer.json Installation Instructions Puli Module
  66. Bernhard Schussek · webmozart.io 67/114 Puli is Not the Next

    Composer
  67. Bernhard Schussek · webmozart.io 68/114 3 Core Foci Accessing Resources

    Asset URLs Discovery
  68. Bernhard Schussek · webmozart.io 69/114 Accessing Resources

  69. Bernhard Schussek · webmozart.io 70/114 $twig->render('/batman/blog/views/index.html.twig'); Accessing Resources

  70. Bernhard Schussek · webmozart.io 71/114 Puli Path /batman/blog/views/index.html.twig

  71. Bernhard Schussek · webmozart.io 72/114 batman/blog /res /batman/blog/views/index.html.twig Path Prefix

    Physical Location Path Mapping
  72. Bernhard Schussek · webmozart.io 73/114 /res/views/index.html.twig Resolved Path /batman/blog/views/index.html.twig Physical

    Location Path Prefix batman/blog
  73. Bernhard Schussek · webmozart.io 74/114 puli.json (in the batman/blog module)

    { "resources": { "/batman/blog": "res" } } preview
  74. Bernhard Schussek · webmozart.io 75/114 {% extend '/batman/blog/views/layout.html' %} Usage

    Example: Templates
  75. Bernhard Schussek · webmozart.io 76/114 {% include '../sidebar.html' %} {%

    use 'macros.html' %} Relative Paths
  76. Bernhard Schussek · webmozart.io 77/114 imports: - /batman/blog/config.yml - app.yml

    Usage Example: Configuration
  77. Bernhard Schussek · webmozart.io 78/114 Access Resources in Any Module

  78. Bernhard Schussek · webmozart.io 79/114 and Your Application

  79. Bernhard Schussek · webmozart.io 80/114 $html = $this->render('/app/views/post.html'); Resource Access

    puli.json (in your application) { "resources": { "/app": "res" } }
  80. Bernhard Schussek · webmozart.io 81/114 Web Assets + URLS (concept

    phase)
  81. Bernhard Schussek · webmozart.io 82/114 PHP Code Resource Files Publish

    Assets public/ css/ style.min.css images/ background.png Module Document Root PHP Code Resource Files Module
  82. Bernhard Schussek · webmozart.io 83/114 Repository JSON URL Generator JSON

    Gulp/Grunt + puli.js Asset Pipeline
  83. Bernhard Schussek · webmozart.io 84/114 Gulpfile.js var gulp = require('gulp'),

    puli = require('puli').load(); gulp.task('vendor', function () { puli.src('/batman/blog/public/**/*') .dest('res/public/blog'); });
  84. Bernhard Schussek · webmozart.io 85/114 Generate URLs var urls =

    require('puli-urls'); gulp.task('vendor', function () { puli.src('/batman/blog/public/**/*') .pipe(urls()) .dest('res/public/blog') .pipe(urls.mapping()) .dest('.puli/urls.json'); });
  85. Bernhard Schussek · webmozart.io 86/114 $generator = new JsonUrlGenerator('.puli/urls.json'); $generator->generateUrl('/acme/blog/public/style.css');

    // => /blog/style.css Print URLs in PHP
  86. Bernhard Schussek · webmozart.io 87/114 <img src="{{ asset_url('/batman/blog/public/bg.png') }}" <link

    href="{{ asset_url('../public/css/style.css') }}" Print URLs in Templates
  87. Bernhard Schussek · webmozart.io 88/114 body { background-image: asset_url("/res/public/bg.png"); //

    Relative paths background-image: asset_url("bg.png"); } Print URLs in CSS
  88. Bernhard Schussek · webmozart.io 89/114 Discovery “Let Modules Find Each

    Other“
  89. Bernhard Schussek · webmozart.io 90/114 my/app-or-module routes.php vendor/router Router

  90. Bernhard Schussek · webmozart.io 91/114 my/app-or-module messages.de.xlf vendor/translator Translator

  91. Bernhard Schussek · webmozart.io 92/114 my/app-or-module BlogPost.xml vendor/orm ObjectMapper

  92. Bernhard Schussek · webmozart.io 93/114 my/app-or-module vendor/di-container Container MyServiceProvider

  93. Bernhard Schussek · webmozart.io 94/114 { "provide": { "/app/routing.yml": "symfony/routing-yml",

    "/app/trans/*.xlf": "league/translation-xlf", "My\\AppServiceProvider": "Psr\\DI\\ServiceProviderInterface" } } puli.json (of your application or module)
  94. Bernhard Schussek · webmozart.io 95/114 $router = new Router($discovery); $translator

    = new Translator($discovery); $container = new Container($discovery); // ... Using the Modules
  95. Bernhard Schussek · webmozart.io 96/114 https://www.flickr.com/photos/sthomasphotos/10205212344 (CC BY-NC-ND 2.0) $speed++

  96. Bernhard Schussek · webmozart.io 97/114 https://commons.wikimedia.org/wiki/File:View_from_Volcano_Pacaya,_Guatemala.jpg (CC BY-SA 4.0) Outlook

  97. Bernhard Schussek · webmozart.io 98/114 Interoperable Modules Have Potential

  98. Bernhard Schussek · webmozart.io 99/114 for Sharing Modules Between Frameworks

  99. Bernhard Schussek · webmozart.io 100/114 https://www.flickr.com/photos/_flood_/6468131271 (CC BY-NC-ND 2.0) $quality++

  100. Bernhard Schussek · webmozart.io 101/114 for Enabling Module-Based Frameworks (vs.

    Component-Based)
  101. Bernhard Schussek · webmozart.io 102/114 for Writing Apps Without A

    Framework
  102. Bernhard Schussek · webmozart.io 103/114 $container = new Container($discovery); $router

    = $container->get(Router::class); $controller = $router->getController('/hello/world'); $response = $controller->handle($request); Applications with Puli and PSR-11
  103. Bernhard Schussek · webmozart.io 104/114 Status of PSR-11

  104. Bernhard Schussek · webmozart.io 105/114 Soon to enter Entrance Vote

  105. Bernhard Schussek · webmozart.io 106/114 Status of Puli

  106. Bernhard Schussek · webmozart.io 107/114 1.0.0-beta10

  107. Bernhard Schussek · webmozart.io 108/114 Extensive Documentation docs.puli.io

  108. Bernhard Schussek · webmozart.io 109/114 Successfully in Production

  109. Bernhard Schussek · webmozart.io 110/114 Working Symfony Bundle puli/symfony-bundle

  110. Bernhard Schussek · webmozart.io 111/114 Symfony Distribution puli/symfony-puli-edition

  111. Bernhard Schussek · webmozart.io 113/114 Feedback Wanted puli.io Twitter: @PuliPHP

    Chat: gitter.im/puli/issues
  112. Bernhard Schussek · webmozart.io 114/114 Get Involved! github.com/container-interop/service-provider github.com/puli/issues

  113. Bernhard Schussek · webmozart.io 115/114 @webmozart

  114. Bernhard Schussek · webmozart.io 117/114 Questions? Questions? Bernhard Schussek Bernhard

    Schussek @webmozart @webmozart https://www.flickr.com/photos/puliarfanita/7167723539 (CC BY 2.0) webmozart.io