To achieve the setup where you use a local repository during development and a GitHub repository in production with a single composer.json file, you can leverage Composer's ability to handle different configurations based on environment variables.
Here's a step-by-step solution:
-
Define Both Repositories in
composer.json:You can define both the local path repository and the GitHub repository in your
composer.json. Use an environment variable to determine which one to use."repositories": [ { "type": "path", "url": "~/Packages/me/arepo-php", "options": { "symlink": true }, "only": ["dev"] }, { "type": "vcs", "url": "https://github.com/me/arepo-php", "only": ["prod"] } ] -
Use Environment Variables:
You can use an environment variable to switch between development and production. For example, you can use the
COMPOSER_ENVvariable. -
Modify Composer Command:
When you run Composer, specify the environment. For development, you can set the environment variable to
dev, and for production, set it toprod.-
For development:
COMPOSER_ENV=dev composer install -
For production:
COMPOSER_ENV=prod composer install
-
-
Conditional Repository Loading:
Unfortunately, Composer does not natively support conditional loading of repositories based on environment variables directly in
composer.json. However, you can use a script to modify thecomposer.jsonbefore runningcomposer install.Here's a simple example of a script that could be used:
#!/bin/bash if [ "$COMPOSER_ENV" == "dev" ]; then # Use local path repository jq '.repositories[0].only = ["dev"] | .repositories[1].only = []' composer.json > composer.json.tmp && mv composer.json.tmp composer.json else # Use GitHub repository jq '.repositories[0].only = [] | .repositories[1].only = ["prod"]' composer.json > composer.json.tmp && mv composer.json.tmp composer.json fi composer installThis script uses
jqto modify thecomposer.jsonfile based on the environment variable.
By following these steps, you can manage your dependencies from different sources based on the environment, using a single composer.json file.