Composer: The cool kid at the PHParty (LAPHP)

F5dfeeef276fcfd4751f4063487a5a3f?s=47 weaverryan
November 20, 2013

Composer: The cool kid at the PHParty (LAPHP)

This talk takes a practical look at getting started with Composer in your project, regardless of what PHP technology you're using. We also look at why sharing in PHP used to suck, and how awesome our world has become!

F5dfeeef276fcfd4751f4063487a5a3f?s=128

weaverryan

November 20, 2013
Tweet

Transcript

  1. Composer: PHP Package Manager with your friend: Ryan Weaver @weaverryan

    Wednesday, November 20, 13
  2. Composer: The Cool Kid at the PHParty with your friend:

    Ryan Weaver @weaverryan Wednesday, November 20, 13
  3. Who is this dude? • The “Docs” guy for Symfony

    • KnpLabs US - Symfony consulting, training, Kumbaya • Writer for KnpUniversity.com screencasts • Husband of the much more talented @leannapelham knplabs.com github.com/weaverryan @weaverryan Wednesday, November 20, 13
  4. Intro Life before Composer and Components (The Desert of the

    Real) Wednesday, November 20, 13
  5. Component: A small PHP Library that can work anywhere Wednesday,

    November 20, 13
  6. Component: A small PHP Library that can work anywhere Drupal

    Symfony CodeIgniter Wordpress Wednesday, November 20, 13
  7. We Suck at Sharing and that sucks for you @weaverryan

    1 Wednesday, November 20, 13
  8. Including External Libraries is Depressing Wednesday, November 20, 13

  9. 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? Wednesday, November 20, 13
  10. Include a Zend Framework 1 component in symfony1 Wednesday, November

    20, 13
  11. Wednesday, November 20, 13

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

    Wednesday, November 20, 13
  13. if you don’t want the WHOLE library, carefully delete everything

    except the dependent components Wednesday, November 20, 13
  14. autoloading is completely custom ... blah gross! Wednesday, November 20,

    13
  15. If PHP is big, we’ll thrive If PHP is small,

    we’ll die @weaverryan 2 Wednesday, November 20, 13
  16. Communities @weaverryan PHP is Huge! Right? http://www.flickr.com/photos/kitty-kat/ Wednesday, November 20,

    13
  17. @weaverryan PHP > Ruby Wednesday, November 20, 13

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

    trend-setters http://www.flickr.com/photos/slpunk99/7329609744 Wednesday, November 20, 13
  19. PHP frameworks < Rails @weaverryan Wednesday, November 20, 13

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

    Wednesday, November 20, 13
  21. Fragmentation @weaverryan • More information we have to know •

    Difficult to hire • Disjointed forums, StackOverflow • Interoperability? What’s that? Wednesday, November 20, 13
  22. Components are shareable across all of PHP Wednesday, November 20,

    13
  23. Act 1 Making Sharing Sexy Wednesday, November 20, 13

  24. PHP Framework Interoperability Group http://www.php-fig.org/ Wednesday, November 20, 13

  25. The United Nations of PHP Wednesday, November 20, 13

  26. (A) The Problem of Autoloading Wednesday, November 20, 13

  27. Autoloader: An invisible function that knows where all of your

    PHP classes live and automatically uses require to include them Wednesday, November 20, 13
  28. 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 Wednesday, November 20, 13
  29. like a town where every store has its own currency

    Wednesday, November 20, 13
  30. From The Mountain: PSR-0 Class Naming Conventions @weaverryan “Thou shalt

    name your classes by following a predictable pattern” Wednesday, November 20, 13
  31. class: sfRequest path: lib/vendor/symfony/???idk Wednesday, November 20, 13

  32. class: Symfony\Component\HttpFoundation\Request path: vendor/symfony/src/Symfony/Component/ HttpFoundation/Request.php Wednesday, November 20, 13

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

    )); $loader->register(); Wednesday, November 20, 13
  34. (B) Managing Vendors in your project Wednesday, November 20, 13

  35. Composer Wednesday, November 20, 13

  36. Composer Wednesday, November 20, 13

  37. Composer 1) An executable that downloads libraries into your project

    Wednesday, November 20, 13
  38. Composer 1) An executable that downloads libraries into your project

    2) ... and sets up an autoloader so that you can stop writing require! Wednesday, November 20, 13
  39. Act 2 Using Composer http://KnpUniversity.com/screencast/composer Wednesday, November 20, 13

  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! Wednesday, November 20, 13
  41. 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! Wednesday, November 20, 13
  42. curl -s https://getcomposer.org/installer | php Download Composer.phar Source: http://getcomposer.org/download/ Wednesday,

    November 20, 13
  43. This downloads a single executable file: composer.phar Wednesday, November 20,

    13
  44. Execute it... And it’ll list the available commands Wednesday, November

    20, 13
  45. 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! Wednesday, November 20, 13
  46. 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 Wednesday, November 20, 13
  47. Search for something Wednesday, November 20, 13

  48. Find the name of the package Wednesday, November 20, 13

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

    branch (likely unstable) ** Latest stable version (safest bet!) Wednesday, November 20, 13
  50. knplabs/knp-menu: 1.1.2 Wednesday, November 20, 13

  51. 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! Wednesday, November 20, 13
  52. { "require": { "knplabs/knp-menu": "1.1.2" } } Create composer.json Wednesday,

    November 20, 13
  53. Or call php composer.phar init to create the composer.json interactively

    Wednesday, November 20, 13
  54. 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! Wednesday, November 20, 13
  55. So Far we have... @weaverryan • The composer.phar executable •

    A composer.json with the name and version of a package • ... that’s all! Wednesday, November 20, 13
  56. Now we tell Composer to read composer.json and download our

    libraries Wednesday, November 20, 13
  57. php composer.phar install Wednesday, November 20, 13

  58. Wednesday, November 20, 13

  59. 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 Wednesday, November 20, 13
  60. 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! Wednesday, November 20, 13
  61. 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!!! Wednesday, November 20, 13
  62. Wednesday, November 20, 13

  63. Wednesday, November 20, 13

  64. Now, how can I bring in other libraries? Wednesday, November

    20, 13
  65. { "require": { "knplabs/knp-menu": "1.1.2", "symfony/finder": "~2.3" } } Update

    composer.json Wednesday, November 20, 13
  66. php composer.phar install Wednesday, November 20, 13

  67. php composer.phar update Wednesday, November 20, 13

  68. composer.lock When you install vendors, Composer creates a composer.lock file,

    with the exact details and version of all libraries Wednesday, November 20, 13
  69. 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 Wednesday, November 20, 13
  70. 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 Wednesday, November 20, 13
  71. Text Wednesday, November 20, 13

  72. Start using the library immediately Wednesday, November 20, 13

  73. require 'vendor/autoload.php'; use Symfony\Component\Finder\Finder; $dir = __DIR__.'/uploads'; $finder = new

    Finder(); $finder->in($dir) ->name('*.gif') ->date('since 1 day ago'); $files = array(); foreach ($finder as $file) { $files[] = $file->getFilename(); } var_dump($files); Wednesday, November 20, 13
  74. Wednesday, November 20, 13

  75. { "require": { "knplabs/knp-menu": "1.1.2", "symfony/finder": "~2.3" } } What’s

    up with this version number? Wednesday, November 20, 13
  76. http://bit.ly/composer-versions 1.0.2 Exact Wednesday, November 20, 13

  77. http://bit.ly/composer-versions >=1.0,<2.0 Range Wednesday, November 20, 13

  78. http://bit.ly/composer-versions 1.0.* Wildcard Wednesday, November 20, 13

  79. ... or the really cool ... Wednesday, November 20, 13

  80. http://bit.ly/composer-versions ~1.2 Tilde Operator Wednesday, November 20, 13

  81. http://bit.ly/composer-versions ~1.2 = >=1.2,<2.0 Tilde Operator Wednesday, November 20, 13

  82. http://bit.ly/composer-versions Tilde Operator ~1.2.3 = >=1.2.3,<1.3 ~1.2 = >=1.2,<2.0 Wednesday,

    November 20, 13
  83. Only the last digit is allowed to move Tilde Operator

    1.2.2, 1.2.3, 1.2.4, ... 1.2.10, 1.2.11, 1.3.0 ~1.2.3 = >=1.2.3,<1.3 ~1.2 = >=1.2,<2.0 Wednesday, November 20, 13
  84. A) Creating private dependencies: http:// bit.ly/I2JD1b Other Topics B) Autoloading

    your own classes: http:// bit.ly/I2JZoP C) Stability Flags: http://bit.ly/18kDKq1 D) Optimizing the Autoloader: http:// bit.ly/composer-dump-autoload Wednesday, November 20, 13
  85. Act 3 Kicking arse in the new PHP eco-system Wednesday,

    November 20, 13
  86. How do we find good libraries? Wednesday, November 20, 13

  87. Wednesday, November 20, 13

  88. Wednesday, November 20, 13

  89. Some Favorites Wednesday, November 20, 13

  90. Mink behat/mink http://mink.behat.org/ By @everzet Wednesday, November 20, 13

  91. 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(); Wednesday, November 20, 13
  92. Monolog monolog/monolog https://github.com/Seldaek/monolog Your Friend Jordi Wednesday, November 20, 13

  93. 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'); Wednesday, November 20, 13
  94. Symfony Components • HttpFoundation • HttpKernel • Event Dispatcher •

    Routing • CSSSelector • DomCrawler • BrowserKit • Config • Yaml • DependencyInjection • Security • OptionsResolver • Console • Filesystem • Finder • Locale • Process • Serializer • Templating • Form • Translation • Validator Wednesday, November 20, 13
  95. Yay Find More! Wednesday, November 20, 13

  96. Epilogue 4 Reasons to get silly-excited about the Future Wednesday,

    November 20, 13
  97. 1) Look for Participation & Consolidation Wednesday, November 20, 13

  98. Shared Building-blocks • Drupal • Symfony • phpBB • Midgard

    • Zikula • ez Publish 5 • ... @weaverryan Wednesday, November 20, 13
  99. Less Library Duplication? • Zend/Form • Zend/Serializer • Zend/Http •

    Zend/EventManager • Zend/Log • Zend/Navigation • ... @weaverryan • Symfony/Form • Symfony/Serializer • Symfony/HttpFoundation • Symfony/EventDispatcher • Monolog • KnpMenu • ... Wednesday, November 20, 13
  100. 2) More high quality, community-grown libraries Wednesday, November 20, 13

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

    Wednesday, November 20, 13
  102. Find them, fork them. Wednesday, November 20, 13

  103. Write their docs :) Wednesday, November 20, 13

  104. 3) Easier upgrades Wednesday, November 20, 13

  105. 4) Grow your Community Wednesday, November 20, 13

  106. Solutions exist outside of your community Wednesday, November 20, 13

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

    them Wednesday, November 20, 13
  108. and realize the power of the entire PHP community Wednesday,

    November 20, 13
  109. Thanks... Ryan Weaver @weaverryan Wednesday, November 20, 13

  110. ... and we love you! Ryan Weaver @weaverryan Ryan Weaver

    @weaverryan Wednesday, November 20, 13