Working with symlinked packages in PHP

Written by Bram.us - - Aggregated on Friday October 4, 2019
Tags: original-content, composer, symlink

When developing a PHP library/package, you most likely also have a project on disk that consumes said library. Take a WordPress plugin for example: to test it, you need a WordPress installation — both are linked but separate projects. To speed up development you can tell Composer to use the local version of the package, instead of having to copy files from folder to folder as you develop.

~

I’m assuming a folder structure like this:

bramus in ~/repos
$ tree -L 1
.
├── my-library
└── my-project

If my-project were to use the published version of my-library, you would run the following command:

bramus in ~/repos/my-project
$ composer require author/my-library

The ~/repos/my-project/composer.json would then look as follows:

{
    // …
    "require": {
        "author/my-library": "*"
    }
}

~

During development you don’t want to be editing the copy of my-library which is contained inside the my-project repo. What you want is my-project to use the local version of ~/repos/my-library, so that you can directly edit those.

To solve this, Composer allows you to configure the package sources using the repositories option of its configuration. To refer to the local copy adjust ~/repos/my-project/composer.json so that it has an entry pointing to ~/repos/my-library/:

{
    // …
    "require": {
        "author/my-library": "*"
    },
    "repositories": [
        {
            "type": "path",
            "url": "../my-library"
        }
    ]
}

With this addition, re-run the composer require command, and it’ll symlink ~/repos/my-library/ into ~/repos/my-project/vendor/author/my-library/:

bramus in ~/repos/my-project
$ composer require author/my-library

- Installing author/my-library (dev)
Symlinked from ../my-library

When you now edit code inside ~/repos/my-library/, the ~/repos/my-project will also be up-to-date

You can also use repositories to refer to privately published repositories. No need to fiddle with Satis or the like

Did this help you out? Like what you see?
Consider donating.

I don’t run ads on my blog nor do I do this for profit. A donation however would always put a smile on my face though. Thanks!

Buy me a Coffee ($3)


« Forms That Submit PUT Requests - Laracasts

Laracasts - Explain How to Group Records By … »