Creating a new composer packageist package

Composer is a package manager for PHP, similar in purpose to NodePM for NodeJS. The getting started section is pretty good at pulling in other packages, but I had a hard time getting to “ok, I want to manage my project with composer”. I finally found a post from Junior Grossi which got me started. Enthusiasts will probably have more to say, but it comes down to 4 parts:

  1. Your source
  2. Composer.json
  3. The autoloader
  4. Required packages

Your source

Your source, and the autoloader go together, but I’ll discuss them separately. Basically your source has to be laid out to work with the PSR-0 autoloader. You can’t use your own auto-loader anymore. Your classes must all be namespaced, with the directory structure following the namespace structure. If you had a class like MicrosoftNLB\NLBNode you would use something like this:

your-working-directory
 |-src
 | \-MicrosoftNLB
 |   \-NLBNode.php
 |-examples
 \-test

Remember your source must be namesapced so NLBNode.php will start out like this:

<?php
namespace MicrosoftNLB;
class NLBNode extends MicrosoftNLB_Node
{

composer.json

So now you have your source laid out in a way which will appease composer. You need to create a composer.json file. Here is one:

{
    "name": "robertlabrie/microsoftnlb",
    "description": "Classes and tools for Microsoft NLB",
    "license": "GPL V2",
    "authors": [
        {
            "name": "Robert Labrie",
            "email": "robert.labrie@gmail.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {
        "react/react": "0.3.*@dev"
    },
    "autoload": {
        "psr-0": {
            "MicrosoftNLB": "src/"
        },
        "classmap" :[ "src/MicrosoftNLB" ]
    }
}

Name, description, authors, all pretty straight forward. The minimum-stability setting is for your dependencies (see below).

The autoloader

This one tripped me up a bit. You need to use the composer auto-loader so that everyone’s packages can co-exist. Setup comes in the form of an array of namespace=path where path is the path where the namespace directory can be found. Junior’s directory layout which I shamelessly copied is what really made this make sense. So in the example above, the autoloader will search in src/CoolProject for classes. Remember your classes have to be namespaced or this thing won’t work.

Classmap

Or will it? The classmap section lets you bolt directories into PSR-0. Basically anything in here will be included for classes. The other case where you need to use a classmap is if classes have underscores in them. PSR-0 will replace underscores with slashes. Since my project uses boiler plate WMI classes, I had class names like MicrosoftNLB_Node, which was being turned into MicrosoftNLB/Node, which was then breaking. Thanks to Juan Treminio in #composer for that advise.

Required packages

This is also pretty straight forward and well explained at getcomposer.org.

Examples and testing

Your testing will probably use PHPUnit or similar. Examples can go in /examples, and will look something like this:

<?php
require_once __DIR__."../vendor/autoload.php";
$node = new MicrosoftNLB\NLBNode('localhost');

Including the autoloader from vendor directory lets composer do all the magic.

Advertisements

About robertlabrie
DevOps Engineer at The Network Inc in metro Atlanta. Too many interests to list here, check out my posts, or look me up on LinkedIn

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: