Getting Started with Composer

Composer is a package managers for PHP which makes it easy to share and re-use code.

I’m still new to using composer. I’m stumbled across it through Laravel. And I found that if you want to use Laravel, understanding composer is a must. Beyond that, it makes including classes across a project extremely easy.

Quick Tip: You should not commit your vendor directory. This was a mistake I made when first using Laravel. The better solution is to add the vendor directory to your .gitignore file. Then, pull in the dependencies with composer install.

But that’s getting ahead of ourself. Here are some of my notes on getting started with composer…

Installing Composer #

First things first. Go to composer’s download page and copy out the curl command to install composer.

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

This will install a composer.phar file into the current directory. You can think of this file as a PHP archive file.

Running the composer.phar file with php composer.phar, will show the available commands.

Make Globally Available #

Move composer into bin directory to make it globally available.

sudo mv composer.phar /usr/local/bin/composer

Now, you can simply type in composer anywhere, and have access to the utility.

Running a Test #

Create a temp directory

cd ~/
mkdir temp

Use the init command to generate a composer.json file. This file describes the dependencies of your project.

composer init

And answer the questions:

Once completed, listing the files ls will show us our new composer.json file.

{
    "name": "project/temp",
    "description": "Testing out composer",
    "type": "library",
    "authors": [
        {
            "name": "Marty Thomas",
            "email": "marty@tend.io"
        }
    ],
    "minimum-stability": "stable",
    "require": {}
}

Add Packages #

Going to packagist.org will show us thousands of available packages we can add to our app. Let’s add [monolog]https://packagist.org/packages/monolog/monolog) to test.

packagist.png

As the page indicates, we can pull in this package with the following

composer require monolog/monolog

This command pulls in the required files into the vendor directory, and adds it to the required array in composer.json

"require": {
    "monolog/monolog": "^1.16"
}

The Lock File #

It also created a composer.lock file. The point of this file is to lock in the specific versions that your app is using. Typically, you won’t have to worry about this file. read more about the lock file…

Running the composer install command, will reference the lock file, to install the specific versions. This is what you would do on a production environment.

The composer update command will assume that there is no lock file. So this command only looks at your composer.json file, and will update the lock file accordingly with any new versions.

Keep in mind that the lock file should be committed to your git repo, to keep all environments/users in sync.

Autoload Files & Classes #

Now that we know how to pull in dependencies, let’s look at how to auto-load our own classes.

We will do this in two steps.

1) Autoload a file
2) Autoload a class

Autoload a file #

Let’s start by creating a file in the vendor folder called functions.php. And let’s add the following function to that file that will format a date.

<?php
function formatDate($val) 
{
    return date("M d, Y",$val);
}

Now we want to make this function available everywhere in our app.

To test, let’s create a new /index.php file, and add call the function.

<?php formatDate(time()); ?>

However, if we run this script php index.php, it will not work. This is because we need to require the autoloader.

Add the following to the top of the /index.php file:

require 'vendor/autoload.php'; 

Next, Add an autoload object to the composer.json file, and specify the array of files to autoload. (In our case, we only have one file).

"autoload": {
    "files": [
        "vendor/functions.php"
    ]
}

Finally, we need to rebuild our list of auto-load files with the command:

composer dump

This will update the composer/autoload_files.php file with the functions.php command.

Now, running php index.php will result with today’s formatted date.

Autoload a class #

To autoload a class, we will need to add the class to the autoload object like so:

“autoload”: {
“files”: [
“vendor/functions.php”
],
“classmap”: [
“Helper/Today.php”
]
}

Now create a new Helper/Today.php file, and return today’s date:

<?php
    class Today {
        function __construct() 
        {
            echo formatDate(time());
        }
    }

Run composer dump again to refresh to autoload list, we should see the new class referenced in the autoload_classmap.php file.

Finally, update the index.php file with the new class:

<?php 
require 'vendor/autoload.php';

new Today;

Running php index.php will now display today’s formatted date.

 
35
Kudos
 
35
Kudos

Now read this

Samsung Test Drive

The other day, reading through Feedly, I noticed that Sumsung was offering a “Test Drive” of their devices for only $1. The twist was, that you needed to sign up for the Test Drive with an iPhone. Thought that was brilliant! I’ve been... Continue →