Composer - The missing package manager for PHP

16,227 views
16,057 views

Published on

I gave the talk in on 6th June, 2014 at Digital World developer conference at Bangabandhu International Conference Center (BICC) in Dhaka, Bangladesh.

Composer - The missing package manager for PHP

  1. 1. COMPOSER The missing package manager for PHP
  2. 2. PyPi
  3. 3. Common Web Languages
  4. 4. Communities inside PHP
  5. 5. SHARING WASN’T CARING • Autoloading PHP Classes • Dependencies? • Where does this thing go?
  6. 6. WHY BOTHER? • As a developer, you want to use consistent version of libraries/dependencies and tools • Consistent versioning across all team members • Share your own library
  7. 7. WHY BOTHER? More developers, more dependency problem PHPUnit 3.6.10 Behat 2.3.5 Monolog 1.0 PHPUnit 3.4.15 Behat 2.2 Monolog 1.2 Hasin Rifat
  8. 8. SOLUTIONS • PEAR • SVN Externals • Git Submodules • Vendor Management Scripts
  9. 9. SOLUTIONS • PEAR • SVN Externals • Git Submodules • Vendor Management Scripts
  10. 10. SOLUTIONS • PEAR • SVN Externals • Git Submodules • Vendor Management Scripts
  11. 11. SOLUTIONS • PEAR • SVN Externals • Git Submodules • Vendor Management Scripts
  12. 12. SOLUTIONS • PEAR • SVN Externals • Git Submodules • Vendor Management Scripts
  13. 13. SOLUTIONS • PEAR • SVN Externals • Git Submodules • Vendor Management Scripts Composer!
  14. 14. Isn’t PEAR and the PEAR installer the de-facto PHP package manager?
  15. 15. Isn’t PEAR and the PEAR installer the de-facto PHP package manager? Yep
  16. 16. Why not PEAR? • PEAR tends to be used globally* • Composer is intended to be used in a 
 per-project manner • Spread out channels • Strict Standards * pear can be installed per-project. But we are not gonna talk about it.
  17. 17. 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/
  18. 18. 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
  19. 19. 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…
  20. 20. http://getcomposer.org
  21. 21. Install Composer
  22. 22. Install Composer With PEAR
  23. 23. Install Composer With PEAR For MaximumTrolling
  24. 24. Install Composer With PEAR For MaximumTrolling
  25. 25. Install Composer With PEAR For MaximumTrolling I am kidding!
  26. 26. INSTALLING COMPOSER
  27. 27. INSTALLING COMPOSER Locally
 $ curl -sS https://getcomposer.org/installer | php
  28. 28. INSTALLING COMPOSER Locally
 $ curl -sS https://getcomposer.org/installer | php
  29. 29. INSTALLING COMPOSER Locally
 $ curl -sS https://getcomposer.org/installer | php Globally
 $ curl -sS https://getcomposer.org/installer | php
 $ mv composer.phar /usr/local/bin/composer
  30. 30. NowYou Can Use Composer
  31. 31. I Am Building A Project How Composer can help me?
  32. 32. COMPOSER 101 Getting the dependencies
  33. 33. COMPOSER 101 Getting the dependencies $ cd ~/Desktop/composer-test/
  34. 34. COMPOSER 101 Getting the dependencies $ cd ~/Desktop/composer-test/ $ vi composer.json
  35. 35. COMPOSER 101 Getting the dependencies $ cd ~/Desktop/composer-test/ $ vi composer.json put this in your project root
  36. 36. COMPOSER 101 Getting the dependencies $ cd ~/Desktop/composer-test/ $ vi composer.json put this in your project root
  37. 37. COMPOSER 101 Getting the dependencies $ cd ~/Desktop/composer-test/ $ vi composer.json put this in your project root • Exact version • Range • Wildcard • Tilde
  38. 38. COMPOSER 101 Getting the dependencies $ cd ~/Desktop/composer-test/ $ vi composer.json $ composer.phar install put this in your project root • Exact version • Range • Wildcard • Tilde
  39. 39. Your dependency
  40. 40. Your dependency
  41. 41. Your dependency’s dependencies Your dependency
  42. 42. Your dependency’s dependencies Your dependency
  43. 43. Your dependency’s dependencies Your dependency Suggested Package
  44. 44. Checking Server Requirements I need your server to have these…
  45. 45. File Organisation vendor/autoload.php
  46. 46. UsingThe Libraries
  47. 47. UsingThe Libraries require_once 'vendor/autload.php';
  48. 48. UsingThe Libraries require_once 'vendor/autload.php'; Include the libraries
  49. 49. UsingThe Libraries require_once 'vendor/autload.php'; Include the libraries Start using it
  50. 50. UsingThe Libraries require_once 'vendor/autload.php'; Include the libraries Start using it As simple as that
  51. 51. Installing, Updating And Moving On How does Composer guarantee consistency?
  52. 52. composer.json composer.lock
  53. 53. composer.json composer.lock Metadata and list your dependencies
  54. 54. composer.json composer.lock Metadata and list your dependencies Existing dependencies and current commit hashes
  55. 55. composer.json composer.lock
  56. 56. composer.json composer.lock Update
  57. 57. composer.json composer.lock Update
  58. 58. composer.json composer.lock Update reads
  59. 59. composer.json composer.lock Update reads
  60. 60. composer.json composer.lock Update reads gets latest
  61. 61. composer.json composer.lock Update reads gets latest
  62. 62. composer.json composer.lock Update reads gets latest writes
  63. 63. composer.json composer.lock Update Install reads gets latest writes
  64. 64. composer.json composer.lock Update Install reads gets latest writes
  65. 65. composer.json composer.lock Update Install reads gets latest writes reads
  66. 66. composer.json composer.lock Update Install reads gets latest writes reads
  67. 67. composer.json composer.lock Update Install reads gets latest writes reads compares
  68. 68. composer.json composer.lock Update Install reads gets latest writes reads compares
  69. 69. composer.json composer.lock Update Install reads gets latest writes reads compares gets locked version
  70. 70. KeepTheTeam OnThe Same Page • Commit composer.lock into the repository • Use composer install • Ignore the vendor directory
  71. 71. A Contributor Appears
  72. 72. A Contributor Appears $ git clone git@github.com:tareq1988/composer-test.git
  73. 73. A Contributor Appears $ git clone git@github.com:tareq1988/composer-test.git $ composer install
  74. 74. A Contributor Appears $ git clone git@github.com:tareq1988/composer-test.git $ composer install
  75. 75. A Contributor Appears $ git clone git@github.com:tareq1988/composer-test.git $ composer install
  76. 76. A Contributor Appears $ git clone git@github.com:tareq1988/composer-test.git $ composer install Lock File
  77. 77. Adding A New Package $ composer.phar require symfony/yaml=~2.1.0
  78. 78. Finding A Package
  79. 79. https://packagist.org/
  80. 80. How Do I LoadThe Files? PSR-0 and the modern autoloader
  81. 81. Composer Generates A Autoload File For AllYour Dependencies vendor/autoload.php
  82. 82. Types Of Autoloading
  83. 83. PSR-4 is the recommended way Types Of Autoloading
  84. 84. PSR-4 is the recommended way Types Of Autoloading PSR-0 auto loading
  85. 85. PSR-4 is the recommended way Types Of Autoloading PSR-0 auto loading PEAR packages and other libraries
  86. 86. PSR-4 is the recommended way Types Of Autoloading PSR-0 auto loading PEAR packages and other libraries PHP Functions and helpers
  87. 87. PSR What? PHP FIG: PHP Framework Interop Group PSR-0:Autoloading Standard
 PSR-1: Basic Coding Standard
 PSR-2: Coding Style Guide
 PSR-3: Logger Interface
 PSR-4: Improved Autoloading: http://www.php-fig.org/
  88. 88. PSR What? PHP FIG: PHP Framework Interop Group PSR-0:Autoloading Standard
 PSR-1: Basic Coding Standard
 PSR-2: Coding Style Guide
 PSR-3: Logger Interface
 PSR-4: Improved Autoloading: http://www.php-fig.org/ Post Standards Recommendation
  89. 89. OptimisingThe Autoloader • Adding huge library slows down • It calls file_exists every time
  90. 90. OptimisingThe Autoloader • Adding huge library slows down • It calls file_exists every time $ composer dump-autoload -o
  91. 91. OptimisingThe Autoloader • Adding huge library slows down • It calls file_exists every time $ composer dump-autoload -o
  92. 92. OptimisingThe Autoloader • Adding huge library slows down • It calls file_exists every time $ composer dump-autoload -o Optimize flag
  93. 93. OptimisingThe Autoloader • Adding huge library slows down • It calls file_exists every time $ composer dump-autoload -o Optimize flag A bit slower, but performance boost
  94. 94. I Am Developing A Library How Composer can spread my library around?
  95. 95. https://packagist.org/about
 http://semver.org/
  96. 96. https://packagist.org/about
 http://semver.org/ Name of the library
  97. 97. https://packagist.org/about
 http://semver.org/ Name of the library Description
  98. 98. https://packagist.org/about
 http://semver.org/ Name of the library Description tag
  99. 99. https://packagist.org/about
 http://semver.org/ Name of the library Description tag About you
  100. 100. https://packagist.org/about
 http://semver.org/ Name of the library Description tag About you Autoloader type
  101. 101. I Have A Private Repository Will it work if I am developing applications in-house? • Composer • VCS • PEAR https://getcomposer.org/doc/05-repositories.md
  102. 102. Find Out More http://getcomposer.org https://packagist.org https://github.com/composer composer-dev google group #composer & #composer-dev
  103. 103. Can I Use Composer with WordPress? http://wpackagist.org/ http://composer.rarst.net/resources
  104. 104. Thats All
  105. 105. Tareq Hasan @tareq_cse Founder, Lead Developer - weDevs http://tareq.weDevs.com http://wedevs.com
  106. 106. Thanks

×