Composer introduction

Drupal and Composer relationship

Basic usage

slides.com/mikebarkas/drupal-composer

/live

Composer Logo

Why do they use a conductor for their logo?

What is Composer

Composer is an application-level package manager for the PHP programming language that provides a standard format for managing dependencies of PHP software and required libraries.

wat?

It downloads stuff...

Composer

Run it from a shell terminal

Install it via a shell terminal

Composer allows you to declare the libraries your project needs 

and it will manage (install/update) them for you.

The install command reads the composer.json file from the current directory, resolves the dependencies, and installs them into vendor

Where does Composer download from?

A central location where software is stored and managed called a repository.

The software repository is called:

Packagist

Packagist

Packagist is the main Composer repository.

https://packagist.org/

 It aggregates public PHP packages installable with Composer.

  This is where Composer looks to download the packages and libraries for your project.

Composer deals with "packages" or libraries, it manages them on a per-project basis, installing them in a directory named vendor inside your project. 

Dependency Management

By default it will never install anything globally. Thus, it is a dependency manager.

Dependency management is not unique to PHP, Composer, or Drupal

Ruby :         Bundler    http://bundler.io/

Javascript : NPM    https://www.npmjs.com/

PHP :           Composer    https://getcomposer.org/

Composer Links

https://getcomposer.org/

https://packagist.org/

https://getcomposer.org/doc/

https://github.com/composer/composer

Why are you telling me about Composer ?

How does this relate to Drupal ?

Drupal's History

Drupal 4

Drupal 5

Drupal 6

Drupal 7

Functional programming paradigm

Community specific modules

Do it the "Drupal way"

PHP Community

Frameworks

Object oriented programming

Libraries

Composer

Other members of the open-source PHP community

Drupal Initiative

to be more like the rest of the PHP community

The recent initiative by the Drupal community, with regards to version 8.

Increase use of frameworks

More object oriented

Utilize package manager

This is why you may think, Drupal 8 is soo... different.

Drupal meets Composer

+

Drupal Composer

http://drupal-composer.org/

Drupal's Packagist

https://groups.drupal.org/composer

Composer Drupal Group

Drupal Contrib Modules

for working with Composer

Composer

Composer Manager

Composer Generate

Composer Autoload



Composer

Drush utility for Drupal 8 only

Composer Manager

Composer Generate

Composer and Drush

Composer in relation to Drush Make

https://www.drupal.org/node/2471553

drush make   =>

drush down-load   =>

drush pm-update   =>

Compare Drush Make and Composer

composer install

composer require*

composer update

*composer require also updates your composer file

Composer File Structure

project-folder

composer.json

composer.json

This file describes the dependencies of your project and may contain other metadata.

Require - Specify what vendor and package you want.

Package Names - Consists of vendor/package name.

Package Versions - Specify version of module/package. 1.0.*

Some basic commands: 

Using Composer

It is a command line tool

What can composer do?

composer install


composer update

composer.json example

 "require": {
    "drupal/drupal": "7.*",
    "drupal/ctools": "7.1.*",
    "drupal/devel": "7.1.*",
    "drupal/views": "7.3.*",
    "drupal/module_filter": "7.2.*",
    "drupal/admin_menu": "7.3.0-rc5"
  }
"require": {
    "vendor/package": "1.3.2",
    "vendor/package2": "1.*",
    "vendor/package3": "^2.0.3"
}

Helper Modules

Many third-party plugins to extend Composer

"extra": {
    "installer-paths": {
      "www/": ["type:drupal-core"],
      "www/sites/all/modules/contrib/{$name}/": ["type:drupal-module"],
      "www/sites/all/themes/contrib/{$name}/": ["type:drupal-theme"],
      "www/sites/all/libraries/{$name}/": ["type:drupal-library"],
      "www/sites/all/drush/{$name}/": ["type:drupal-drush"],
      "www/profiles/{$name}/": ["type:drupal-profile"]
    },
    "preserve-paths": [
      "web/sites/all/modules/contrib",
      "web/sites/all/themes/contrib",
      "web/sites/all/libraries",
      "web/sites/all/drush",
      "web/sites/default/settings.php",
      "web/sites/default/files"
    ]
  }

Composer Command Events

Composer's command events are scripts that run a specific times.

Scripts are useful for running custom code when needed.

Examples:

pre-install-cmd

post-install-cmd

pre-update-cmd

post-update-cmd

custom-user-defined

Custom Scripts Examples

  "scripts": {
    "post-install-cmd": "scripts/post-install.sh",
    "pre-update-cmd": "scripts/backup-database.sh",
    "install-drupal": "scripts/site-install.sh",
    "generate-content": "scripts/generate-content.sh"
  }

Example Project Layout

drupal-project

composer.json

scripts

database-backups

www

Drupal and Composer

By mikebarkas

Drupal and Composer

Intro to Composer and using it to build a Drupal site.

  • 221