Managing your Drupal project with Composer

32
Managing Your Drupal Project with Composer matt glaman | @nmdmatt | mglaman

Transcript of Managing your Drupal project with Composer

Page 1: Managing your Drupal project with Composer

Managing Your Drupal Project with

Composermatt glaman | @nmdmatt | mglaman

Page 2: Managing your Drupal project with Composer

Matt GlamanSenior Drupal Consultant @ Commerce Guys

Co-maintainer of Drupal Commerce

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

Page 3: Managing your Drupal project with Composer

DependencyManagement

It ain't’ new.

PIP

Page 4: Managing your Drupal project with Composer
Page 5: Managing your Drupal project with Composer

composerThe de facto dependency management tool for PHP

Page 6: Managing your Drupal project with Composer

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

Page 7: Managing your Drupal project with Composer

composer.lockcomputed information about dependencies and expected install state

Page 8: Managing your Drupal project with Composer

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

Page 9: Managing your Drupal project with Composer

composer updateupdates defined dependenciesrebuilds the lock filegenerates autoloader

Page 10: Managing your Drupal project with Composer

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

Page 11: Managing your Drupal project with Composer

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

Page 12: Managing your Drupal project with Composer
Page 13: Managing your Drupal project with Composer

New to DrupalDrupal never had dependency management. Drush kind of helped.Still a little rocky

Page 14: Managing your Drupal project with Composer

Installing Drupal

Page 15: Managing your Drupal project with Composer

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.

Page 16: Managing your Drupal project with Composer

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

Page 17: Managing your Drupal project with Composer

Adding dependencies to Drupal

Page 18: Managing your Drupal project with Composer

Without Composer (or any tools.)

Page 19: Managing your Drupal project with Composer

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….

Page 20: Managing your Drupal project with Composer

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
Page 21: Managing your Drupal project with 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" } },

Page 22: Managing your Drupal project with Composer

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

Page 23: Managing your Drupal project with Composer

Updating dependencies

Page 24: Managing your Drupal project with Composer

Without Composer (or any tools.)

Page 25: Managing your Drupal project with Composer

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

Page 26: Managing your Drupal project with Composer

Updating Drupal Core

Page 27: Managing your Drupal project with Composer

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

Page 28: Managing your Drupal project with Composer

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.

Page 29: Managing your Drupal project with Composer

With Composer project template

composer update drupal/core --with-dependencies

Page 30: Managing your Drupal project with Composer

PATCHES!

Page 31: Managing your Drupal project with Composer

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

Page 32: Managing your Drupal project with Composer

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