PHP Training in Coimbatore|PHP training Coimbatore|PHP certification
Composer - The missing package manager for PHP
-
Upload
tareq-hasan -
Category
Software
-
view
3.210 -
download
7
description
Transcript of Composer - The missing package manager for PHP
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
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…
INSTALLING COMPOSERLocally $ curl -sS https://getcomposer.org/installer | php
Globally $ curl -sS https://getcomposer.org/installer | php$ mv composer.phar /usr/local/bin/composer
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
Using The Libraries
require_once 'vendor/autload.php';
Include the libraries
Start using it
As simple as that
composer.json composer.lock
Metadata and list your dependencies
Existing dependencies and current commit hashes
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$ git clone [email protected]:tareq1988/composer-test.git
A Contributor Appears$ git clone [email protected]:tareq1988/composer-test.git
$ composer install
Lock File
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
$ 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
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
Tareq Hasan
@tareq_cse
Founder, Lead Developer - weDevs
http://tareq.weDevs.com
http://wedevs.com