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

Composer: Effective Dependency Management for PHP Projects

Composer: Effective Dependency Management for PHP Projects

When people ask you to name your hobbies most likely manually managing dependencies for my project is not high on the list. Composer is a new tool that allows you to easily manage the required libraries and components that make your application scream awesomeness. In this talk we will take a look at how Composer works, how you can use Composer today, and how to expose your projects and libraries via Composer.

Jeff Carouth

June 21, 2012
Tweet

More Decks by Jeff Carouth

Other Decks in Programming

Transcript

  1. However, PEAR tends to be used globally.* Composer is intended

    to be used in a per-project manner. * PEAR can be used per-project, but that’s not what this talk is about. Okay?
  2. The Problem As a developer working on a PHP project,

    I want to use a consistent version of libraries and tools, so that the code will be stable and predictable.
  3. Example: Behat $ pear channel-discover pear.behat.org $ sudo pear install

    behat/behat $ sudo pear install behat/mink $ behat --version Behat version 2.3.5
  4. More Developers, More Dependency Problems Jeff PHPUnit 3.6.10 Behat 2.3.5

    Guzzle 2.0 Bob PHPUnit 3.4.15 Behat 2.2 Guzzle 1.1
  5. More Developers, More Dependency Problems Jeff PHPUnit 3.6.10 Behat 2.3.5

    Guzzle 2.0 Bob PHPUnit 3.4.15 Behat 2.2 Guzzle 1.1 Of course, they were all installed via the PEAR installer
  6. More Developers, More Dependency Problems Jeff PHPUnit 3.6.10 Behat 2.3.5

    Guzzle 2.0 Bob PHPUnit 3.4.15 Behat 2.2 Guzzle 1.1 Of course, they were all installed via the PEAR installer standardize & distribute
  7. Common Solution $ cd ~/projects/my_awesome_project/vendor/ $ mkdir Behat && cd

    Behat $ wget https://github.com/downloads/Behat/Behat/behat.phar $ cd ../../ $ php vendor/Behat/behat.phar tests/features/
  8. Common Solution $ cd ~/projects/my_awesome_project/vendor/ $ mkdir Behat && cd

    Behat $ wget https://github.com/downloads/Behat/Behat/behat.phar $ cd ../../ $ php vendor/Behat/behat.phar tests/features/ Or use a git submodule… or an SVN external if it’s still 2007
  9. Common Solution $ cd ~/projects/my_awesome_project/vendor/ $ mkdir Behat && cd

    Behat $ wget https://github.com/downloads/Behat/Behat/behat.phar $ cd ../../ $ php vendor/Behat/behat.phar tests/features/ Or use a git submodule… or an SVN external if it’s still 2007 I kid. I kid.
  10. Common Solution $ cd ~/projects/my_awesome_project/vendor/ $ mkdir Behat && cd

    Behat $ wget https://github.com/downloads/Behat/Behat/behat.phar $ cd ../../ $ php vendor/Behat/behat.phar tests/features/ Or use a git submodule… or an SVN external if it’s still 2007 BUT…we can do better I kid. I kid.
  11. getcomposer.org local install $ curl -s http://getcomposer.org/installer | php $

    php composer.phar --version Composer version 6f576d4
  12. getcomposer.org local install $ curl -s http://getcomposer.org/installer | php $

    php composer.phar --version Composer version 6f576d4 global install $ curl -s http://getcomposer.org/installer | php $ sudo mv composer.phar /usr/local/bin/composer $ composer --version Composer version 6f576d4
  13. $ cd ~/projects/composed-project $ vim composer.json { "require": { "slim/slim":

    "1.6.3", "pimple/pimple": "1.0.0" } } $ composer install Installing dependencies - Installing slim/slim (1.6.3) Downloading: 100% - Installing pimple/pimple (1.0.0) Downloading: 100% Writing lock file Generating autoload files
  14. $ cd ~/projects/composed-project $ vim composer.json { "require": { "slim/slim":

    "1.6.3", "pimple/pimple": "1.0.0" } } $ composer install Installing dependencies - Installing slim/slim (1.6.3) Downloading: 100% - Installing pimple/pimple (1.0.0) Downloading: 100% Writing lock file Generating autoload files
  15. $ md public && vim public/index.php 1 <?php 2 defined('BASEPATH')

    3 || define('BASEPATH', realpath(__DIR__."/../../")); 4 5 /** 6 * Configure autoloading from the composer library 7 */ 8 require_once BASEPATH . "/vendor/autoload.php"; 9 10 $app = new Slim(); 11 12 $app->get('/', function() { 13 print "Hello, world!"; 14 }); 15 16 $app->run();
  16. 1 <?php 2 defined('BASEPATH') 3 || define('BASEPATH', realpath(__DIR__."/../../")); 4 5

    /** 6 * Configure autoloading from the composer library 7 */ 8 require_once BASEPATH . "/vendor/autoload.php"; 9 10 $app = new Slim(); 11 12 $app->get('/', function() { 13 print "Hello, world!"; 14 }); 15 16 $app->run(); $ md public && vim public/index.php
  17. You should not ignore the [composer.lock] file in git. In

    fact you should commit it whenever it is changed. Users of your project or other developers can then run “composer install” to install exactly the versions you had in your lock file, rather than installing versions based on composer.json and potentially ending up with a slightly newer dependency which could result in problems, since you didn’t test with that particular version. - Nils Adermann
  18. $ vim .gitignore vendor/ $ git add . $ git

    commit -m “Hello world! First commit” [master (root-commit) f38852e] Hello world! First commit 4 files changed, 44 insertions(+) create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 public/index.php create mode 100644 .gitignore
  19. A Contributor Appears Remember Bob? He’s going to contribute to

    this project. So he needs to make sure he gets the correct dependencies after he clones the project.
  20. A Contributor Appears Remember Bob? He’s going to contribute to

    this project. So he needs to make sure he gets the correct dependencies after he clones the project.
  21. A Contributor Appears Remember Bob? He’s going to contribute to

    this project. So he needs to make sure he gets the correct dependencies after he clones the project.
  22. $ hub clone jcarouth/composer-example $ [master ✓] composer install Installing

    dependencies from lock file - Installing pimple/pimple (1.0.0) Downloading: 100% - Installing slim/slim (1.6.3) Downloading: 100% Generating autoload files
  23. $ hub clone jcarouth/composer-example $ [master ✓] composer install Installing

    dependencies from lock file - Installing pimple/pimple (1.0.0) Downloading: 100% - Installing slim/slim (1.6.3) Downloading: 100% Generating autoload files
  24. $ hub clone jcarouth/composer-example $ [master ✓] composer install Installing

    dependencies from lock file - Installing pimple/pimple (1.0.0) Downloading: 100% - Installing slim/slim (1.6.3) Downloading: 100% Generating autoload files BOOM! That was easy
  25. $ vim composer.json 1 { 2 "require": { 3 "slim/slim":

    "1.6.3", 4 "pimple/pimple": "1.0.0" 5 }, 6 "require-dev": { 7 "behat/behat": "2.4.0" 8 }, 9 "config": { 10 "bin-dir": "bin" 11 } 12 } $ composer update Updating dependencies Nothing to install or update Writing lock file Generating autoload files
  26. $ composer install --dev Installing dependencies from lock file Nothing

    to install or update Installing dev dependencies - Installing symfony/finder (dev-master) Cloning 9ee9a907afeef52956187e862714a7702ca26590 - Installing symfony/yaml (dev-master) Cloning 8272d98f8087878db37e8e5f6d4e37c5cc783dc3 ... snip --- - Installing behat/gherkin (v2.2.1) Downloading: 100% - Installing behat/behat (v2.4.0) Downloading: 100% ... suggestions ... Generating autoload files
  27. Suggestions Suggested packages that can enhance or work well with

    this package. These are just informational and are displayed after the package is installed, to give your users a hint that they could add more packages, even though they are not strictly required. behat/behat suggests installing behat/mink-extension (for integration of with Mink) behat/behat suggests installing behat/yii-extension (for integration of with Yii framework) Generating autoload files
  28. 1 { 2 "repositories": [ 3 { 4 "type": "composer",

    5 "url": "http://packages.zendframework.com/" 6 } 7 ], 8 "require": { 9 "slim/slim": "1.6.3", 10 "pimple/pimple": "1.0.0", 11 "zendframework/zend-validator": "2.0.*" 12 }, 13 "require-dev": { 14 "behat/behat": "2.4.0" 15 }, 16 "config": { 17 "bin-dir": "bin" 18 } 19 } Including the Zend\Validator component
  29. $ composer update Updating dependencies - Installing zendframework/zend-validator (2.0.0-beta4) Downloading:

    100% zendframework/zend-validator suggests installing zendframework/ze… zendframework/zend-validator suggests installing zendframework/ze… zendframework/zend-validator suggests installing zendframework/ze… Writing lock file Generating autoload files