Developing Composer Packages Locally

September 8, 2018

When building composer packages, the development workflow may not be immediately apparent. Consuming composer packages in your project can be as easy as adding one line to your composer.json file, but what about a package that is currently under development and not yet available via Packagist?

Luckily, Composer gives you the ability to link the local directory of your in-development package to a project for easy testing.

Development Package

Start by creating a simple composer.json file for your development package.

{
    "name": "michaeljdennis/dev-package",
    "description": "A local package currently in development.",
    "autoload": {
        "psr-4": {
            "Namespace\\": "src/"
        }
    }
}

Test Application

Inside the test application you will need to add two entries to your composer.json file in order for this to work. First, add the usual require statement.

{
    "require": {
        "michaeljdennis/dev-package": "*"
    }
}

Then add a repositories setting that tells composer the absolute or relative path to your development package.

{
    "repositories": [
        {
            "type": "path",
            "url": "../dev-package"
        }
    ]
}

After adding these two settings, run composer update inside your test application and the development package will be symlinked if possible, otherwise is will be copied.

Now you can quickly test out your new package. Check out the documentation for full details on Composer's path based repositories.