Using a repository that doesnt have composer.json

Joe • December 19, 2013

composer github laravel php version control

Have you ever run across a repository that you'd love to throw into your project via composer and the repository author didn't include composer.json? I ran into that exact issue with a project and had to figure out what the best way to handle the situation was. There are a few different ways you could include the repo. I'll show you how I did it without having to copy the repository directly into my project.

The use case

I have a Laravel project that I wanted to add firechat to. In order to authenticate your users to your Firebase backend, you need to create tokens for them. Using PHP, there are two repositories you need to accomplish this. First you need PHP-JWT For encoding the JSON Web Tokens (JWT). Second, you need Firebase Token Generator for PHP

The problem

If you wandered off and looked at those two repositories you may have already noticed that JWT has composer instructions, but Firebase Token Generator for PHP does not. What now? Do you just clone that repository into your folder and add it to your project's repository? Well, you could; thankfully you Don't have to!

How I solved the problem (with some help!)

I want to thank Luke Wilkins for steering me in the right direction. He immediately knew I could accomplish this with composer. He was right.

First Step: Fork the repository

I forked the firebase-token-generator-php into svpernova09/firebase-token-generator-php

Add composer.json

{
        "name": "firebase/firebase-token-generator-php",
        "description": "Firebase Token Creation",
        "license": "MIT",
        "require": {
            "php": ">=5.3.0",
            "firebase/php-jwt": "dev-master"
        }
    }

You can see I have added the PHP-JWT repository as a dependency to our new package so it will automatically grab that for you as well. Commit and push this file to your fork.

Loading a package from a VCS repository with Composer

Luke sent me this link which guided me to what I needed to do.

Add this to your project's composer.json:

"repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/svpernova09/firebase-token-generator-php"
        }
    ],

Then simply add the dependency:

"require": {
        "firebase/firebase-token-generator-php": "dev-master"
    },

Run composer update to update your project and you should have these folders in your project:

vendor/firebase/firebase-token-generator-php
    vendor/firebase/php-jwt

Success!

We can now access the Services_FirebaseTokenGenerator() class in FirebaseToken.php from within our application:

$tokenGen = new Services_FirebaseTokenGenerator($secret);

If for some reason you can't, your package many not support PSR-0 Autoloading. You can manually add the file to be autoloaded by adding this to composer.json in the ‘autoload' section:

"files": ["vendor/firebase/firebase-token-generator-php/FirebaseToken.php"],

If you still can't access the class, check out the autoload section of the composer documentation.

If you have any questions you can find me @Svpernova09