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
  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. 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
  5. if you don’t want the WHOLE library, carefully delete everything

    except the dependent components Thursday, October 25, 12
  6. If PHP is big, we’ll thrive If PHP is small,

    we’ll die @weaverryan 3 Thursday, October 25, 12
  7. Fragmentation @weaverryan But fragmentation makes us tiny, isolated, and misguided

    trend-setters http://www.flickr.com/photos/slpunk99/7329609744 Thursday, October 25, 12
  8. Fragmentation @weaverryan • More information we have to know •

    Difficult to hire • Disjointed forums, StackOverflow • Interoperability? What’s that? Thursday, October 25, 12
  9. 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
  10. From The Mountain: PSR-0 Class Naming Conventions @weaverryan “Thou shalt

    name your classes by following a predictable pattern” Thursday, October 25, 12
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. Find the right version ** Bleeding edge (unstable) ** Development

    branch (likely unstable) ** Latest stable version (safest bet!) Thursday, October 25, 12
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. Typical Usage php composer.phar create-project package/name local/dir The name of

    the package on Packagist.org Thursday, October 25, 12
  27. php composer.phar create-project \ The ZF2 Skeleton App ... just

    breaking a long shell command onto multiple lines... Thursday, October 25, 12
  28. 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
  29. 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
  30. 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
  31. 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
  32. { "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
  33. php composer.phar update ** Remember to download or copy composer.phar

    into your Zf2 skeleton project Thursday, October 25, 12
  34. 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
  35. 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
  36. 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
  37. 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
  38. 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
  39. 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
  40. Download composer.phar php composer.phar update ** if there is no

    composer.lock yet, update == install Thursday, October 25, 12
  41. 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
  42. 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
  43. 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
  44. Shared Building-blocks @weaverryan • Drupal • phpBB • Midgard •

    Zikula • eZ Publish • ... Thursday, October 25, 12
  45. 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
  46. Weier O'Phinney will not come to your house and tell

    you about them Thursday, October 25, 12