fetzi.dev

Add composer-link command for local development

2 minutes

I’ve written a blog post a year ago about developing a composer package locally by using it in another project.

To do so you need to setup a local repository configuration in your composer.json file and then require the dependency with composer require.

Caleb Porzio has written an article about his bash function composer-link and I enhanced this command to directly require the package with composer-link.

The command

The command is responsible for adding the local repository configuration and also to require the dependency.

composer-link() {
  repositoryName=${3:-local}

  composer config repositories.$repositoryName '{"type": "path", "url": "'$1'", "options": {"symlink": true}}' --file composer.json
  composer require $2 @dev
}

To use the function you need to specify two parameters, the relative path to the package and the package name:

composer-link ../my-package fetzi/my-package

An optional third parameter defines the name of the repository entry in your composer.json file. The value defaults to local.

To start using this bash function you need to add it to your ~/.bashrc, ~/.bash_profile or your ~/.zshrc.

Multiple repository definitions

If your package already exists in an already defined repository entry, the order of the repository entries in your composer.json file will mather. You will have to add the symlink repo manually at first position to be able to require the local version instead of the version from the other repository.

Resources

This might be also interesting

Do you enjoy reading my blog?

sponsor me at ko-fi