Composer - The missing package manager for PHP

113
COMPOSER The missing package manager for PHP

description

I gave the talk in on 6th June, 2014 at Digital World developer conference at Bangabandhu International Conference Center (BICC) in Dhaka, Bangladesh.

Transcript of Composer - The missing package manager for PHP

COMPOSERThe missing package manager for PHP

PyPi

Common Web Languages

Communities inside PHP

SHARING WASN’T CARING• Autoloading PHP Classes

• Dependencies?

• Where does this thing go?

WHY BOTHER?• As a developer, you want to use consistent

version of libraries/dependencies and tools

• Consistent versioning across all team members

• Share your own library

WHY BOTHER?More developers, more dependency problem

PHPUnit 3.6.10 Behat 2.3.5 Monolog 1.0

PHPUnit 3.4.15 Behat 2.2 Monolog 1.2

Hasin Rifat

SOLUTIONS• PEAR

• SVN Externals

• Git Submodules

• Vendor Management Scripts

SOLUTIONS• PEAR

• SVN Externals

• Git Submodules

• Vendor Management Scripts

SOLUTIONS• PEAR

• SVN Externals

• Git Submodules

• Vendor Management Scripts

SOLUTIONS• PEAR

• SVN Externals

• Git Submodules

• Vendor Management Scripts

SOLUTIONS• PEAR

• SVN Externals

• Git Submodules

• Vendor Management Scripts

SOLUTIONS• PEAR

• SVN Externals

• Git Submodules

• Vendor Management Scripts

Composer!

Isn’t PEAR and the PEAR installer the de-facto PHP package manager?

Isn’t PEAR and the PEAR installer the de-facto PHP package manager?

Yep

Why not PEAR?• PEAR tends to be used globally*

• Composer is intended to be used in a per-project manner

• Spread out channels

• Strict Standards

* pear can be installed per-project. But we are not gonna talk about it.

COMMON SOLUTION$ cd ~/projects/my_awesome_project/vendor/$ mkdir Behat && cd Behat$ wget https://github.com/downloads/Behat/Behat/behat.phar$ cd ../../$ php vendor/Behat/behat.phar tests/features/

COMMON SOLUTION$ cd ~/projects/my_awesome_project/vendor/$ mkdir Behat && cd Behat$ wget https://github.com/downloads/Behat/Behat/behat.phar$ cd ../../$ php vendor/Behat/behat.phar tests/features/

Or use a git submodule… or an SVN external if it’s still 2007

COMMON SOLUTION$ cd ~/projects/my_awesome_project/vendor/$ mkdir Behat && cd Behat$ wget https://github.com/downloads/Behat/Behat/behat.phar$ cd ../../$ php vendor/Behat/behat.phar tests/features/

Or use a git submodule… or an SVN external if it’s still 2007

BUT, we can do better…

http://getcomposer.org

Install Composer

Install Composer

With PEAR

Install Composer

With PEAR

For Maximum Trolling

Install Composer

With PEAR

For Maximum Trolling

Install Composer

With PEAR

For Maximum TrollingI am kidding!

INSTALLING COMPOSER

INSTALLING COMPOSERLocally $ curl -sS https://getcomposer.org/installer | php

INSTALLING COMPOSERLocally $ curl -sS https://getcomposer.org/installer | php

INSTALLING COMPOSERLocally $ curl -sS https://getcomposer.org/installer | php

Globally $ curl -sS https://getcomposer.org/installer | php$ mv composer.phar /usr/local/bin/composer

Now You Can Use Composer

I Am Building A ProjectHow Composer can help me?

COMPOSER 101Getting the dependencies

COMPOSER 101Getting the dependencies

$ cd ~/Desktop/composer-test/

COMPOSER 101Getting the dependencies

$ cd ~/Desktop/composer-test/$ vi composer.json

COMPOSER 101Getting the dependencies

$ cd ~/Desktop/composer-test/$ vi composer.json put this in your project root

COMPOSER 101Getting the dependencies

$ cd ~/Desktop/composer-test/$ vi composer.json put this in your project root

COMPOSER 101Getting the dependencies

$ cd ~/Desktop/composer-test/$ vi composer.json put this in your project root

• Exact version • Range • Wildcard • Tilde

COMPOSER 101Getting the dependencies

$ cd ~/Desktop/composer-test/$ vi composer.json

$ composer.phar install

put this in your project root

• Exact version • Range • Wildcard • Tilde

Your dependency

Your dependency

Your dependency’s dependencies

Your dependency

Your dependency’s dependencies

Your dependency

Your dependency’s dependencies

Your dependency

Suggested Package

Checking Server Requirements

I need your server to have these…

File Organisation

vendor/autoload.php

Using The Libraries

Using The Libraries

require_once 'vendor/autload.php';

Using The Libraries

require_once 'vendor/autload.php';

Include the libraries

