Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Wonderful World of Composer and ZF2

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.

weaverryan

October 25, 2012
Tweet

More Decks by weaverryan

Other Decks in Technology

Transcript

  1. The Wonderful World of
    Composer & ZF2
    by your friend:
    Ryan Weaver
    @weaverryan
    Thursday, October 25, 12

    View Slide

  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

    View Slide

  3. Intro
    Life before components
    (The Desert of the Real)
    Copyright by Moyan Brenn http://www.flickr.com/aigle_dore
    Thursday, October 25, 12

    View Slide

  4. Zend Framework 2 consists
    of 48 individual
    components
    Thursday, October 25, 12

    View Slide

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

    View Slide

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

    View Slide

  7. Who Cares?
    Thursday, October 25, 12

    View Slide

  8. We Suck at Sharing
    and that sucks for you
    @weaverryan
    1
    Thursday, October 25, 12

    View Slide

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

    View Slide

  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

    View Slide

  11. Include a Zend Framework 1
    component in symfony1
    Thursday, October 25, 12

    View Slide

  12. Thursday, October 25, 12

    View Slide

  13. manually download the
    library and commit it
    into your project
    Thursday, October 25, 12

    View Slide

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

    View Slide

  15. autoloading is completely
    custom ... blah gross!
    Thursday, October 25, 12

    View Slide

  16. Components are the key to
    mastering your framework
    @weaverryan
    2
    Thursday, October 25, 12

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  20. Fragmentation
    @weaverryan
    But fragmentation makes us tiny, isolated, and
    misguided trend-setters
    http://www.flickr.com/photos/slpunk99/7329609744
    Thursday, October 25, 12

    View Slide

  21. PHP frameworks < Rails
    @weaverryan
    Thursday, October 25, 12

    View Slide

  22. I don’t want a damned
    CakePHP Plugin!
    @weaverryan
    CakePHP
    CodeIgniter
    Thursday, October 25, 12

    View Slide

  23. Fragmentation
    @weaverryan
    • More information we have to know
    • Difficult to hire
    • Disjointed forums, StackOverflow
    • Interoperability? What’s that?
    Thursday, October 25, 12

    View Slide

  24. Components are shareable
    across all of PHP
    Thursday, October 25, 12

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  30. like a town where every
    store has its own currency
    Thursday, October 25, 12

    View Slide

  31. From The Mountain:
    PSR-0 Class Naming Conventions
    @weaverryan
    “Thou shalt name your classes by
    following a predictable pattern”
    Thursday, October 25, 12

    View Slide

  32. class:
    sfRequest
    path:
    lib/vendor/symfony/???idk
    Thursday, October 25, 12

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  36. (B)
    Managing Vendors in your
    project
    Thursday, October 25, 12

    View Slide

  37. Composer
    Thursday, October 25, 12

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  41. curl -s https://getcomposer.org/installer | php
    Download Composer.phar
    Source: http://getcomposer.org/download/
    Thursday, October 25, 12

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  46. Search for
    something
    Thursday, October 25, 12

    View Slide

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

    View Slide

  48. Find the right
    version
    ** Bleeding edge (unstable)
    ** Development branch
    (likely unstable)
    ** Latest stable version
    (safest bet!)
    Thursday, October 25, 12

    View Slide

  49. knplabs/knp-menu: v1.1.2
    Thursday, October 25, 12

    View Slide

  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

    View Slide

  51. {
    "require": {
    "knplabs/knp-menu": "v1.1.2"
    }
    }
    Create composer.json
    Thursday, October 25, 12

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  55. Now we tell Composer to
    read composer.json and
    download our libraries
    Thursday, October 25, 12

    View Slide

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

    View Slide

  57. Thursday, October 25, 12

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  61. Thursday, October 25, 12

    View Slide

  62. Thursday, October 25, 12

    View Slide

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

    View Slide

  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

    View Slide

  65. https://github.com/zendframework/ZendSkeletonApplication
    Thursday, October 25, 12

    View Slide

  66. php composer.phar create-project
    A special one-time-use way to
    “clone” a project skeleton
    Thursday, October 25, 12

    View Slide

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

    View Slide

  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

    View Slide

  69. Typical Usage
    php composer.phar create-project package/name local/dir
    The command name
    Thursday, October 25, 12

    View Slide

  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

    View Slide

  71. Typical Usage
    php composer.phar create-project package/name local/dir
    The name of the package on Packagist.org
    Thursday, October 25, 12

    View Slide

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

    View Slide

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

    View Slide

  74. php composer.phar create-project \
    The ZF2 Skeleton App
    ... just breaking a long
    shell command onto
    multiple lines...
    Thursday, October 25, 12

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  78. Thursday, October 25, 12

    View Slide

  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

    View Slide

  80. Now, how can I bring in other
    libraries?
    Thursday, October 25, 12

    View Slide

  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

    View Slide

  82. php composer.phar install
    Thursday, October 25, 12

    View Slide

  83. php composer.phar update
    ** Remember to download or
    copy composer.phar into your
    Zf2 skeleton project
    Thursday, October 25, 12

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  87. Thursday, October 25, 12

    View Slide

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

    View Slide

  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

    View Slide

  90. Act 4
    Using Zend Framework 2
    Components from Anywhere
    Thursday, October 25, 12

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  95. Add your packages...
    Thursday, October 25, 12

    View Slide

  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

    View Slide

  97. Download composer.phar
    php composer.phar install
    Thursday, October 25, 12

    View Slide

  98. Download composer.phar
    php composer.phar update
    ** if there is no composer.lock yet,
    update == install
    Thursday, October 25, 12

    View Slide

  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

    View Slide

  100. And that’s it!
    Thursday, October 25, 12

    View Slide

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

    View Slide

  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

    View Slide

  103. How do we find good
    libraries?
    Thursday, October 25, 12

    View Slide

  104. Thursday, October 25, 12

    View Slide

  105. Thursday, October 25, 12

    View Slide

  106. Some Favorites
    Thursday, October 25, 12

    View Slide

  107. Mink
    behat/mink
    http://mink.behat.org/
    By @everzet
    (this is a current photo)
    Thursday, October 25, 12

    View Slide

  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

    View Slide

  109. Monolog
    monolog/monolog
    https://github.com/Seldaek/monolog
    Your Friend Jordi
    Thursday, October 25, 12

    View Slide

  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

    View Slide

  111. Yay Find More!
    Thursday, October 25, 12

    View Slide

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

    View Slide

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

    View Slide

  114. Shared
    Building-blocks
    @weaverryan
    • Drupal
    • phpBB
    • Midgard
    • Zikula
    • eZ Publish
    • ...
    Thursday, October 25, 12

    View Slide

  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

    View Slide

  116. 2) More high quality,
    community-grown libraries
    Thursday, October 25, 12

    View Slide

  117. Solving 1 specific problem
    is a low barrier to entry
    Thursday, October 25, 12

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  124. Weier O'Phinney will not come
    to your house and tell you
    about them
    Thursday, October 25, 12

    View Slide

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

    View Slide

  126. Cast your vote by using the
    best libraries and improving
    them
    Thursday, October 25, 12

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide