Managing your Drupal project with Composer

Post on 16-Jan-2017

55 views 1 download

Transcript of Managing your Drupal project with Composer

Managing Your Drupal Project with

Composermatt glaman | @nmdmatt | mglaman

Matt GlamanSenior Drupal Consultant @ Commerce Guys

Co-maintainer of Drupal Commerce

Author of Drupal 8 Development Cookbookhttp://bit.ly/d8-dev-cookbook

DependencyManagement

It ain't’ new.

PIP

composerThe de facto dependency management tool for PHP

composer.jsondefines metadata about the project and dependencies for the project

composer.lockcomputed information about dependencies and expected install state

composer installdownloads and installs dependencieswill install off of lock fileif no lock file, acts as update

composer updateupdates defined dependenciesrebuilds the lock filegenerates autoloader

composer requireadds a new dependency, updates the JSON and .lock file.updates autoloader

composer removeremoves a dependency, updates the JSON and .lock fileupdates autoloader

New to DrupalDrupal 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

composer create-project drupal-composer/drupal-project:8.x-dev some-dir --stability dev --no-interaction

Creates your project in some-dirContains vendor and web directoryFollows current standard application structure

https://github.com/drupal-composer/drupal-project

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

https://packages.drupal.org/8

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

Matt Glaman
How do we know what the packages are named and available on drupal.org?
Matt Glaman
but with Drupal.org ... it's hard to know what is available.
Matt Glaman
version naming

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

~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.0Same as everyone else.

semver ftw.

https://blog.madewithlove.be/post/tilde-and-caret-constraints/

https://semver.mwl.be/#?package=drupal%2Fdrupal

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 updatedAlternatively, could just run composer update for all the things

Updating Drupal Core

Without Composer (manually)

1. Download latest 8.x.x archive

2. Remove your core and vendor directories

3. Make note of changes to .htaccess, composer.json, or robots.txt.

4. Apply archive contents to site

5. Re-apply changes to .htaccess, composer.json, or robots.txt.

6. Run update.php

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 dependencyIt is a Composer plugin

Specify patches in the extra definitionApplies 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

Managing Your Drupal Project with Composerhttps://glamanate.com/blog/managing-your-drupal-project-composer

Drupal Composer project templatehttps://github.com/drupal-composer/drupal-project

Drupal Commerce project templatehttps://github.com/drupalcommerce/project-base

Platform.sh Drupal 8 + Composer template examplehttps://github.com/platformsh/platformsh-example-drupal/tree/8.x

Amazee Labs Composer recipes (examples of what we covered.)https://www.amazeelabs.com/en/blog/drupalcomposerrecipes

Using Drupal + Composer project templates with Pantheon siteshttps://pantheon.io/blog/using-composer-relocated-document-root-pantheon