introduction to composer for drupal

36
Introduction to Composer for Drupal Drupal Camp Japan in Tokyo とは 2017

Upload: luc-bezier

Post on 11-Feb-2017

216 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Introduction to Composer for Drupal

Introduction to Composer

for Drupal

Drupal Camp Japan in Tokyoとは 2017

Page 2: Introduction to Composer for Drupal

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 …

Page 3: Introduction to Composer for Drupal

Solution Architect & Consultant

Architecture and drupal developmenton large projects.

Page 4: Introduction to Composer for Drupal

Promet Source and Composer + Drupal

Using Drupal with Composer since 2014.

Drupal 7, Drupal 8.

Page 5: Introduction to Composer for Drupal

What is Composer?

Dependency Manager for PHP.

Declare the libraries of your project and Composer will manage (install/update) them for you.

Page 6: Introduction to Composer for Drupal

これはいくらですか?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.

Page 7: Introduction to Composer for Drupal

BTW, the logo?

Could this man be a composer too, but what he is doing is conducting.

Page 8: Introduction to Composer for Drupal

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.

Page 9: Introduction to Composer for Drupal

Why using Composer?

Drupal 8 adopted Composer.

Contributed modules are using composer to manage their PHP depencencies.

Page 10: Introduction to Composer for Drupal

Composer commands

$ composer installCreates a resource (vendor) folder and downloads all

dependencies.

$ composer updateUpdates the dependencies according to composer.json.

Page 11: Introduction to Composer for Drupal

Composer commands

$ composer requireAdds a new dependency.

$ composer removeRemoves a dependency.

Page 12: Introduction to Composer for Drupal

Composer commands

Debugging?

$ composer install -vvvVerbose mode will let you see what is composer doing.

Page 13: Introduction to Composer for Drupal

Composer’s files

Simply remember

composer.json: list of dependencies for the project.

composer.lock: expected install state.

Page 14: Introduction to Composer for Drupal

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"

},...

Page 15: Introduction to Composer for Drupal

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

Page 16: Introduction to Composer for Drupal

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"},

Page 17: Introduction to Composer for Drupal

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.

Page 18: Introduction to Composer for Drupal

Composer for Drupal

Drupal 8 tarball from drupal.org ships with a composer.json and composer.lock files.

Better way: composer templates.

Page 19: Introduction to Composer for Drupal

Composer for Drupal

Two templates to use composer with Drupal.

drupal/drupal

drupal-composer/drupal-project

(https://www.drupal.org/node/2718229)

Page 20: Introduction to Composer for Drupal

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

Page 21: Introduction to Composer for Drupal

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

Page 22: Introduction to Composer for Drupal

No composer template Drupal Composer template

web

Example, the core folder is not in git.

Page 23: Introduction to Composer for Drupal

Composer for Drupal

Need a new module?

$ composer require drupal/pathauto

Enable the module.

Page 24: Introduction to Composer for Drupal

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

Page 25: Introduction to Composer for Drupal

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" } ] }, },}

Page 26: Introduction to Composer for Drupal

Warning, this is the sad part.

Page 27: Introduction to Composer for Drupal

Composer is slow.

助けて!

Page 28: Introduction to Composer for Drupal

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.

Page 29: Introduction to Composer for Drupal

Composer performance

Update specific packages, not everything.

$ composer update drupal/panels --with-dependencies

Not

$ composer update

Page 30: Introduction to Composer for Drupal

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/)

Page 31: Introduction to Composer for Drupal

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.

Page 32: Introduction to Composer for Drupal

Composer performance

Most Drupal Platform as a Service (PaaS) (Acquia, Pantheon ...) do not yet support composer.

Check the documentation.

Page 33: Introduction to Composer for Drupal

Composer and Drupal

My blog articlewebizat.com/blog/composer-for-drupal

(feel free to comment!)

Page 35: Introduction to Composer for Drupal

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

Page 36: Introduction to Composer for Drupal

ありがとう

Twitter @luukyb

email: luc [at] webizat.com

web: webizat.com