Adding a random GitHub repository to your project using Composer

I’m becoming a big fan of Composer. I recently had to re-create the environment for an older project on a different development machine, a process which unfortunately included quite a few manual steps to get the dependencies configured.

I decided to set up the project with Composer, and now setting up the project is fast and easy (well at least easy … waiting for the Zend Framework files to download does take a little while).

It’s super easy to get started and follow the Composer documentation to use dependencies that are available through Packagist. More and more packages, like both Zend Framework 1 and Zend Framework 2, are becoming available there.

But what about packages that aren’t available through Packagist? If you control the package, you can publish it there, or if it’s something for private/corporate use, you can set it up as a Composer package using Satis.

But what if you don’t have control over the package? What if it’s just a random GitHub repository?

Because Composer can work with any source code repository, it’s actually not difficult to make it work with a third-party repository on GitHub (or Bitbucket, whatever … as long as it uses Git, Mercurial or Subversion).

For example, for my Zend Framework 1 projects, I like to use a dependency injection container called Yadif. With some help, I was able to get it set up in my project like so:

"repositories": {
    "yadif": {
        "type": "package",
        "package": {
            "name": "beberlei/yadif",
            "version": "1.0.0",
            "source": {
                "url": "https://github.com/beberlei/yadif",
                "type": "git",
                "reference": "d874042a50"
            }
        }
    }
},
"require": {
    "beberlei/yadif": "1.0.0"
},
"autoload": {
    "psr-0": {
        "Yadif": "vendor/beberlei/yadif/src/"
    }
}

Because the Yadif repository doesn’t have any tags for version numbers, we have to tell Composer very explicitly about it. Under “repositories”, we give the package its name and specify a version number (I used 1.0.0 but you could plug in whatever makes sense). We then tie that version to a specific commit, in this case d8740042a50.

After basically defining the repository for Composer, we then tell Composer to require the repository. Then — and I love this part — we tell Composer how to autoload the classes from the package. Because Yadif basically follows the PSR-0 convention, we just tell Composer where the core class files are found, and autoloading is all set up — no need to mess with symlinks or include paths.

Note: Check this post for more explanation and sample composer.json code.

Comments

  1. Hey Eugene, thanks for linking to me. Just wanted to let you know that I migrated my old Drupal blog to Jekyll, so the link has changed. The post you linked to is now located at http://texaswebdeveloper.net/2012/08/21/create-a-local-composer-repo-with-satis/

Leave a Reply to Eugene Morgan Cancel reply

*