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

Composer

Tom
October 16, 2012

 Composer

Composer is a tool for managing dependencies in PHP projects. This talk will introduce Composer and its associated package repository Packagist. The key features and benefits of using Composer will be explored and through practical demonstrations you will gain a working knowledge of using Composer for dependency management.

Tom

October 16, 2012
Tweet

More Decks by Tom

Other Decks in Programming

Transcript

  1. What is Composer? “Composer is a tool for dependency management.

    It allows you to declare the dependent libraries your project needs and it will install them in your project for you.” [http://getcomposer.org/doc/00-intro.md] Tuesday, 16 October 12
  2. What does that mean? • Scope is per project, not

    global • Resolves dependencies Tuesday, 16 October 12
  3. What does that mean? • Scope is per project, not

    global • Resolves dependencies • Runs installation tasks Tuesday, 16 October 12
  4. Packagist • Composer’s default repository • Put your open source

    libraries here • Great resource for well maintained PHP libraries Tuesday, 16 October 12
  5. Statistics • Over 4000 packages listed on packagist.org • More

    than 4,500,000 package installations over the last 6 months [https://packagist.org/statistics] Tuesday, 16 October 12
  6. Installing Composer Locally: $ cd /project/path $ curl -s https://getcomposer.org/installer

    | php Globally: $ sudo mv composer.phar /usr/local/bin/composer Tuesday, 16 October 12
  7. Staying current Locally: $ cd /project/path $ php composer.phar self-update

    Globally: $ composer self-update Tuesday, 16 October 12
  8. Using Composer • Define project dependencies • Install them •

    Bootstrap the autoloader Tuesday, 16 October 12
  9. Define your dependencies composer.json { "require": { "phpmelb/project1": "1.0.2" "phpmelb/project2":

    "1.0.*" "phpmelb/project3": ">=1.0,<2.0" "phpmelb/project4": "4d3fe5feaf" "phpmelb/project5": "dev-master" } } Tuesday, 16 October 12
  10. Using the autoloader <?php require 'vendor/autoload.php'; $foo = new PHPMelb\Project1\Foo();

    $bar = new PHPMelb_Project2_Bar(); ... Tuesday, 16 October 12
  11. { "name": "symfony/framework-standard-edition", "description": "The \"Symfony Standard Edition\" distribution", "autoload":

    { "psr-0": { "": "src/" } }, "require": { "php": ">=5.3.3", "symfony/symfony": "2.1.*", "doctrine/orm": ">=2.2.3,<2.4-dev", "doctrine/doctrine-bundle": "1.0.*", "twig/extensions": "1.0.*", "symfony/assetic-bundle": "2.1.*", "symfony/swiftmailer-bundle": "2.1.*", "symfony/monolog-bundle": "2.1.*", "sensio/distribution-bundle": "2.1.*", "sensio/framework-extra-bundle": "2.1.*", "sensio/generator-bundle": "2.1.*", "jms/security-extra-bundle": "1.2.*", "jms/di-extra-bundle": "1.1.*" }, Tuesday, 16 October 12
  12. { "name": "symfony/framework-standard-edition", "description": "The \"Symfony Standard Edition\" distribution", "autoload":

    { "psr-0": { "": "src/" } }, "require": { "php": ">=5.3.3", "symfony/symfony": "2.1.*", "doctrine/orm": ">=2.2.3,<2.4-dev", "doctrine/doctrine-bundle": "1.0.*", "twig/extensions": "1.0.*", "symfony/assetic-bundle": "2.1.*", "symfony/swiftmailer-bundle": "2.1.*", "symfony/monolog-bundle": "2.1.*", "sensio/distribution-bundle": "2.1.*", "sensio/framework-extra-bundle": "2.1.*", "sensio/generator-bundle": "2.1.*", "jms/security-extra-bundle": "1.2.*", "jms/di-extra-bundle": "1.1.*" }, "scripts": { "post-install-cmd": [ "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile" ], "post-update-cmd": [ "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile" ] }, Tuesday, 16 October 12
  13. { "name": "symfony/framework-standard-edition", "description": "The \"Symfony Standard Edition\" distribution", "autoload":

    { "psr-0": { "": "src/" } }, "require": { "php": ">=5.3.3", "symfony/symfony": "2.1.*", "doctrine/orm": ">=2.2.3,<2.4-dev", "doctrine/doctrine-bundle": "1.0.*", "twig/extensions": "1.0.*", "symfony/assetic-bundle": "2.1.*", "symfony/swiftmailer-bundle": "2.1.*", "symfony/monolog-bundle": "2.1.*", "sensio/distribution-bundle": "2.1.*", "sensio/framework-extra-bundle": "2.1.*", "sensio/generator-bundle": "2.1.*", "jms/security-extra-bundle": "1.2.*", "jms/di-extra-bundle": "1.1.*" }, "scripts": { "post-install-cmd": [ "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile" ], "post-update-cmd": [ "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile" ] }, "config": { "bin-dir": "bin" }, Tuesday, 16 October 12
  14. { "name": "symfony/framework-standard-edition", "description": "The \"Symfony Standard Edition\" distribution", "autoload":

    { "psr-0": { "": "src/" } }, "require": { "php": ">=5.3.3", "symfony/symfony": "2.1.*", "doctrine/orm": ">=2.2.3,<2.4-dev", "doctrine/doctrine-bundle": "1.0.*", "twig/extensions": "1.0.*", "symfony/assetic-bundle": "2.1.*", "symfony/swiftmailer-bundle": "2.1.*", "symfony/monolog-bundle": "2.1.*", "sensio/distribution-bundle": "2.1.*", "sensio/framework-extra-bundle": "2.1.*", "sensio/generator-bundle": "2.1.*", "jms/security-extra-bundle": "1.2.*", "jms/di-extra-bundle": "1.1.*" }, "scripts": { "post-install-cmd": [ "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile" ], "post-update-cmd": [ "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile" ] }, "config": { "bin-dir": "bin" }, "minimum-stability": "dev", Tuesday, 16 October 12
  15. { "name": "symfony/framework-standard-edition", "description": "The \"Symfony Standard Edition\" distribution", "autoload":

    { "psr-0": { "": "src/" } }, "require": { "php": ">=5.3.3", "symfony/symfony": "2.1.*", "doctrine/orm": ">=2.2.3,<2.4-dev", "doctrine/doctrine-bundle": "1.0.*", "twig/extensions": "1.0.*", "symfony/assetic-bundle": "2.1.*", "symfony/swiftmailer-bundle": "2.1.*", "symfony/monolog-bundle": "2.1.*", "sensio/distribution-bundle": "2.1.*", "sensio/framework-extra-bundle": "2.1.*", "sensio/generator-bundle": "2.1.*", "jms/security-extra-bundle": "1.2.*", "jms/di-extra-bundle": "1.1.*" }, "scripts": { "post-install-cmd": [ "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile" ], "post-update-cmd": [ "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile" ] }, "config": { "bin-dir": "bin" }, "minimum-stability": "dev", Tuesday, 16 October 12
  16. { "name": "symfony/framework-standard-edition", "description": "The \"Symfony Standard Edition\" distribution", "autoload":

    { "psr-0": { "": "src/" } }, "require": { "php": ">=5.3.3", "symfony/symfony": "2.1.*", "doctrine/orm": ">=2.2.3,<2.4-dev", "doctrine/doctrine-bundle": "1.0.*", "twig/extensions": "1.0.*", "symfony/assetic-bundle": "2.1.*", "symfony/swiftmailer-bundle": "2.1.*", "symfony/monolog-bundle": "2.1.*", "sensio/distribution-bundle": "2.1.*", "sensio/framework-extra-bundle": "2.1.*", "sensio/generator-bundle": "2.1.*", "jms/security-extra-bundle": "1.2.*", "jms/di-extra-bundle": "1.1.*" }, "scripts": { "post-install-cmd": [ "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile" ], "post-update-cmd": [ "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile" ] }, "config": { "bin-dir": "bin" }, "minimum-stability": "dev", "extra": { "symfony-app-dir": "app", "symfony-web-dir": "web" } } Tuesday, 16 October 12
  17. Composer: Killer Features • composer.lock • Satis - Your private

    repository • composer installers Tuesday, 16 October 12
  18. Composer: Killer Features • composer.lock • Satis - Your private

    repository • composer installers • Get started with a new framework project directly from composer Tuesday, 16 October 12
  19. composer.lock • Used when you call composer install • Updated

    by composer update Tuesday, 16 October 12
  20. composer.lock • Used when you call composer install • Updated

    by composer update • Everybody runs the exact same version of dependencies Tuesday, 16 October 12
  21. Satis • Your very own packagist for your proprietary code

    • Eases deployment Tuesday, 16 October 12
  22. Satis • Your very own packagist for your proprietary code

    • Eases deployment • Composer will connect to it securely Tuesday, 16 October 12
  23. Satis • Your very own packagist for your proprietary code

    • Eases deployment • Composer will connect to it securely • HTTPS -Public key Tuesday, 16 October 12
  24. Satis • Your very own packagist for your proprietary code

    • Eases deployment • Composer will connect to it securely • HTTPS -Public key • SSH Tuesday, 16 October 12
  25. Satis • Your very own packagist for your proprietary code

    • Eases deployment • Composer will connect to it securely • HTTPS -Public key • SSH • See https://github.com/composer/satis Tuesday, 16 October 12
  26. Composer Installers •CakePHP •cakephp-plugin •CodeIgniter •codeigniter-library •codeigniter-third-party •codeigniter-module •Drupal •drupal-module

    •drupal-theme •drupal-profile •drupal-drush •FuelPHP •fuelphp-module •Joomla •joomla-component •joomla-module •joomla-template •joomla-plugin •joomla-library •Kohana •kohana-module •Laravel •laravel-library •Lithium •lithium-library •lithium-source •Magento •magento-library •magento-skin •magento-theme •Mako •mako-package •MediaWiki •mediawiki-extension •phpBB •phpbb-extension •PPI •ppi-module •SilverStripe •silverstripe-module •silverstripe-theme •symfony1 •symfony1-plugin •WordPress •wordpress-plugin •wordpress-theme •Zend •zend-library •zend-extra Tuesday, 16 October 12
  27. Composer Installers • Bring the power and ease of Composer

    to your framework/CMS •CakePHP •cakephp-plugin •CodeIgniter •codeigniter-library •codeigniter-third-party •codeigniter-module •Drupal •drupal-module •drupal-theme •drupal-profile •drupal-drush •FuelPHP •fuelphp-module •Joomla •joomla-component •joomla-module •joomla-template •joomla-plugin •joomla-library •Kohana •kohana-module •Laravel •laravel-library •Lithium •lithium-library •lithium-source •Magento •magento-library •magento-skin •magento-theme •Mako •mako-package •MediaWiki •mediawiki-extension •phpBB •phpbb-extension •PPI •ppi-module •SilverStripe •silverstripe-module •silverstripe-theme •symfony1 •symfony1-plugin •WordPress •wordpress-plugin •wordpress-theme •Zend •zend-library •zend-extra Tuesday, 16 October 12
  28. Composer Installers • Bring the power and ease of Composer

    to your framework/CMS • Magically installs packages/ plugins to the correct location for framework •CakePHP •cakephp-plugin •CodeIgniter •codeigniter-library •codeigniter-third-party •codeigniter-module •Drupal •drupal-module •drupal-theme •drupal-profile •drupal-drush •FuelPHP •fuelphp-module •Joomla •joomla-component •joomla-module •joomla-template •joomla-plugin •joomla-library •Kohana •kohana-module •Laravel •laravel-library •Lithium •lithium-library •lithium-source •Magento •magento-library •magento-skin •magento-theme •Mako •mako-package •MediaWiki •mediawiki-extension •phpBB •phpbb-extension •PPI •ppi-module •SilverStripe •silverstripe-module •silverstripe-theme •symfony1 •symfony1-plugin •WordPress •wordpress-plugin •wordpress-theme •Zend •zend-library •zend-extra Tuesday, 16 October 12
  29. Composer Installers • Bring the power and ease of Composer

    to your framework/CMS • Magically installs packages/ plugins to the correct location for framework • See https://github.com/ composer/installers •CakePHP •cakephp-plugin •CodeIgniter •codeigniter-library •codeigniter-third-party •codeigniter-module •Drupal •drupal-module •drupal-theme •drupal-profile •drupal-drush •FuelPHP •fuelphp-module •Joomla •joomla-component •joomla-module •joomla-template •joomla-plugin •joomla-library •Kohana •kohana-module •Laravel •laravel-library •Lithium •lithium-library •lithium-source •Magento •magento-library •magento-skin •magento-theme •Mako •mako-package •MediaWiki •mediawiki-extension •phpBB •phpbb-extension •PPI •ppi-module •SilverStripe •silverstripe-module •silverstripe-theme •symfony1 •symfony1-plugin •WordPress •wordpress-plugin •wordpress-theme •Zend •zend-library •zend-extra Tuesday, 16 October 12
  30. Setup a new project • Symfony2 composer create-project symfony/framework-standard-edition path/to/install

    • Zend Framework 2 composer create-project --repository-url="http:// packages.zendframework.com" zendframework/skeleton-application path/to/ install Tuesday, 16 October 12