Author: Manuel Lemos
Posted on: 2013-12-12
Categories: PHP Tutorials, New site features
Read this article to learn how it works, how you can install packages from PHP Classes and JS Classes in projects that may also include packages from other repository sites.
Contents
What is Composer?
Installing Composer
Installing PHP Classes Packages with Composer
Automatic Login In Private Repositories that Require Authentication
Autoloading Installed PHP Classes
Installing JavaScript, CSS, Images and other Files Under your Web Document Root
Providing Your Packages in the PHP Classes Composer Repository
Automatic Package Versioning
More Reputation for Authors of Packages in the Composer Repository
Next Developments
What is Composer?
Composer is a PHP program created by Jordi Boggiano and Nils Aderman that can download and install packages of files from one or more repositories.
It can also determine which other packages a package may depend. If necessary, it can also download and install the dependency packages.
Installing Composer
Composer is mainly a tool meant to be used from the command line shell. So, to use it you need to have access to a shell console of the machine on which you want to install your packages.
First you need to install Composer. Composer is an application made of many PHP scripts but it is distributed as a single file named composer.phar. PHAR is a special archive format that may include multiple PHP scripts. It has support built-in since PHP 5.2.
You can obtain the latest Composer version from its site:
http://getcomposer.org/composer.phar
Alternatively you can also download and run the installer script. This script will perform additional checks to determine if your PHP has all that is necessary to run Composer. Just execute the following line in the shell:
php -r "eval('?>'.file_get_contents( 'https://getcomposer.org/installer' ));"
Installing PHP Classes Packages with Composer
You can install one or more packages from PHP Classes, JS Classes or other Composer repository sites.
The first thing you need to do is to create a text file in your project directory named composer.json.
Composer is a project mostly inspired in NPM (Node.js Package Manager). So practically all the configuration files it needs must be in the JSON format.
To make it simpler for you, PHP Classes and JS Classes generate a sample composer.json file for each package available in the Composer repository. Just go in the page of the package you want to install and click on the Install with Composer link.
For instance if you go to the page of the Forms Generation and Validation package and click on the Install with Composer link, it will show a sample of the composer.json file like this:
{ "require": { "phpclasses/formsgeneration": ">=1.0.0" }, "repositories": [ { "type": "composer", "url": "http:\/\/www.phpclasses.org\/" }, { "packagist": false } ] }
Basically it tells Composer that your project needs to install the package with base name formsgeneration with version number at least 1.0.0.
All packages in the PHP Classes site have the prefix phpclasses/ to not confuse with packages from different repositories. So the requires section lists a package named phpclasses/formsgeneration .
The repositories section lists all repositories from where you want to retrieve your packages. The URL for the PHP Classes Composer repository is obviously http://www.phpclasses.org/ and for JS Classes is http://www.jsclasses.org/ .
By default Composer would fetch packages from the Packagist repository. That is another repository maintained by Composer developers.
In this sample composer.json, only the phpclasses/formsgeneration is needed. So it includes a section setting "packagist": false to make it faster when looking for packages only from other repositories. If you need to include in your project packages from Packagist, you need to remove this section.
There are other options that you can include in your project composer.json file. Those in this sample file are sufficient to get you started.
Now you are ready to retrieve and install the required packages. You can tell Composer to install the packages using a shell command like this:
php composer.phar install
If you want to update the list of packages to be installed, you should run a shell command like this:
php composer.phar update
Automatic Login In Private Repositories that Require Authentication
As you may have noticed, when installing some packages from the PHP Classes or JS Classes repositories, composer requests a user name and a password.
The login requirement is an option of the author of each package. So, some packages may require it, others don't. This requirement is useful, so when a package is updated, the users that downloaded it may be able to receive notifications by email, so they can upgrade to a new version eventually with more features and less bugs.
When the login is required, by default you need to enter your user name in PHP Classes or JS Classes sites. However the password is not the same as your user password in the site. The password you need to enter there is shown in the package repository page above the sample composer.json file.
This password is the same to install any packages in the same site. So the password is the same for all PHP Classes packages. For JS Classes it is the same password for downloading any packages from there, but it is different from the one you will use in PHP Classes.
Every time you need to install any packages, you need to enter the same user name and password. This is a bit tedious. Therefore there is a way to set the user name and password in a configuration file.
By the time this article is being written, this feature of reading user names and passwords from a configuration file is not yet built-in the latest Composer version.
There is a patch for that created by Stephan Hochdoerfer of bitExpert. Until this patch is included in the main Composer distribution, you can use a Composer plugin package that can automate the login using the user name and password read from the authentication configuration file.
The authentication configuration file is named auth.json. That file needs to be created in your Composer project directory. This article explains better how it works.
The actual auth.json file must have a format like this:
{ "config": { "basic-auth": { "www.phpclasses.org": { "username": "your user name here", "password": "your php classes composer password here" }, "www.jsclasses.org": { "username": "your user name here", "password": "your js classes composer password here" } } } }
Autoloading Installed PHP Classes
Once you have installed your packages, you are ready to use them in your applications.
One of the good things about Composer is that it automatically generates class loader script for all the classes that were installed.
All you need to do is to include this line in the beginning of your application PHP scripts:
require('vendor/autoload.php');
For instance with the example of the Forms Generation and Validation class above, after this line you can just create an object without needing anymore require statements.
$form = new form_class;
You may be wondering how the autoloader generated by Composer knows what are the names of the classes and the names of the class files to include.
There are a several ways Composer autoloader may find the classes. In the case of classes installed from the PHPClasses and JSClasses Composer repositories, the sites tell Composer which are the files in the packages that contain classes. Then Composer parses these files, extract the class names, and creates a PHP script with an array that maps class names to the respective class files.
Installing JavaScript, CSS, Images and other Files Under your Web Document Root
However, some packages contain JavaScript, CSS, or image files that need to be installed inside your application Web document root directory.
It is possible to make Composer install specific package files in other directories outside the vendor/ directory. Currently this can be achieved with this Composer plugin package.
This topic was covered in more detail in the JS Classes site blog. If you are not yet a user of the JS Classes site, you may consider register there so you can be notified by email when the blog post is published. If you are a PHP Classes user already, just go to the registration page to instantly become a user of JS Classes too.
Providing Your Packages in the PHP Classes Composer Repository
If you are an author of classes published in PHP Classes and JS Classes, it is interesting to make your packages available for installation from these sites Composer repositories. This way your packages may become more visible and popular because many users prefer to install the packages using Composer instead of going through a manual archive download process.
Currently, your packages do not become automatically available in the Composer repository. For now, the sites require that you intentionally add your package to the Composer repository. The idea is to educate authors by encouraging them to learn about a good practice, which is to adopt systematic package installation tool like Composer.
Adding your packages to the Composer repository is very easy. Just go on the page of any of your packages and click on the Install with Composer link. Then click on the Add packages to repository link to add any of your packages.
If you have multiple packages in the site, you can also add all your packages to the Composer repository at once. Just click on the link that says Add all your packages to a repository.
This is all you need to do. You can also add a file to your package named composer.json with additional definitions, like dependencies on packages from other repositories or different autoloading rules, but that is not mandatory. The site will create a composer.json file for packages that do not have one.
Automatic Package Versioning
Each package that you add to the Composer repository must have a version number. This is necessary to let Composer determine if the installed packages are up to date. If a package does not have a version number, the site will set it automatically to 1.0.0.
From then on, every time you add, update or remove files to a package, the site will bump the version number by increasing the lower digit of the version. So after version 1.0.0, it becomes 1.0.1, and so on.
If the bumped version number is not correct according to your numbering criteria, you can fix the version number manually in the package details editing page.
More Reputation for Authors of Packages in the Composer Repository
As mentioned above, adopting a package installer like Composer is a good practice that every good developer should embrace.
Therefore, the PHP Classes and JS Classes sites are now giving additional reputation points to class authors that add their packages to the Composer repository. That will increase the position of each author in the reputation ranking, as well the perception that the author is qualified.
If you have linked your site account with Facebook or Twitter, the site will also post messages in your timeline to let your friends know that your packages are avaliable for installation from the Composer repository. That can help increasing the visibility of your packages and attract more users.
Next Developments
Composer support was one of the features that were announced earlier this year in June when the PHP Classes site completed 14 years of age.
As you may have read, several improvements were planned to be implemented. I would have liked to implement all at once, but unfortunately I have to deal with one at a time.
This fact creates a challenge, which is to determine what is more important to implement first?
I tend to listen to what the users are saying to evaluate what can be more urgent. Still the decision on what should I implement next is always a guess. This is a bit frustrating because later I may realize that some things should have been done first.
So for now my guess is that the next development will be to improve the site navigation, specifically the part of browsing packages and their file contents to reduce the number of clicks to let users do what they want to do.
I will also fix bugs and pending issues related with other parts of the site, but the new thing that I plan to work on next will be the package navigation.
I do not know exactly what I will do but I have a few ideas. If you have concrete suggestions regarding the package navigation, and also about Composer support, or other needed improvements, please post a comment to this article.
If you would like to influence about the future of the site, your opportunity to do it is now. Go ahead and send your comments.
You need to be a registered user or login to post a comment
1,409,575 PHP developers registered to the PHP Classes site.
Be One of Us!
Login Immediately with your account on:
Comments:
1. composer password - evident (2016-09-18 05:52)
composer password... - 3 replies
Read the whole comment and replies