Composer

Vijaya Chandran Mani

@vijaycs85

Introduction

What

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

Why

Package A 

Package B

Package C

Package D

Package E

1.0.0

2.0.0

1.0.0

1.2.0

4.0.0

drupal/core

guzzlehttp/guzzle

guzzlehttp/promises

^6.3

^1.0

composer show --tree

^8.8

How

composer

Repository

packagist.org

Repository

packages.drupal.org/8

Implementation

Commands

  • init
  • create
  • install

Type

{
    "name": "myorg/mysite",
    "type": "project",
    "require": {
        "myorg/mymodule": "^1"
    }
}
{
    "name": "myorg/mysite",
    "type": "project",
    "require": {
        "composer/installers": "^1.0.24",
        "drupal/core": "^8.8"
    },
    "extra": {
        "installer-paths": {
            "core": ["type:drupal-core"],
            "libraries/{$name}": ["type:drupal-library"],
            "modules/contrib/{$name}": ["type:drupal-module"],
            "profiles/contrib/{$name}": ["type:drupal-profile"],
            "themes/contrib/{$name}": ["type:drupal-theme"],
            "drush/Commands/contrib/{$name}": ["type:drupal-drush"],
            "modules/custom/{$name}": ["type:drupal-custom-module"],
            "themes/custom/{$name}": ["type:drupal-custom-theme"]
        },
    }
}

Version

~ vs ^ vs *

"require": {
    "drupal/core": "8.8.2", // exactly 8.8.2

    // >, <, >=, <= | specify upper / lower bounds
    "drupal/core": ">=8.7.2", // anything above or equal to 8.7.2
    "drupal/core": "<8.7.2", // anything below 8.7.2

    // * | wildcard
    "drupal/core": "8.7.*", // >=8.7.0 <8.8.0

    // ~ | allows last digit specified to go up
    "drupal/core": "~8.7.2", // >=8.7.2 <8.8.0
    "drupal/core": "~8.7", // >=8.7.0 <9.0.0

    // ^ | doesn't allow breaking changes (major version fixed - following semver)
    "drupal/core": "^8.7.2", // >=8.7.2 <9.0.0
    "drupal/core": "^0.3.2", // >=0.3.2 <0.4.0 // except if major version is 0
}

json vs lock

composer.

Demo

See also...

Advanced

Repository

Demo

Scripts

Demo

See also...

1. pre/post

2. PHPUnit

 

 

Plugin

Demo: Drupal-layout

Drupal

Assets

Demo

Drupal/core

Best practices

Common Issues

The requested package is satisfiable by x.y.x but these conflict with your requirements or minimum-stability.

Update lock files

References