Share
Embed
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
Transcript
69 slides
-
Tom Corrigan rostercloud.com @t_corrigan Melbourne PHP Users Group October 2012
Tuesday, 16 October 12 -
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 -
What does that mean? • Scope is per project, not
global Tuesday, 16 October 12 -
What does that mean? • Scope is per project, not
global • Resolves dependencies Tuesday, 16 October 12 -
What does that mean? • Scope is per project, not
global • Resolves dependencies • Runs installation tasks Tuesday, 16 October 12 -
Packagist • Composer’s default repository • Put your open source
libraries here Tuesday, 16 October 12 -
Packagist • Composer’s default repository • Put your open source
libraries here • Great resource for well maintained PHP libraries Tuesday, 16 October 12 -
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 -
Installing Composer Locally: $ cd /project/path $ curl -s https://getcomposer.org/installer
| php Tuesday, 16 October 12 -
Installing Composer Locally: $ cd /project/path $ curl -s https://getcomposer.org/installer
| php Globally: Tuesday, 16 October 12 -
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 -
Staying current Locally: $ cd /project/path $ php composer.phar self-update
Tuesday, 16 October 12 -
Staying current Locally: $ cd /project/path $ php composer.phar self-update
Globally: Tuesday, 16 October 12 -
Staying current Locally: $ cd /project/path $ php composer.phar self-update
Globally: $ composer self-update Tuesday, 16 October 12 -
Using Composer • Define project dependencies • Install them •
Bootstrap the autoloader Tuesday, 16 October 12 -
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 -
Install $ composer install vendor/ .composer/ phpmelb/ project1/ project2/ project3/
project4/ autoload.php Tuesday, 16 October 12 -
Using the autoloader <?php require 'vendor/autoload.php'; $foo = new PHPMelb\Project1\Foo();
$bar = new PHPMelb_Project2_Bar(); ... Tuesday, 16 October 12 -
{ "name": "symfony/framework-standard-edition", "description": "The \"Symfony Standard Edition\" distribution", "autoload":
{ "psr-0": { "": "src/" } }, Tuesday, 16 October 12 -
{ "name": "symfony/framework-standard-edition", "description": "The \"Symfony Standard Edition\" distribution", "autoload":
{ "psr-0": { "": "src/" } }, Tuesday, 16 October 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.*" }, Tuesday, 16 October 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 -
{ "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 -
{ "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 -
{ "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 -
{ "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 -
Composer: Killer Features • composer.lock • Satis - Your private
repository Tuesday, 16 October 12 -
Composer: Killer Features • composer.lock • Satis - Your private
repository • composer installers Tuesday, 16 October 12 -
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 -
composer.lock • Used when you call composer install • Updated
by composer update Tuesday, 16 October 12 -
composer.lock • Used when you call composer install • Updated
by composer update • Everybody runs the exact same version of dependencies Tuesday, 16 October 12 -
Satis • Your very own packagist for your proprietary code
Tuesday, 16 October 12 -
Satis • Your very own packagist for your proprietary code
• Eases deployment Tuesday, 16 October 12 -
Satis • Your very own packagist for your proprietary code
• Eases deployment • Composer will connect to it securely Tuesday, 16 October 12 -
Satis • Your very own packagist for your proprietary code
• Eases deployment • Composer will connect to it securely • HTTPS -Public key Tuesday, 16 October 12 -
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 -
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 -
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 -
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 -
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 -
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 -
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