Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Composer The Right Way - 010PHP

2,635 views

Published on

Composer has triggered a renaissance in the PHP community, it has changed the way we deal with other people’s code and it has changed the way we share our code. We are all slowly moving to using Composer, from Wordpress to Joomla and Drupal and frameworks in between. But many of us mistreat composer, follow outdated practices or simply lack a few tricks. In this session i’ll get you the low down on how to use composer the right way.

Published in: Software
  • Be the first to comment

Composer The Right Way - 010PHP

  1. 1. Composer the right way @rdohms Rafael Dohms
  2. 2. photo: smileymanwithahat
  3. 3. photo: Rob Allen jobs.usabilla.com We are hiring!
  4. 4. photo: 18millionpixels
  5. 5. Library Publisher photo: 18millionpixels
  6. 6. Library Publisher Library Consumer photo: 18millionpixels
  7. 7. Library Publisher Library Library Consumer photo: 18millionpixels
  8. 8. Library Publisher Library Library Consumer Packagist photo: 18millionpixels
  9. 9. photo: 18millionpixels
  10. 10. { "require": { "monolog/monolog": “2.0” } } photo: 18millionpixels
  11. 11. { "require": { "monolog/monolog": “2.0” } } { "name": “monolog/monolog” ... } photo: 18millionpixels
  12. 12. { "require": { "monolog/monolog": “2.0” } } { "name": “monolog/monolog” ... } ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Your requirements could not be resolved to an installable set of packages. Problem 1 - The requested package monolog/monolog could not be found in any version, there may be a typo in the package name. Potential causes: - A typo in the package name - The package is not available in a stable-enough version according to your minimum-stability setting see <https://groups.google.com/d/topic/composer-dev/_g3ASeIFlrc/discussion> for more details. Read <http://getcomposer.org/doc/articles/troubleshooting.md> for further common problems. Installation failed, reverting ./composer.json to its original content. photo: 18millionpixels
  13. 13. { "require": { "monolog/monolog": “2.0” } } { "name": “monolog/monolog” ... } ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Your requirements could not be resolved to an installable set of packages. Problem 1 - The requested package monolog/monolog could not be found in any version, there may be a typo in the package name. Potential causes: - A typo in the package name - The package is not available in a stable-enough version according to your minimum-stability setting see <https://groups.google.com/d/topic/composer-dev/_g3ASeIFlrc/discussion> for more details. Read <http://getcomposer.org/doc/articles/troubleshooting.md> for further common problems. Installation failed, reverting ./composer.json to its original content. ___
 ~1.1 photo: 18millionpixels
  14. 14. { "require": { "monolog/monolog": “2.0” } } { "name": “monolog/monolog” ... } ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Your requirements could not be resolved to an installable set of packages. Problem 1 - The requested package monolog/monolog could not be found in any version, there may be a typo in the package name. Potential causes: - A typo in the package name - The package is not available in a stable-enough version according to your minimum-stability setting see <https://groups.google.com/d/topic/composer-dev/_g3ASeIFlrc/discussion> for more details. Read <http://getcomposer.org/doc/articles/troubleshooting.md> for further common problems. Installation failed, reverting ./composer.json to its original content. ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) - Installing psr/log (1.0.0) Loading from cache - Installing monolog/monolog (1.11.0) Downloading: 100% monolog/monolog suggests installing graylog2/gelf-php (Allow sending log messages to a GrayLog2 server) [...] monolog/monolog suggests installing ext-mongo (Allow sending log messages to a MongoDB server) monolog/monolog suggests installing aws/aws-sdk-php (Allow sending log messages to AWS services like DynamoDB) monolog/monolog suggests installing rollbar/rollbar (Allow sending log messages to Rollbar) Writing lock file Generating autoload files ___
 ~1.1 photo: 18millionpixels
  15. 15. composer
  16. 16. composercomposer.json!
  17. 17. composercomposer.json! composer.lock!
  18. 18. composercomposer.json! composer.lock!
  19. 19. discovery Packagist " composercomposer.json! composer.lock!
  20. 20. discovery sourcePackagist " Repository # composercomposer.json! composer.lock!
  21. 21. discovery source installation Packagist " Vendor
 Folder $ Repository # composercomposer.json! composer.lock!
  22. 22. $ composer self-update Production $ composer self-update --snapshot Development $ composer self-update --preview CI / Build
  23. 23. $ composer init Library Publisher
  24. 24. $ composer init Library Publisher $ composer require <vendor>/<package> Library Consumer
  25. 25. Install instructions
  26. 26. Install instructions 1. Edit your composer.json
  27. 27. Install instructions 1. Edit your composer.json 2. Add `package/vendor`
  28. 28. Install instructions 1. Edit your composer.json 2. Add `package/vendor` 3. Run `composer update`
  29. 29. Install instructions 1. Edit your composer.json 2. Add `package/vendor` 3. Run `composer update` or was that install? err… i can’t remember.
  30. 30. $ composer require monolog/monolog Tip
  31. 31. $ composer require monolog/monolog Tip Using version ^1.11 for monolog/monolog
  32. 32. $ composer require monolog/monolog --sort-packages Tip
  33. 33. $ composer require monolog/monolog 
 --prefer-stable --prefer-lowest --no-scripts Tip
  34. 34. $ composer remove monolog/monolog Tip
  35. 35. Library Publishers Composer for
  36. 36. Library Publisher Unique vendor names vendor / package
  37. 37. Library Publisher Unique vendor names pick your own 
 unique vendor
  38. 38. Library Publisher Unique vendor names or join a collective
  39. 39. README! CHANGELOG% LICENSE& Library Publisher What’s in your library?
  40. 40. README! CHANGELOG% LICENSE& Library Publisher What’s in your library? ' What problem does it solve? ' Usage examples ' Install instructions ' How can I contribute?
  41. 41. README! CHANGELOG% LICENSE& Library Publisher What’s in your library? ' List relevant changes ' Make BC breaks prominent ' Show examples of how to upgrade ' What problem does it solve? ' Usage examples ' Install instructions ' How can I contribute?
  42. 42. README! CHANGELOG% LICENSE& Library Publisher What’s in your library? ' List relevant changes ' Make BC breaks prominent ' Show examples of how to upgrade ' Pick one that reflects your values ' choosealicense.com can help ' What problem does it solve? ' Usage examples ' Install instructions ' How can I contribute?
  43. 43. Library Publisher Semantic Versioning 1 . 2 3.major minor patch
  44. 44. Library Publisher Semantic Versioning 1 . 2 3.major minor patch Bug Fixes
  45. 45. Library Publisher Semantic Versioning 1 . 2 3.major minor patch New Features Bug Fixes
  46. 46. Library Publisher Semantic Versioning 1 . 2 3.major minor patch API / BC Breaks New Features Bug Fixes
  47. 47. Library Publisher Tagging Please, tag your releases.
  48. 48. Library Publisher Tagging Please, tag your releases. often!
  49. 49. http://phppackagechecklist.com/ The PHP Package
 Checklist Library Publisher The Checklist
  50. 50. Library Publisher “I'm out" photo: nickwebb
  51. 51. Library Publisher “I'm out"
  52. 52. Library Publisher “I'm out" Click here and provide an alternative
  53. 53. Library Publisher “I'm out"
  54. 54. Pick the 
 correct 
 version Library Publisher Pick a Version
  55. 55. *the asterisk Library Publisher Pick a Version
  56. 56. *the asterisk○Library Publisher Pick a Version
  57. 57. ~the tilde Library Publisher Pick a Version
  58. 58. ~1.2 >=1.2.0, <2.0.0 Library Publisher Pick a Version
  59. 59. ~1.2 >=1.2.0, <2.0.0 Library Publisher Pick a Version )
  60. 60. ~1.2.3 >=1.2.3, <1.3 Library Publisher Pick a Version
  61. 61. ~1.2.3 >=1.2.3, <1.3 Library Publisher Pick a Version )
  62. 62. Yo @rdohms, I’m really happy for you and I’m gonna let you finish, but the tilde operator is totally old news.
  63. 63. ^the caret Library Publisher Pick a Version
  64. 64. 1.2.3 >=1.2.3, <2.0.0 Library Publisher Pick a Version ^
  65. 65. 1.2.3 >=1.2.3, <2.0.0 Library Publisher Pick a Version ) ^ )
  66. 66. 0.3.0 >=0.3.0, <0.4.0 Library Publisher Pick a Version ^
  67. 67. 0.3.0 >=0.3.0, <0.4.0 Library Publisher Pick a Version ^ )
  68. 68. 0.3.0 >=0.3.0, <0.4.0 Library Publisher Pick a Version ^ ) Major Version Zero
  69. 69. "require": { "zendframework/zend-stdlib": “2.1.*”, "zendframework/zend-servicemanager": "2.1.*", }, “require-dev": { “phpunit/phpunit": “~3.7” } Library Publisher Pick a Version "require": { "zendframework/zend-stdlib": “^2.3”, "zendframework/zend-servicemanager": “^2.3", }, “require-dev": { “phpunit/phpunit": “~4” } 3rd party library Your application
  70. 70. "require": { "zendframework/zend-stdlib": “2.1.*”, "zendframework/zend-servicemanager": "2.1.*", }, “require-dev": { “phpunit/phpunit": “~3.7” } Library Publisher Pick a Version "require": { "zendframework/zend-stdlib": “^2.3”, "zendframework/zend-servicemanager": “^2.3", }, “require-dev": { “phpunit/phpunit": “~4” } 3rd party library Your application *
  71. 71. "require": { "zendframework/zend-stdlib": “2.1.*”, "zendframework/zend-servicemanager": "2.1.*", }, “require-dev": { “phpunit/phpunit": “~3.7” } Library Publisher Pick a Version "require": { "zendframework/zend-stdlib": “^2.3”, "zendframework/zend-servicemanager": “^2.3", }, “require-dev": { “phpunit/phpunit": “~4” } 3rd party library Your application *
  72. 72. "require": { "zendframework/zend-stdlib": “2.1.*”, "zendframework/zend-servicemanager": "2.1.*", }, “require-dev": { “phpunit/phpunit": “~3.7” } Library Publisher Pick a Version "require": { "zendframework/zend-stdlib": “^2.3”, "zendframework/zend-servicemanager": “^2.3", }, “require-dev": { “phpunit/phpunit": “~4” } 3rd party library Your application * ^2.1
  73. 73. Library Consumers Composer for
  74. 74. Library Consumer Install or update? install or update?
  75. 75. Library Consumer Install or update? Make sure you have installed the last updates from other developers. ? install updateor
  76. 76. Library Consumer Install or update? Make sure you have installed the last updates from other developers. ? install updateor
  77. 77. Library Consumer Install or update? Deploying a new release of your application to production. ? install updateor
  78. 78. Library Consumer Install or update? Deploying a new release of your application to production. ? install updateor
  79. 79. Library Consumer Install or update? Checked out a new project and want to start coding. ? install updateor
  80. 80. Library Consumer Install or update? Checked out a new project and want to start coding. ? install updateor
  81. 81. Library Consumer Install or update? Grab new versions for the dependencies of your project. ? install updateor
  82. 82. Library Consumer Install or update? Grab new versions for the dependencies of your project. ? install updateor
  83. 83. Your application $ Public Repository # Library Consumer Behind the scenes with install/update composer.lock! composer install composer update
  84. 84. Your application $ Public Repository # Library Consumer Behind the scenes with install/update composer.lock! read composer install composer update
  85. 85. Your application $ Public Repository # Library Consumer Behind the scenes with install/update composer.lock! read grab version composer install composer update
  86. 86. Your application $ Public Repository # Library Consumer Behind the scenes with install/update composer.lock! read grab version check latest compatible release composer install composer update
  87. 87. Your application $ Public Repository # Library Consumer Behind the scenes with install/update composer.lock! read grab version check latest compatible release update composer install composer update
  88. 88. Your application $ Public Repository # Library Consumer Behind the scenes with install/update composer.lock! read grab version check latest compatible release update composer install composer update no lock file? composer update
  89. 89. Library Consumer The .lock issue commit your lock file*
  90. 90. Library Consumer The .lock issue commit your lock file* * always! even for libraries! :D
  91. 91. Library Consumer The .lock issue commit your lock file* * always! even for libraries! :D ** we will discuss that over beer.
  92. 92. Tip since beta1 Library Consumer Updating interactively
  93. 93. $ composer update —interactive (-i) Tip since beta1 Library Consumer Updating interactively
  94. 94. CONFLICT (content): Merge conflict in composer.lock Automatic merge failed; fix conflicts and then commit the result. Library Consumer Merge Conflict
  95. 95. CONFLICT (content): Merge conflict in composer.lock Automatic merge failed; fix conflicts and then commit the result. Library Consumer Merge Conflict
  96. 96. # First. # Get the valid, clean external version of the lock $ git checkout origin/<target> -- composer.lock composer.json # Second. # Replay your changes, whatever they were $ composer require <new/package> --update-with-dependencies $ composer update <existing/package> # Third. # Commit your changes $ git add . $ git commit. # Profit! Solving Merge Conflicts Library Consumer Solving Merge Conflicts
  97. 97. Pick the 
 correct 
 version Library Consumer Version Selection
  98. 98. Library Consumer Version Selection dev-master
  99. 99. Library Consumer Version Selection
  100. 100. Library Consumer Version Selection
  101. 101. Tip since beta1
  102. 102. $ composer why-not <package> doctrine/doctrine-bundle 1.6.2 requires symfony/framework-bundle (~2.3|~3.0) doctrine/doctrine-cache-bundle 1.3.0 requires symfony/doctrine-bridge (~2.2|~3.0) doctrine/doctrine-fixtures-bundle 2.3.0 requires symfony/doctrine-bridge (~2.3|~3.0) doctrine/doctrine-migrations-bundle dev-master requires symfony/framework-bundle (~2.3|~3.0) doctrine/migrations dev-master requires symfony/yaml (~2.3|~3.0) doctrine/orm v2.5.4 requires symfony/console (~2.5|~3.0) friendsofsymfony/rest-bundle 1.7.7 requires symfony/http-kernel (^2.3.24|~3.0) incenteev/composer-parameter-handler v2.1.2 requires symfony/yaml (~2.3|~3.0) jms/serializer-bundle 1.1.0 requires symfony/framework-bundle (~2.3|~3.0) knplabs/knp-paginator-bundle 2.5.1 requires symfony/framework-bundle (~2.3|~3.0) kriswallsmith/assetic v1.3.2 requires symfony/process (~2.1|~3.0) Tip since beta1
  103. 103. $ composer outdated composer/composer 1.0.3 1.1.0 filp/whoops 2.1.0 2.1.2 justinrainbow/json-schema 1.6.1 2.0.3 nikic/fast-route v0.7.0 v1.0.0 oscarotero/psr7-middlewares v3.14.3 v3.15.1 Tip
  104. 104. Library Consumer Using forks found a bug?
  105. 105. Library Consumer Using forks $ composer install --prefer-source Tip
  106. 106. Library Consumer Using forks
  107. 107. Library Consumer Using forks patch it,
  108. 108. Library Consumer Using forks patch it, fork it,
  109. 109. Library Consumer Using forks patch it, fork it, push it…
  110. 110. Library Consumer Using forks patch it, fork it, push it… wait for it.
  111. 111. Library Consumer Using forks patch it, fork it, push it… wait for it.
  112. 112. Library Consumer Using forks symfony/symfony
  113. 113. Library Consumer Using forks symfony/symfony rdohms/symfony
  114. 114. Library Consumer Using forks symfony/symfony rdohms/symfony "repositories": [ { "type": "vcs", "url": "https://github.com/rdohms/symfony" } ]
  115. 115. Library Consumer Using forks symfony/symfony rdohms/symfony "repositories": [ { "type": "vcs", "url": "https://github.com/rdohms/symfony" } ] +
  116. 116. Library Consumer Using forks Do not put forked repositories
 on packagist ,
  117. 117. Library Consumer Using forks "require": { "symfony/symfony": "dev-my-patch as 2.5.0" } Tip
  118. 118. Library Consumer Composer and deployments Composer and production
  119. 119. Library Consumer Composer and deployments $ composer install --prefer-dist --no-dev --optimize-autoloader ,
  120. 120. Library Consumer Composer and deployments $ composer install --prefer-dist --no-dev --optimize-autoloader ,
  121. 121. Library Consumer Composer and deployments $ composer install --prefer-dist --no-dev --optimize-autoloader , - Install same versions - Uses information defined in the composer.lock file
  122. 122. $ composer install --prefer-dist --no-dev --optimize-autoloader Library Consumer Composer and deployments ,
  123. 123. $ composer install --prefer-dist --no-dev --optimize-autoloader Library Consumer Composer and deployments , - Downloads distribution packages - Can use local cache for previously downloaded - No git required
  124. 124. $ composer install --prefer-dist --no-dev --optimize-autoloader Library Consumer Composer and deployments ,
  125. 125. $ composer install --prefer-dist --no-dev --optimize-autoloader Library Consumer Composer and deployments , - Avoids download unnecessary developer libraries
  126. 126. $ composer install --prefer-dist --no-dev --optimize-autoloader Library Consumer Composer and deployments ,
  127. 127. $ composer install --prefer-dist --no-dev --optimize-autoloader Library Consumer Composer and deployments , - Generates classmap from PSR-0/4 autoloaders - Speeds up autoloading
  128. 128. Library Consumer Licensing $ composer licenses Name: __root__ Version: 1.0.0 Licenses: none Dependencies: doctrine/annotations v1.2.1 MIT doctrine/cache v1.3.1 MIT doctrine/collections v1.2 MIT doctrine/common v2.4.2 MIT doctrine/inflector v1.0 MIT doctrine/lexer v1.0 MIT psr/log 1.0.0 MIT symfony/symfony v2.5.6 MIT twig/twig v1.16.2 BSD-3-Clause Tip
  129. 129. Library Consumer Simulating Environments Tip Production Server . Development Server .
  130. 130. Library Consumer Simulating Environments Tip Production Server . Development Server .PHP 7.0 PHP 5.6
  131. 131. Library Consumer Simulating Environments "config": { "platform": { "php": “5.6.2”, “ext-mongodb”: “1.1” } }, Tip Production Server . Development Server .PHP 7.0 PHP 5.6
  132. 132. Library Consumer Simulating Environments $ composer install —-ignore-platform-reqs Tip
  133. 133. Library Consumer Private Packages and Proxy Satis and Toran
  134. 134. composer Packagist " Your application $ Public Repository # Library Consumer Private Packages and Proxy
  135. 135. composer Packagist " Your application $ Public Repository # Satis / Toran " Private Repository / proxy Library Consumer Private Packages and Proxy
  136. 136. Library Consumer Private Packages and Proxy toranproxy.com private repos, automatic packagist proxy and support composer development
  137. 137. Library Consumer Tooling $ composer require jquery/jquery How many of you?
  138. 138. Library Consumer Tooling
  139. 139. Library Consumer Tooling
  140. 140. Javascript only Developer Library Consumer Tooling 0 my-js-library 1
  141. 141. Javascript only Developer Library Consumer Tooling 0 my-js-library ! package.json 1
  142. 142. Javascript only Developer Library Consumer Tooling 0 my-js-library ! package.json ! bower.json 1
  143. 143. Javascript only Developer Library Consumer Tooling 0 my-js-library ! composer.json ! package.json ! bower.json 1
  144. 144. Javascript only Developer Library Consumer Tooling 0 my-js-library ! composer.json ! package.json ! bower.json ! .gemspec 1
  145. 145. Javascript only Developer Library Consumer Tooling 0 my-js-library ! composer.json ! package.json ! bower.json ! .gemspec 1 2 2
  146. 146. Library Consumer Tooling
  147. 147. Library Consumer Tooling
  148. 148. Library Consumer Tooling Right tool.
 Right job.
  149. 149. Library Consumer One last thing One last thing…
  150. 150. Library Consumer pickle!
  151. 151. Library Consumer pickle! $ pickle install memcache https://wiki.php.net/rfc/pickle https://github.com/FriendsOfPHP/pickle
  152. 152. Thank you. jobs.usabilla.com We are hiring!
  153. 153. Thank you.any questions? 3 http://slides.doh.ms 4 http://doh.ms 5 @rdohms jobs.usabilla.com We are hiring!

×