Using The Libraries

require_once 'vendor/autload.php';

Include the libraries

Start using it

Using The Libraries

require_once 'vendor/autload.php';

Include the libraries

Start using it

As simple as that

Installing, Updating And Moving On

How does Composer guarantee consistency?

composer.json composer.lock

composer.json composer.lock

Metadata and list your dependencies

composer.json composer.lock

Metadata and list your dependencies

Existing dependencies and current commit hashes

composer.json composer.lock

composer.json composer.lock

Update

composer.json composer.lock

Update

composer.json composer.lock

Update

reads

composer.json composer.lock

Update

reads

composer.json composer.lock

Update

reads

gets latest

composer.json composer.lock

Update

reads

gets latest

composer.json composer.lock

Update

reads

gets latest

writes

composer.json composer.lock

Update Install

reads

gets latest

writes

composer.json composer.lock

Update Install

reads

gets latest

writes

composer.json composer.lock

Update Install

reads

gets latest

writes

reads

composer.json composer.lock

Update Install

reads

gets latest

writes

reads

composer.json composer.lock

Update Install

reads

gets latest

writes

reads

compares

composer.json composer.lock

Update Install

reads

gets latest

writes

reads

compares

composer.json composer.lock

Update Install

reads

gets latest

writes

reads

compares

gets locked version

Keep The Team On The Same Page

• Commit composer.lock into the repository • Use composer install

• Ignore the vendor directory

A Contributor Appears

A Contributor Appears$ git clone [email protected]:tareq1988/composer-test.git

A Contributor Appears$ git clone [email protected]:tareq1988/composer-test.git

$ composer install

A Contributor Appears$ git clone [email protected]:tareq1988/composer-test.git

$ composer install

A Contributor Appears$ git clone [email protected]:tareq1988/composer-test.git

$ composer install

A Contributor Appears$ git clone [email protected]:tareq1988/composer-test.git

$ composer install

Lock File

Adding A New Package$ composer.phar require symfony/yaml=~2.1.0

Finding A Package

https://packagist.org/

How Do I Load The Files?PSR-0 and the modern autoloader

Composer Generates A Autoload File For All Your

Dependencies

vendor/autoload.php

Types Of Autoloading

PSR-4 is the recommended way

Types Of Autoloading

PSR-4 is the recommended way

Types Of Autoloading

PSR-0 auto loading

PSR-4 is the recommended way

Types Of Autoloading

PSR-0 auto loading

PEAR packages and other libraries

PSR-4 is the recommended way

Types Of Autoloading

PSR-0 auto loading

PEAR packages and other libraries

PHP Functions and helpers

PSR What?

PHP FIG: PHP Framework Interop Group

PSR-0: Autoloading StandardPSR-1: Basic Coding StandardPSR-2: Coding Style Guide PSR-3: Logger Interface PSR-4: Improved Autoloading:

http://www.php-fig.org/

PSR What?

PHP FIG: PHP Framework Interop Group

PSR-0: Autoloading StandardPSR-1: Basic Coding StandardPSR-2: Coding Style Guide PSR-3: Logger Interface PSR-4: Improved Autoloading:

http://www.php-fig.org/

Post Standards Recommendation

Optimising The Autoloader

• Adding huge library slows down • It calls file_exists every time

Optimising The Autoloader

• Adding huge library slows down • It calls file_exists every time

$ composer dump-autoload -o

Optimising The Autoloader

• Adding huge library slows down • It calls file_exists every time

$ composer dump-autoload -o

Optimising The Autoloader

• Adding huge library slows down • It calls file_exists every time

$ composer dump-autoload -o

Optimize flag

Optimising The Autoloader

• Adding huge library slows down • It calls file_exists every time

$ composer dump-autoload -o

Optimize flag

A bit slower, but performance boost

I Am Developing A LibraryHow Composer can spread my library around?

https://packagist.org/abouthttp://semver.org/

https://packagist.org/abouthttp://semver.org/

Name of the library

https://packagist.org/abouthttp://semver.org/

Name of the library

Description

https://packagist.org/abouthttp://semver.org/

Name of the library

Description

tag

https://packagist.org/abouthttp://semver.org/

Name of the library

Description

tag

About you

https://packagist.org/abouthttp://semver.org/

Name of the library

Description

tag

About you

Autoloader type

I Have A Private RepositoryWill it work if I am developing applications in-house?

• Composer

• VCS

• PEAR

https://getcomposer.org/doc/05-repositories.md

Find Out More

http://getcomposer.org

https://packagist.org

https://github.com/composer

composer-dev google group

#composer & #composer-dev

Can I Use Composer with WordPress?

http://wpackagist.org/ http://composer.rarst.net/resources

Thats All

Tareq Hasan

@tareq_cse

Founder, Lead Developer - weDevs

http://tareq.weDevs.com

http://wedevs.com

Thanks