Introduction
Composer is used for Package Management in PHP. It gives the facility for you to declare libraries of your own as well as custom libraries and it will install them in your project for you.
Composer is not a package manager or a library manager alone but it manages your library for a per project basis so it is actually a dependency manager.
Composer is highly inspired by node’s npm and ruby’s bundler.
The solution provided by Composer?
a) My project depends on PHPUnit Library.
b)PHPUnit Library depend on other libraries.
c)The things on which My project depends is declared
d) Composer look for which versions of which packages is required to be installed, and installs them (Meaning it downloads them into My project).
Declaring dependencies
Let’s conside the senarion in which you are developing a project, and library is required that does unit Testing as well as Integration Testing. PHPUnit is decided by you to use. In order to add it to your project, all you require to do is to make a composer.json file which outlines the project’s dependencies.
We are simply stating that our project requires phpunit/phpunit package, any version beginning with 3.7.14
System Requirements For Using Composer
Composer requires PHP 5.3.2+ to run. A few php settings and compile flags are also needed, but the installer will advise you about any incompatibilities.
If packages is to be installed from sources instead of simple zip archives, you require git(a distributed revision control), svn(version control system) or hg depending on how the package is version-controlled.
Composer is multi-platform and we can make it run equally well on Windows, Linux ,OSX.
Installation – *nix
Downloading the Composer Executable
Locally
To actually get Composer, we need to do two things.
The step one is to install Composer(again, which means downloading it into your project):
$ curl -sS https://getcomposer.org/installer | php
Or download composer.phar into your project root.
Install Dependencies
Execute this in your project root.
php composer.phar install
This will just check a few PHP settings and then download composer.phar to your working directory. This file is the Composer binary. It is a PHAR (PHP archive), which is an archive format for PHP which can be run on the command line, along with other things.
The Composer can be installed to a specific directory by using the –install-dir option and providing a target directory (it can be an absolute or relative path):
$ curl -sS https://getcomposer.org/installer | php — –install-dir=bin
Globally
The composer file can be placed wherever you desire. If you put it in your PATH, it can be accessed globally. On unixy machines it can be made executable and invoke it without php.
These commands can be run to easily access composer from anywhere on your system:
$ curl -sS https://getcomposer.org/installer | php
$ mv composer.phar /usr/local/bin/composer
Note: If any of the above commands did not work due to permissions, run the mv command line again with sudo.
Then, simply run composer in order to run Composer instead of php composer.phar.
Globally (on OSX via homebrew)
Composer is a portion of the homebrew-php project.
1. Tap the homebrew-php repository to the brew installation if it is not done so yet: brew tap josegonzalez/homebrew-php
2. Run brew install josegonzalez/php/composer.
3. Use Composer with the composer command.
Note: If you get an error saying PHP53 or higher is missing , Kindly use this command to install php brew install php53-intl
Installation – Windows
Using the Installer
This is the best way of getting Composer set up on your Windows Machine.
Download and run Composer-Setup.exe, this will install the latest version of Composer and set up your PATH so that you just need to call composer from any directory in your command line.
Manual Installation
Change to a directory on your PATH and run the install snippet to get composer.phar downloaded to your machine:
C:Usersusername>cd C:bin
C:bin>php -r “readfile(‘https://getcomposer.org/installer’);” | php
Note: If the above command fails due to readfile, try to use the http url or kindly enable php_openssl.dll in php.ini
Make a new composer.bat file alongwith composer.phar:
C:bin>echo @php “%~dp0composer.phar” %*>composer.bat
Close your current terminal. Test usage with a new terminal:
C:Usersusername>composer -V
Composer version 27d8904
C:Usersusername>
Using Composer
We require Composer to install the project dependencies.
To resolve and download dependencies, run the install command:
$ php composer.phar install
If a global install is done and if you do not have the phar in that directory run this instead:
$ composer install
Autoloading
Apart from downloading the library, Composer also makes an autoload file that’s capable of autoloading all of the classes in any of the libraries that it downloads. To make use of it, following line needs to be added to your code’s bootstrap process:
require ‘vendor/autoload.php’;
Inorder to Run the PHPunit you need to execute the command vendo/bin/phpunit from your project Document root.
While writing tests for my project I needed to install phpdocumentor for my project for more code readability. So I need to just add a line in my composer.json(refer above in Declaring dependencies)
and just give $ composer update.
With this I got the phpdocumentor installed in my project.
Conclusion
Composer thus helps us in rapid PHP development making us free of thinking about which all dependencies need to be installed and also gives our project a package format so that it can be made to a finished software product easily with all the Software methodologies inbuilt in that.
Happy Composer!!!
Sources:
http://getcomposer.org/doc/00-intro.md