Composer - The Missing Package manager for PHP

Love Huria

@LoveHuria

Ashutosh Singh

   @0ashutosh0

Born and raised in Kaithal, Haryana, India

Senior Web Developer (Drupal/PHP, Javascript, CSS3, HTML5)

Born and raised in Bhopal, MP India

Team Lead

What Are We Going to Cover:

  • What is Composer?
  • Why bother..
  • Composer Installation
  • Useful commands
  • Setting up a Drupal instance using Composer

Composer - "one who puts together"

Per-project PHP dependency manager

It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

Packages maintained on https://packagist.org/

Released in 2012

Confused?

ok, Let's break it down!

Example

A

B

Projects

C

D

E

C

D

F

G

Both projects can have respective versions of a library

installed

Problem:

- To use consistent versions of the dependencies in a project across all team        members

Pear - Can't install a package on per-project basis

Git submodules/subtrees - Not really documented/supported well

Composer!

Solution:

- Share your own library

Why bother with Composer?

Composer Installation (Linux/OSX)

$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ php -r "if (hash_file('SHA384', 'composer-setup.php') 
=== '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a
858088ffbc1f233e9b180f061')
 { echo 'Installer verified'; } else { echo 'Installer corrupt';
 unlink('composer-setup.php'); } echo PHP_EOL;"
$ php composer-setup.php
$ php -r "unlink('composer-setup.php');"

Visit: http://dcg-workshop.surge.sh/

Useful Commands

$ composer init

There is no need to create the composer.json file manually, There is also an init command available that makes it a bit easier to do this.

$ composer init
$ vi package.json

Useful Commands

$ composer install

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

Useful Commands

$ composer update

Get the latest versions of the dependencies and this will resolve all dependencies of the project and write the exact versions into composer.lock

$ composer update vendor/package

If you just want to update a few packages and not all.

Useful Commands

$ composer require

The require command adds new packages to the composer.json file from the current directory. If no file exists one will be created on the fly.

$ composer require vendor/package

If you do not want to choose requirements interactively, you can just pass them to the command.

Useful Commands

$ composer self-update

To update Composer itself to the latest version, just run the self-update command. It will replace your composer.phar with the latest version.

Installing Drupal 8

<Thank You/>

Questions?

Overview of Composer & Drush

By Love Huria

Overview of Composer & Drush

Composer - The missing package manager for PHP

  • 969