How to load composer package from remote git when running in production, and load from local package when development environment?

Viewed 424

I have two composer files to run composer in each environment

"repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/igorhaf/laravel-admin"
        }
    ],
    "require": {
        "php": "^7.2.5|^8.0",
        "fideloper/proxy": "^4.4",
        "guzzlehttp/guzzle": "^7.4",
        "laravel/framework": "^7.0",
        "laravel/telescope": "^3.0",
        "laravel/tinker": "^2.5",
        "encore/laravel-admin": "dev-master"
    },

I want running laravel-admin from a local folder in my project (packages) when I am using local environment, but with the same composer.json file

1 Answers

If I understand your use case clearly, you develop custom Laravel package and you don't want to always have to push it before you test it in local.

There's absolutely a use case for it. I personally use it. With this approach, your composer.json can stay the same both on local and server. No need to change anything.

  1. You would need the package https://github.com/franzliedke/studio . The documentation is quite explanatory. You install it globally via composer. composer global require franzl/studio though can be installed per project but that's quite tedious because you may only need it locally.

  2. At the root of your project run studio load path/to/your/local/vendor/directory/* (if you want all packages in directory) or studio load path/to/your/local/package (for a single package. You may add multiple packages). This will generate a studio.json file at the root.

Eg.

{
    "version": 2,
    "paths": [
        "path/to/local/vendor/directory/*",
        "path/to/local/package"
    ]
}
  1. Anytime you run composer update, composer will first look in your studio.json file and symlink to local packages if available, else get it from the remote repository.

Note: i. Make sure to add studio.json to .gitignore. ii. Also remove the paths from studio.json as seen below and run composer update to generate new composer.lock file before you push your code to production/server. Else your code on server will be looking for packages from the symlink in local setup when you run composer install and give you errors.

{
    "version": 2,
    "paths": [
        
    ]
}

iii. After pushing to server, return the studio.json to initial state and continue to develop. No need to push to github/remote vcs before you see your changes. Changes are symlinked from local if available or gotten from remote.

iv. Run composer install on server.

v. Don't forget to push your package to remote else, you will not see the changes on server, though you see on local.

Related