Space Cat, Prince Among Thieves

Load a Github Gist with Composer

Composer is amazing for pulling in packages, but what if you find a Gist that isn't Composer aware? Fear not, as Composer has the magical ability to pull in repositories not explicitly set up in Packagist by defining them in the "repositories" section.

But what if you wanted to load a single class from a git gist?

You can add the following repositories section to your composer.json file, adjusting name and url as necessary. The name should be a vendor/package (all lower) style name you make up for it, and url will be from the Clone this gist box on GitHub.

"repositories": [
    {
        "type":"package",
        "package": {
            "name": "turin86/wssoapclient",
            "version": "master",
            "source": {
                "url": "https://gist.github.com/5569152.git",
                "type": "git",
                "reference":"master"
            },
            "autoload": {
                "classmap": ["."]
            }
        }
    }
]

Then in your require section, add your selected name as follows.

"require": {
    "turin86/wssoapclient": "dev-master"
}

If the name of the class doesn't match the filename in a friendly autoload-able manner, you'll have to replace "classmap": ["."] with "files": ["ClassToAutoload.php"] replacing ClassToAutoload.php with the filename. More info on autoloading can be found here. Note that if you are experimenting with autoload settings you may need to remove your vendor directory and composer install, as updates to the autoload were not reflected for me after a composer update.

After this, just run composer update and voilà, you are loading a gist with Composer.


Comment by: Fendi Septiawan on

Fendi Septiawan's Gravatar Thanks, it works like charm... Great tutorial.

Email address will never be publicly visible.

Basic HTML allowed.