Manage Dependency with Composer

 
Manage Dependency with Composer

PHP ComposerProject ruled with libraries

Our development require many libraries which are helpful on performing operation without rewriting it. We may require a payment gateway api code in projects or to connect and post to facefook we may require a facebook SDK. Some time the library that we may require need some other libraries to be present. Also project may require unit testing for which there will be more classes used. Testing project with coding standard tools also need libraries into project.

Our project that involves many libraries may be developed by more coders and for that we have push all the libraries into repository. When the library version get changed we have to push that into repository.

To overcome this limitation we have the dependency management system like ‘composer’. It uses the json format to describe our libraries dependency and we can install in any location as well as update when version changed. Most modern php resuable code libraries as well as frameworks start to follow composer model.

Setup Composer

Composer available as executable Phar archive. You should ensure ‘Phar’ extension enabled in php.ini

You can download the executable Phar directly from this location //getcomposer.org/download/

Alternatively we have a installer script that can be downloaded in command prompt

>curl -s https://getcomposer.org/installer | php
or
>php -r "readfile('https://getcomposer.org/installer');"

After validating the requirement it downloads the Phar executable.

Download Dependencies

After installation we can use ‘composer’ to get some packages into our project. We need to make a composer configuration file(composer.json).

If we need slim framework into the project we can use the following code to make the ‘composer.json’.

{
 "require": {
 "slim/slim": "2.*"
 }
}

To download the package we can use the following install command in our project directory where configuration file is created.

>php composer.phar install

To update the package we can use following command

>php composer.phar update

Packages are placed into “./vendor” project directory along with some generated code for autoloading. If a package has dependency it get downloaded automatically.

Packagist

‘Composer’ maitains a common repository namely Packagist. It’s the central repository where the packages mentioned in json are downloaded. We can create our own library and can be packaged into the repository. Most handful library and frameworks entered into composer that can be downloaded easily.

You can search for the packages you need and that can be referred in our json definition file.

Autoloading

To make our life easy ‘composer’ generates a autoloader in ‘PSR-O’ format. So we can simply require ‘autoload.php’ into our php script and start using the library classes without a need to require that manually.

// Autoload 
require 'vendor/autoload.php';

// Create Slim instance
$application = new \Slim\Slim();
// Define a route handler
$app->get('/home/:start', function ($mode) {
 echo "Application Started";
});

// Execute the instance
$app->run();

Get more help on composer working here.

Ayya Samy
Latest posts by Ayya Samy (see all)