introduction to composer for drupal
TRANSCRIPT
Introduction to Composer
for Drupal
Drupal Camp Japan in Tokyoとは 2017
About me
Luc Bezier, Drupal & Open Source freelance.
France 出身です.
Drupal development since 2010.
URL: webizat.com
Organizer of Drupalcamp Cebu (Philippines)
Worked with BBC Worldwide, UK Government agency, Ben&Jerry’s, European commission …
Solution Architect & Consultant
Architecture and drupal developmenton large projects.
Promet Source and Composer + Drupal
Using Drupal with Composer since 2014.
Drupal 7, Drupal 8.
What is Composer?
Dependency Manager for PHP.
Declare the libraries of your project and Composer will manage (install/update) them for you.
これはいくらですか?How much does it cost?
Composer is Free and Open source.By Nils Adermann and Jordi Boggiano in 2012.
Not only Drupal, used in other PHP projects like Symfony.
BTW, the logo?
Could this man be a composer too, but what he is doing is conducting.
Why using Composer?
Never miss a dependency again.
Clear view on your project’s requirements.
Smaller amount of data in your git repo.
Makes maintenance easier.
Why using Composer?
Drupal 8 adopted Composer.
Contributed modules are using composer to manage their PHP depencencies.
Composer commands
$ composer installCreates a resource (vendor) folder and downloads all
dependencies.
$ composer updateUpdates the dependencies according to composer.json.
Composer commands
$ composer requireAdds a new dependency.
$ composer removeRemoves a dependency.
Composer commands
Debugging?
$ composer install -vvvVerbose mode will let you see what is composer doing.
Composer’s files
Simply remember
composer.json: list of dependencies for the project.
composer.lock: expected install state.
composer.json
…"require": {
"drupal/core": "~8.0","drush/drush": "^8.1",
"drupal/console": "~0.10","drupal/field_group": "~8.1@alpha",
"drupal/honeypot": "^8.1","drupal/google_analytics": "^8.2",
"drupal/avatars": "^8.1@beta"
},...
composer.json
Composer.json uses versions.
"drupal/core": "~8.0",
~8.0 is equivalent to >=8.0 <9.0
The ~ will keep our version as Drupal 8.
Doc: https://getcomposer.org/doc/articles/versions.md
composer.lock
The file composer.lock contains all informations about your dependencies, including which exact version is in
use.
"name": "drupal/core","version": "8.1.10","source": { "type": "git", "url": "https://github.com/drupal-composer/drupal-core.git", "reference": "9562f733cdefd735337bf827b3ba5ad031aba4c3"},
Composer and git
Commit composer.json and composer.lock
Do not commit the vendor folder. It would defeat the purpose of using composer to manage your
dependencies.
Composer for Drupal
Drupal 8 tarball from drupal.org ships with a composer.json and composer.lock files.
Better way: composer templates.
Composer for Drupal
Two templates to use composer with Drupal.
drupal/drupal
drupal-composer/drupal-project
(https://www.drupal.org/node/2718229)
Composer for Drupal
I recommend the drupal-composer template.
Quick installation via:$ composer create-project drupal-composer/drupal-project
https://github.com/drupal-composer/drupal-project
Composer for Drupal
Using the template for Drupal projects, files used by Drupal (such as modules) will be placed at the right
location.
Core -> web/core
Contrib modules -> web/modules/contrib
Contrib themes -> web/themes/contrib
No composer template Drupal Composer template
web
Example, the core folder is not in git.
Composer for Drupal
Need a new module?
$ composer require drupal/pathauto
Enable the module.
Composer for Drupal
Update all dependencies (can be slow).$ composer update
Update Drupal core.$ composer update drupal/core --with-dependencies
Update a module.$ composer update drupal/panels --with-dependencies
Composer for Drupal
Patches?Use composer-patches
https://github.com/cweagans/composer-patches"extra": { "patches": { "drupal/date": { "7.2.9": [ { "title": "Fix an issue with undefined index: show_remaining_days in date_field_formatter_view()", "url": "https://www.drupal.org/files/issues/date-show_remaining_days_notice-2469189-1.patch" } ] }, },}
Warning, this is the sad part.
Composer is slow.
助けて!
Composer performance
Composer is slow.
Composer will check all the available tags when updating a dependency for example.
The more dependencies, the slower. That can be an issue with some Drupal projects.
Composer performance
Update specific packages, not everything.
$ composer update drupal/panels --with-dependencies
Not
$ composer update
Composer performance
You are using Vagrant? Execute composer from your local, not the VM.
You are using Docker?Execute composer from your local, not the container.
When possible, use the latest version of PHP to execute composer.
PHP 7 (https://secure.php.net/releases/)
Composer performance
Other solutions (I haven’t tried)?
Local mirror of the packages using Satis*getcomposer.org/doc/articles/handling-private-packages-with-satis.md
Parallel download with prestissimo**github.com/hirak/prestissimo
* Thanks to Johnnie Fox for sharing.** Thanks Koyama Tetsuji for sharing.
Composer performance
Most Drupal Platform as a Service (PaaS) (Acquia, Pantheon ...) do not yet support composer.
Check the documentation.
Composer and Drupal
My blog articlewebizat.com/blog/composer-for-drupal
(feel free to comment!)
Composer and Drupal
Great article by Matt Glamanhttps://glamanate.com/blog/managing-your-drupal-pr
oject-composer
Useful links and resources:
https://github.com/drupal-composer/drupal-project
https://github.com/cweagans/composer-patches
Test it with Expresso PHP
Docker for PHP developers
Simple and easy to understand. Original images only.
Nginx + PHP (7 / 5)Apache + PHP (7 / 5)
github.com/expresso-php/expresso-php
ありがとう
Twitter @luukyb
email: luc [at] webizat.com
web: webizat.com