Developing composer packages locally
Assume you are creating a new php package that will be shipped via composer/packagist. During development you will use your unit tests for validating the features.
But at some point in time you need to include your package in a “real” application to ensure everything is working as expected. To do so you will push your dev branch to github and reference a new repository in your application’s composer.json
.
This approach has one big disadvantage: You need to set the minimum-stability
of your project to dev
and this change will pull in the development versions of all referenced packages.
In this article I will explain another approach that allows you to keep the stability at stable and also brings you the ability to use a local package in your application and test its functionality.
Starting the package
In your composer.json
file you need to specify the package name and the PSR-4 autoload definition.
{
"name": "fetzi/dev-package",
"description": "package under development",
"autoload": {
"psr-4": {
"Fetzi\\Package\\": "src"
}
},
}
Now you implement your package logic inside the src
folder.
Configuring the application
Inside the application you need to add a new repository definition. For general information how you declare repositories please refer to the composer documentation.
{
"name": "fetzi/application",
...
"repositories": {
"dev-package": {
"type": "path",
"url": "relative/or/absolute/path/to/my/dev-package",
"options": {
"symlink": true
}
}
}
}
By setting the "type": "path"
composer knows that you would like to reference a local repository and the url
defines the package location (the path can be relative or absolute).
With this two settings you are good to go, but composer will copy the package code into the vendor folder and you need to call composer update
on every package change.
To prevent composer from doing so you need to specify the symlink
option and composer will create a symlink to the package folder.
The last step is to issue a composer require
command to include the package in your application.
composer require fetzi/dev-package @dev
After executing this command the package folder is symlinked into the application’s vendor folder and each change to your package will be immediately available in your application.