Managing Your Drupal Project with Composer
matt glaman | @nmdmatt | mglaman
Matt Glaman
Senior Drupal Consultant @ Commerce Guys
Co-maintainer of Drupal Commerce
Author of Drupal 8 Development Cookbook� http://bit.ly/d8-dev-cookbook
Dependency
Management
It ain't’ new.
PIP
composer
The de facto dependency management tool for PHP
composer.json
defines metadata about the project and dependencies for the project
composer.lock
computed information about dependencies and expected install state
composer install
downloads and installs dependencies�will install off of lock file�if no lock file, acts as update
composer update
updates defined dependencies�rebuilds the lock file�generates autoloader
composer require
adds a new dependency, updates the JSON and .lock file.�updates autoloader
composer remove
removes a dependency, updates the JSON and .lock file�updates autoloader
New to Drupal
Drupal never had dependency management. �Drush kind of helped.�Still a little rocky
Installing Drupal
Install from packaged archive
Drupal.org has a packaging system which provides zip and tar archives.
These archives come with all third party dependencies downloaded.
Install via Composer template
Creates your project in some-dir�Contains vendor and web directory�Follows current standard application structure
https://github.com/drupal-composer/drupal-project
composer create-project drupal-composer/drupal-project:8.x-dev some-dir --stability dev --no-interaction
Adding dependencies to Drupal
Without Composer (or any tools.)
With Composer
composer require drupal/panels
�"require": {
"drupal/panels": "3.0-beta4",
}
Use Composer require command to add the dependency
Your composer.json should have the following
BUT WAIT. There’s currently a catch….
Drupal.org Composer Façade
Repository endpoints provide packages
Drupal.org provides a repository endpoint (beta)
Allows Drupal.org projects to be installed via composer
Allows Drupal.org projects to specify 3rd party libraries
Initial community initative: https://github.com/drupal-composer
Adding the endpoint to composer.json
composer config repositories.drupal composer https://packages.drupal.org/8
Your composer.json will now have
"repositories": {
"drupal": {
"type": "composer",
"url": "https://packages.drupal.org/8"
}
},
Version constraints
Same as everyone else.
semver ftw.
https://blog.madewithlove.be/post/tilde-and-caret-constraints/��https://semver.mwl.be/#?package=drupal%2Fdrupal
~8.2.1: >=8.2.1 <8.3.0��~8.2: >=8.2 <9.0.0��^8.2.1: >=8.2.1 <9.0.0
8.2.0: specifically 8.2.0
Core: 8.2.0, 8.2.1, 8.2.2, 8.3.0-rc1, etc.��Contrib: 8.x-2.0 == 8.2.0, 8.x-2.10 == 8.2.10
Updating dependencies
Without Composer (or any tools.)
With Composer
composer update drupal/panels --with-dependencies
Use Composer update command
--with-dependencies
allows all of the package’s dependencies to be updated
Alternatively, could just run composer update for all the things
Updating Drupal Core
Without Composer (manually)
https://www.drupal.org/docs/8/update/update-procedure-in-drupal-8
With Composer
Modify the shipped composer.json and move drupal/core to a requirement.
"require": {
"composer/installers": "^1.0.21",
"wikimedia/composer-merge-plugin": "~1.3",
"drupal/core": "~8.2"
},
"replace": { },
run composer update drupal/core --with-dependencies and have an up to date Drupal.
With Composer project template
composer update drupal/core --with-dependencies
PATCHES!
Using patch files with Composer
Require cweagans/composer-patches as a dependency�It is a Composer plugin�Specify patches in the extra definition�Applies patches on update and install
"extra": {� "patches": {� "drupal/commerce”: {� "#2805625 Drupal.org": "https://www.drupal.org/files/issues/add_a_new_service_to-2805625-4.patch",� "#2805625: GitHub": "https://github.com/drupalcommerce/commerce/pull/511.patch"� }� }� }
Resources