The Wonderful World of Composer and ZF2

F5dfeeef276fcfd4751f4063487a5a3f?s=47 weaverryan
October 25, 2012

The Wonderful World of Composer and ZF2

Composer is changing the landscape of PHP, bringing new tools and new unity to custom PHP developers (e.g. ZF2, Symfony user and others) and the rest of our giant community (Drupal, Wordpress, etc).

In this talk, we'll look at why sharing has historically been so hard in PHP and why it's now so easy. We'll talk about autoloading and then dive into composer - learning how to install it, find packages, download those packages, and handle autoloading. You'll see how effortless it is to use outside libraries, whether you're in a framework or something entirely different (e.g. Drupal).

We'll also talk about how to bootstrap a ZF2 project with Composer and how to use individual ZF2 components outside of the framework.

F5dfeeef276fcfd4751f4063487a5a3f?s=128

weaverryan

October 25, 2012
Tweet

Transcript

  1. The Wonderful World of Composer & ZF2 by your friend:

    Ryan Weaver @weaverryan Thursday, October 25, 12
  2. Who is this dude? • The Symfony “Docs” guy •

    KnpLabs US - Symfony & Behat consulting, training, and Kumbaya • Writer for KnpUniversity.com screencasts • Husband of the much more talented @leannapelham knplabs.com github.com/weaverryan @weaverryan Thursday, October 25, 12
  3. Intro Life before components (The Desert of the Real) Copyright

    by Moyan Brenn http://www.flickr.com/aigle_dore Thursday, October 25, 12
  4. Zend Framework 2 consists of 48 individual components Thursday, October

    25, 12
  5. Components are available via Composer Thursday, October 25, 12

  6. Snooze... http://www.flickr.com/photos/fiatlux/81268407/ Thursday, October 25, 12

  7. Who Cares? Thursday, October 25, 12

  8. We Suck at Sharing and that sucks for you @weaverryan

    1 Thursday, October 25, 12
  9. Including External Libraries is Depressing Thursday, October 25, 12

  10. The Big Bummer :( •@weaverryan • How do I autoload

    their files? • Does their library depend on anything else? • How do I even store their files in my project? Thursday, October 25, 12
  11. Include a Zend Framework 1 component in symfony1 Thursday, October

    25, 12
  12. Thursday, October 25, 12

  13. manually download the library and commit it into your project

    Thursday, October 25, 12
  14. if you don’t want the WHOLE library, carefully delete everything

    except the dependent components Thursday, October 25, 12
  15. autoloading is completely custom ... blah gross! Thursday, October 25,

    12
  16. Components are the key to mastering your framework @weaverryan 2

    Thursday, October 25, 12
  17. If PHP is big, we’ll thrive If PHP is small,

    we’ll die @weaverryan 3 Thursday, October 25, 12
  18. Communities @weaverryan PHP is Huge! Right? http://www.flickr.com/photos/kitty-kat/ Thursday, October 25,

    12
  19. @weaverryan PHP > Ruby Thursday, October 25, 12

  20. Fragmentation @weaverryan But fragmentation makes us tiny, isolated, and misguided

    trend-setters http://www.flickr.com/photos/slpunk99/7329609744 Thursday, October 25, 12
  21. PHP frameworks < Rails @weaverryan Thursday, October 25, 12

  22. I don’t want a damned CakePHP Plugin! @weaverryan CakePHP CodeIgniter

    Thursday, October 25, 12
  23. Fragmentation @weaverryan • More information we have to know •

    Difficult to hire • Disjointed forums, StackOverflow • Interoperability? What’s that? Thursday, October 25, 12
  24. Components are shareable across all of PHP Thursday, October 25,

    12
  25. Act 1 Making Sharing Sexy Thursday, October 25, 12

  26. PHP Framework Interoperability Group http://www.php-fig.org/ Thursday, October 25, 12

  27. The United Nations of PHP Thursday, October 25, 12

  28. (A) The Problem of Autoloading Thursday, October 25, 12

  29. My Autoloader doesn’t like your PHPs @weaverryan • The symfony1

    autoloader doesn’t know where ZF1 classes live. The Zf1 autoloader doesn’t know where symfony1 classes live • Each library has its own autoloader that you must discover, configure and use Thursday, October 25, 12
  30. like a town where every store has its own currency

    Thursday, October 25, 12
  31. From The Mountain: PSR-0 Class Naming Conventions @weaverryan “Thou shalt

    name your classes by following a predictable pattern” Thursday, October 25, 12
  32. class: sfRequest path: lib/vendor/symfony/???idk Thursday, October 25, 12

  33. class: Symfony\Component\HttpFoundation\Request path: vendor/symfony/src/Symfony/Component/ HttpFoundation/Request.php Thursday, October 25, 12

  34. use Symfony\Component\ClassLoader\UniversalClassLoader; $loader = new UniversalClassLoader(); $loader->registerNamespaces(array( 'Zend' => __DIR__.'/path/to/zf'

    )); $loader->register(); Thursday, October 25, 12
  35. use Symfony\Component\ClassLoader\UniversalClassLoader; $loader = new UniversalClassLoader(); $loader->registerNamespaces(array( 'Zend' => __DIR__.'/path/to/zf'

    )); $loader->register(); ** This uses the Symfony2 autoloader. But all PSR-0 autoloaders are really the same. Composer comes with its own. Thursday, October 25, 12
  36. (B) Managing Vendors in your project Thursday, October 25, 12

  37. Composer Thursday, October 25, 12

  38. Act 2 Using Composer http://KnpUniversity.com/screencast/composer Thursday, October 25, 12

  39. Composer in 5 easy steps 1. Download composer.phar 2. Find

    package name & version 3. Create composer.json configuration 4. Download via composer install 5. Require autoload.php... then go crazy! Thursday, October 25, 12
  40. Composer in 5 easy steps 1. Download composer.phar 2. Find

    package name & version 3. Create composer.json configuration 4. Download via composer install 5. Require autoload.php... then go crazy! Thursday, October 25, 12
  41. curl -s https://getcomposer.org/installer | php Download Composer.phar Source: http://getcomposer.org/download/ Thursday,

    October 25, 12
  42. This downloads an executable file: composer.phar Thursday, October 25, 12

  43. Execute it... And it’ll list the available commands Thursday, October

    25, 12
  44. Composer in 5 easy steps 1. Download composer.phar 2. Find

    package name & version 3. Create composer.json configuration 4. Download via composer install 5. Require autoload.php... then go crazy! Thursday, October 25, 12
  45. Packagist.org @weaverryan • Composer installs packages • A package is

    a directory that contains anything (usually PHP classes) • Every package has a unique name • The mega-repository for packages is http://packagist.org Thursday, October 25, 12
  46. Search for something Thursday, October 25, 12

  47. Find the name of the package Thursday, October 25, 12

  48. Find the right version ** Bleeding edge (unstable) ** Development

    branch (likely unstable) ** Latest stable version (safest bet!) Thursday, October 25, 12
  49. knplabs/knp-menu: v1.1.2 Thursday, October 25, 12

  50. Composer in 5 easy steps 1. Download composer.phar 2. Find

    package name & version 3. Create composer.json configuration 4. Download via composer install 5. Require autoload.php... then go crazy! Thursday, October 25, 12
  51. { "require": { "knplabs/knp-menu": "v1.1.2" } } Create composer.json Thursday,

    October 25, 12
  52. Or call php composer.phar init to create the composer.json interactively

    Thursday, October 25, 12
  53. Composer in 5 easy steps 1. Download composer.phar 2. Find

    package name & version 3. Create composer.json configuration 4. Download via composer install 5. Require autoload.php... then go crazy! Thursday, October 25, 12
  54. So Far we have... @weaverryan • The composer.phar executable •

    A composer.json with the name and version of a package • ... that’s all! Thursday, October 25, 12
  55. Now we tell Composer to read composer.json and download our

    libraries Thursday, October 25, 12
  56. php composer.phar install Thursday, October 25, 12

  57. Thursday, October 25, 12

  58. Hallo Install! The “install” command does 2 things 1. Downloads

    all the required libraries into the vendor/ directory 2. Generates some files to making autoloading effortless Thursday, October 25, 12
  59. Composer in 5 easy steps 1. Download composer.phar 2. Find

    package name & version 3. Create composer.json configuration 4. Download via composer install 5. Require autoload.php... then go crazy! Thursday, October 25, 12
  60. use Knp\Menu\MenuFactory; use Knp\Menu\Renderer\ListRenderer; $factory = new MenuFactory(); $menu =

    $factory->createItem('My menu'); $menu->addChild('Home', array('uri' => '/')); $menu->addChild('Comments', array( 'uri' => '/comments' ))->setAttribute('class', 'comments'); $renderer = new ListRenderer(); echo $renderer->render($menu); require 'vendor/autoload.php'; That’s the magic!!! Thursday, October 25, 12
  61. Thursday, October 25, 12

  62. Thursday, October 25, 12

  63. Act 3 Composer and Zend Framework 2 Thursday, October 25,

    12
  64. Zend Framework 2 • Zend Framework 2 is a group

    of components (i.e. normal PHP libraries) • If you’re using Zend Framework, you actually have a skeleton that is built to leverage these components Thursday, October 25, 12
  65. https://github.com/zendframework/ZendSkeletonApplication Thursday, October 25, 12

  66. php composer.phar create-project A special one-time-use way to “clone” a

    project skeleton Thursday, October 25, 12
  67. Typical Usage php composer.phar create-project package/name local/dir Thursday, October 25,

    12
  68. Typical Usage php composer.phar create-project package/name local/dir Don’t forget to

    download or copy composer.phar to your current directory Thursday, October 25, 12
  69. Typical Usage php composer.phar create-project package/name local/dir The command name

    Thursday, October 25, 12
  70. Typical Usage php composer.phar create-project package/name local/dir use php composer.phar

    create-project --help to learn about any command Thursday, October 25, 12
  71. Typical Usage php composer.phar create-project package/name local/dir The name of

    the package on Packagist.org Thursday, October 25, 12
  72. Typical Usage php composer.phar create-project package/name local/dir Local path to

    download the project Thursday, October 25, 12
  73. php composer.phar create-project \ The ZF2 Skeleton App Thursday, October

    25, 12
  74. php composer.phar create-project \ The ZF2 Skeleton App ... just

    breaking a long shell command onto multiple lines... Thursday, October 25, 12
  75. php composer.phar create-project \ --repository-url="http://packages.zendframework.com" \ The ZF2 Skeleton App

    ZF2 Packages are stored on their own private “Packagist” Thursday, October 25, 12
  76. The ZF2 Skeleton App php composer.phar create-project \ --repository-url="http://packages.zendframework.com" \

    zendframework/skeleton-application \ The name of the package on packages.zendframework.com Thursday, October 25, 12
  77. The ZF2 Skeleton App php composer.phar create-project \ --repository-url="http://packages.zendframework.com" \

    zendframework/skeleton-application \ zf2 ... and our local directory name Thursday, October 25, 12
  78. Thursday, October 25, 12

  79. create-project • Clones the project • Runs php composer.phar install

    to download zendframework/zendframework into the vendor/ directory • You now have a project skeleton that uses Composer to bring in Zf2 #winning Thursday, October 25, 12
  80. Now, how can I bring in other libraries? Thursday, October

    25, 12
  81. { "name": "zendframework/skeleton-application", "description": "Skeleton Application for ZF2", "license": "BSD-3-Clause",

    "keywords": [ "framework", "zf2" ], "homepage": "http://framework.zend.com/", "require": { "php": ">=5.3.3", "zendframework/zendframework": "2.*", "knplabs/knp-menu": "v1.1.2" } } Update composer.json Thursday, October 25, 12
  82. php composer.phar install Thursday, October 25, 12

  83. php composer.phar update ** Remember to download or copy composer.phar

    into your Zf2 skeleton project Thursday, October 25, 12
  84. composer.lock When you install vendors, Composer creates a composer.lock file,

    with the exact details and version of all libraries Thursday, October 25, 12
  85. create-project • install: ignores composer.json and reads frozen vendors from

    composer.lock • update: ignores composer.lock and re- parses composer.json, updating packages to the latest version specified there and then updates composer.lock Thursday, October 25, 12
  86. create-project • install: use day-to-day to make sure your vendor

    libraries are right where they should be • update: use only when you’re specifically adding or upgrading a library Thursday, October 25, 12
  87. Thursday, October 25, 12

  88. Start using the library immediately Thursday, October 25, 12

  89. use Knp\Menu\MenuFactory; use Knp\Menu\Renderer\ListRenderer; class IndexController extends AbstractActionController { public

    function indexAction() { // would be even better as a service... $factory = new MenuFactory(); $menu = $factory->createItem('My menu'); $menu->addChild('Home', array('uri' => '/')); // ... $renderer = new ListRenderer(); return new ViewModel(array( 'renderedMenu' => $renderer->render($menu) )); } } Thursday, October 25, 12
  90. Act 4 Using Zend Framework 2 Components from Anywhere Thursday,

    October 25, 12
  91. Super Easy... Thursday, October 25, 12

  92. One Trick... Thursday, October 25, 12

  93. ZF2 hosts its packages outside of Packagist.org... Thursday, October 25,

    12
  94. composer.json { "repositories": [ { "type": "composer", "url": "http://packages.zendframework.com/" }

    ] } Source: http://framework.zend.com/downloads/composer Now your composer looks for packages at packagist.org *and* packages.zf.com Thursday, October 25, 12
  95. Add your packages... Thursday, October 25, 12

  96. composer.json { "repositories": [ { "type": "composer", "url": "http://packages.zendframework.com/" }

    ], "require": { "zendframework/zend-config": "2.0.*", "zendframework/zend-http": "2.0.*" } } Thursday, October 25, 12
  97. Download composer.phar php composer.phar install Thursday, October 25, 12

  98. Download composer.phar php composer.phar update ** if there is no

    composer.lock yet, update == install Thursday, October 25, 12
  99. Download composer.phar php composer.phar update require 'vendor/autoload.php'; This belongs in

    some bootstrap file in your app Thursday, October 25, 12
  100. And that’s it! Thursday, October 25, 12

  101. Act 5 Kicking ass in the new PHP eco-system Thursday,

    October 25, 12
  102. The PHP Ecosphere @weaverryan Zend Framework is only one part

    of the picture What other stuff exists? • Zend Framework • Symfony • eZ Components • Drupal • ... • Individuals Thursday, October 25, 12
  103. How do we find good libraries? Thursday, October 25, 12

  104. Thursday, October 25, 12

  105. Thursday, October 25, 12

  106. Some Favorites Thursday, October 25, 12

  107. Mink behat/mink http://mink.behat.org/ By @everzet (this is a current photo)

    Thursday, October 25, 12
  108. Command a browser, find elements, click them, and fill out

    forms $driver = new \Behat\Mink\Driver\SahiDriver('firefox'); $session->visit('http://my_project.dev/some_page.php'); $page = $session->getPage(); $anchor = $page->find('css', '.something'); $anchor->click(); // get the content of the new page echo $page->getContent(); Thursday, October 25, 12
  109. Monolog monolog/monolog https://github.com/Seldaek/monolog Your Friend Jordi Thursday, October 25, 12

  110. A Logger, where bells and whistles come standard use Monolog\Logger;

    use Monolog\Handler\StreamHandler; use Monolog\Handler\FirePHPHandler; // Create the logger $logger = new Logger('my_logger'); $logger->pushHandler(new StreamHandler( __DIR__.'/my_app.log' )); $logger->pushHandler(new FirePHPHandler()); $logger->addInfo('My logger is now ready'); Thursday, October 25, 12
  111. Yay Find More! Thursday, October 25, 12

  112. Epilogue 4 Reasons to get silly-excited about the Future Thursday,

    October 25, 12
  113. 1) Look for Participation & Consolidation Thursday, October 25, 12

  114. Shared Building-blocks @weaverryan • Drupal • phpBB • Midgard •

    Zikula • eZ Publish • ... Thursday, October 25, 12
  115. Less Library Duplication? @weaverryan • Zend/Form • Zend/Serializer • Zend/Http

    • Zend/EventManager • Zend/Log • Zend/Navigation • ... • Symfony/Form • Symfony/Serializer • Symfony/HttpFoundation • Symfony/EventDispatcher • Monolog • KnpMenu • ... Thursday, October 25, 12
  116. 2) More high quality, community-grown libraries Thursday, October 25, 12

  117. Solving 1 specific problem is a low barrier to entry

    Thursday, October 25, 12
  118. Find them, fork them. Thursday, October 25, 12

  119. Write their docs :) Thursday, October 25, 12

  120. Programmers that write docs get hugs Thursday, October 25, 12

  121. 3) Easier upgrades Thursday, October 25, 12

  122. 4) Grow your Community Thursday, October 25, 12

  123. Solutions exist outside of your framework Thursday, October 25, 12

  124. Weier O'Phinney will not come to your house and tell

    you about them Thursday, October 25, 12
  125. Image: http://framework.zend.com/404 Thursday, October 25, 12

  126. Cast your vote by using the best libraries and improving

    them Thursday, October 25, 12
  127. and realize the power of the entire PHP community Thursday,

    October 25, 12
  128. Thanks... Ryan Weaver @weaverryan KnpUniversity.com PHP Tutorial Screencasts Thursday, October

    25, 12
  129. ... and we love you! Ryan Weaver @weaverryan joind.in/7038 Thursday,

    October 25, 12