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

Composer: The cool kid at the PHParty (LAPHP)

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!

weaverryan

November 20, 2013
Tweet

More Decks by weaverryan

Other Decks in Technology

Transcript

  1. Composer: The Cool Kid at the PHParty with your friend:

    Ryan Weaver @weaverryan Wednesday, November 20, 13
  2. 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
  3. Component: A small PHP Library that can work anywhere Drupal

    Symfony CodeIgniter Wordpress Wednesday, November 20, 13
  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? Wednesday, November 20, 13
  5. if you don’t want the WHOLE library, carefully delete everything

    except the dependent components Wednesday, November 20, 13
  6. If PHP is big, we’ll thrive If PHP is small,

    we’ll die @weaverryan 2 Wednesday, November 20, 13
  7. Fragmentation @weaverryan But fragmentation makes us tiny, isolated, and misguided

    trend-setters http://www.flickr.com/photos/slpunk99/7329609744 Wednesday, November 20, 13
  8. Fragmentation @weaverryan • More information we have to know •

    Difficult to hire • Disjointed forums, StackOverflow • Interoperability? What’s that? Wednesday, November 20, 13
  9. Autoloader: An invisible function that knows where all of your

    PHP classes live and automatically uses require to include them Wednesday, November 20, 13
  10. 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
  11. From The Mountain: PSR-0 Class Naming Conventions @weaverryan “Thou shalt

    name your classes by following a predictable pattern” Wednesday, November 20, 13
  12. 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
  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! Wednesday, November 20, 13
  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! Wednesday, November 20, 13
  15. 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
  16. 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
  17. Find the right version ** Bleeding edge (unstable) ** Development

    branch (likely unstable) ** Latest stable version (safest bet!) Wednesday, November 20, 13
  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! Wednesday, November 20, 13
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. { "require": { "knplabs/knp-menu": "1.1.2", "symfony/finder": "~2.3" } } What’s

    up with this version number? Wednesday, November 20, 13
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. Shared Building-blocks • Drupal • Symfony • phpBB • Midgard

    • Zikula • ez Publish 5 • ... @weaverryan Wednesday, November 20, 13
  35. 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
  36. ... and we love you! Ryan Weaver @weaverryan Ryan Weaver

    @weaverryan Wednesday, November 20, 13