Drupal + composer = new love !?

38
Drupal + Composer = New Love !?

Transcript of Drupal + composer = new love !?

Drupal + Composer = New Love !?

About me

● Wolfgang Ziegler● @the_real_fago auf Twitter● drupal.org/u/fago● CEO of drunomics

Wolfgang Ziegler // fago

● More than 10 years of Drupal experience● Drupal Core Developer and Maintainer der Entity &

Form Subsysteme● Leading force behind the Drupal 8 Entity Field API

improvements

Wolfgang Ziegler // fago

Creator and Maintainer of modules like● Entity API● Rules● Profile2● Field Collection

We are hiring!

PHP / Drupal 8 Developer Talk to me!

http://getcomposer.org

Overview● Dependency manager for PHP● Use and publish packages● Package-metadata: composer.json● Repository: packagist.org

PHP Namespaces<?phpnamespace MyProject\Component;

use Symfony\Component\HttpFoundation\Request;

class SomeClass {  public function someMethod(Request $request) {    if ($request­>getPort() != 80) {      throw new \LogicException("Application may work on port 80 only.");    }  }}

composer.json{

"name": "drupal/drupal",

"description": "Drupal is an open source content management....",

"type": "drupal­core",

"require": {

"php": ">5.4.4­13",

"twig/twig": "1.15.*",

"symfony/class­loader": "2.5.*",

"symfony/dependency­injection": "2.5.*",

"symfony/event­dispatcher": "2.5.*",

"symfony/http­foundation": "2.5.*",

"symfony/http­kernel": "2.5.*",

"symfony/routing": "2.5.*",

"symfony/validator": "2.5.*",

Using a package

composer require guzzle/guzzle 

Providing a package● Add composer.json● Tag versions Semantic versioning→

● Add Git repository to packagist.org

Packagist

Using it in a project

git clone ­­branch 8.1.x https://git.drupal.org/project/drupal.git

cd drupal

composer install

What happens?● Downloads code and puts it into ./vendor

(or somewhere else)● Generates a class autoloader● Writes lock file → composer.lock

Composer update & locks● composer update

– Update all the dependencies and write new lock file

● composer install – Apply what's recorded in the lock file

Composer & VCS● Commit composer.json● For projects: Commit composer.lock● For libraries: Commit composer.lock for

tests● Avoid commit vendor files

Versions & Composer● Semantic: Major, minor & patch versions● Require versions like 7.3.* to only get bug

fixes● composer supports many different version

constraints● Do not specify a concrete version, use the lock

file

Versions & Composer● Semantic: Major, minor & patch versions● Require versions like 7.3.* to only get bug

fixes● composer supports many different version

constraints● Do not specify a concrete version, use the lock

file

Composer & Drupal● getting off the island● object oriented● OOP basics are necessary

Components in Drupal 8● Symfony components (...)● Twig● PHPUnit● Guzzle● ZendFeed● Doctrine (Annotations)

Composer & Drupal 8.1

● ./vendor is gone!● composer install is your friend!● Nothing changes for tarballs

Prediction● Most Drupal 8 sites will require composer● Drupal commerce is going to depend on

composer● Rules 8.x might do so also● and more will follow

Composer & Drupal Projects● Composer replaces drush make● composer install drush make makefile↔● Support for Drupal modules via:

– https://packagist.drupal­composer.org/

● Custom repositories● Extendable via scripts

http://drupal-composer.org/

Start a new d8 project

● github.com/drupal­composer/drupal­project

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

What does it do?● Adds Drupal into the „web“ dir● Adds folder structure, drush, Drupal console,

…● Custom projects can add more stuff and/or

customize things● Requires own packagist/satis

Drupal modules & ./vendor?● Composer package types:

– Library– Project– Drupal-module– Drupal-Theme– Drupal-profile– Drupal-drush

Drupal 7● New project? Check out →github.com/drupal­composer/drupal­project

● Existing project? composer_generate→drupal.org/project/composer_generate

● Or just use it for adding vendors!

Adding vendors● Composer Manager

– Aggregates composer.json of modules● wikimedia/composer-merge-plugin

– Include composer.json of modules using a wildcard

Gotchas● Prefer „dist“ packages for fast builds● „composer require“ instead of drush dl +

editing make files● Patches? cweagans/composer-patches→

or just fork & add your repo

Edit your vendors

● Delete vendor files / the module●  composer install ­­prefer­source

Update a single modulecomposer update drupal/module

composer update drupal/module  ­­with­depdendencies

Merge conflicts?

composer update ­­lock

composer update nothing

Deploy from Git?● Commit vendor...● Or just commit vendor on release● Watch out for packages installed from

source● Use composer scripts to update Git

Deployments

● Add project specific drush● Use composer scripts or robo● Requires composer / robo on the server or

added to the repo only

composer scripts  "scripts": {

    "drush": "drush ­­local ­­include=web/sites/all/drush/drudrush ­r web",

    "deploy": "drush ­­local ­­include=web/sites/all/drush/drudrush ­r web deploy",

  },

composer scripts

● Uses vendor binaries!● React on events like

– Post-package-install– Post-create-project-cmd– ..

● Reference other scripts via @script

Questions?