PHP Dependency Management Tool – Composer and Packagist

Published on: Sep 25, 2016 4:06 pm  /   Published by:   /   Comments:

Composer is an application-level PHP dependency management tool. It will seamlessly install and update the packages that you declare as your PHP project’s dependencies. Composer pulls the latest packages from the PHP package repository, Packagist. However, it is not a package manager. Nils Adermann and Jordi Boggiano have developed the tool and first released it on March, 2012. Composer manages the packages within a specific directory by default, not globally. It does have a global command and support for the global project as well. In general, composer handles package dependencies per project basis. The idea behind Composer is based on node‘s npm concept.

What is a library or a package?

Being in PHP programming, you know that everyone searches on the internet for a readily available class file to achieve a functionality. So that way we don’t have to code some functionality on our own. Therefore we could save some time. Remember the early age techniques of including a third party class file in your PHP project for handling different requirements like the file upload or to export a data grid into an excel file format or send an email using third party SMTP! Things are not that simple anymore. Now you have to handle User Authentication with social media login support, Request Routing with custom routing pattern matching, File Uploading with extension restriction and remote storage support etc. It is good that the functionalities are still readily available on the internet, however, the fact is, the single class files have now become packages including multiple class files and directories.

What is a PHP dependency management tool

Different code repositories now have a handful of these PHP packages available for you to just pick and use. It would be a pain if you had to manually do that job of including the packages to your project. Here comes the concept of dependency manager for PHP! You generally need to have a central configuration file where you would mention the names and versions of the packages you need in your project. The dependency manager will grab and put those packages in a separate directory under your project. Even the packages that you need might have a dependency on other third party packages. The PHP dependency management tool will take care of that too in a chain so that any dependent package doesn’t miss its dependency.

Composer as a Dependency Manager for PHP

Composer requires at least PHP 5.3.2+ to run. Also, it runs in CLI mode and installs the dependency libraries for a PHP application. Most of all open-source PHP projects including Laravel use composer as a part of the project.

Composer PHP Dependency Manager – Installation and Usage

You need to download the Composer tool locally into your project directory from the official download page. This is the standard installation process mentioned below:

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

If that doesn’t work, run the following commands.

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

It will download the composer.phar to your working directory. Then you can run the command php composer.phar to run composer. If you want to have composer installed globally, run this command mv composer.phar /usr/local/bin/composer. Now instead of php composer.phar, you can simply run composer. For testing, you can run composer -V to check the installed composer version. If you need to update the composer software, run composer self-update

Now all you need to have is the dependency management file which is generally a composer.json file with content like below:

{
"require": {
"vendor/package": "1.3.2",
"vendor/package2": "1.*",
"vendor/package3": "^2.0.3"
}
}

This is the file where you mention all the required packages. The package names can be found from the Packagist. Your composer.json file should finally look something like below:

{
"name": "acme/hello-world",
"type": "project",
"require": {
"monolog/monolog": "1.0.*",
"guzzlehttp/guzzle": "5.1.0",
"jenssegers/agent": "~2.1",
"maatwebsite/excel": "1.*"
}
}

First of all, have the composer.json file ready with the required dependencies mentioned. Finally run the composer update command. As a result, it will install all the mentioned libraries and their dependencies as well (if any) in the /vendor/ directory of your project. Consequently it will create a /vendor/autoload.php file as well which is a optimized class loader file. You now just need to include the autoload file, i.e. require 'vendor/autoload.php'; and you’re done!

 

layers