3

I have multiple development environments, one of which locally contains the source code for my packages, the other one doesn't. I'd like to keep my environments configured separately, but I'd also like composer to know that it should use my local package when present, and use the packagist version as a fallback.

I tried looking for this sort of thing online, but I couldn't find a direct answer. I already know how to require a local package, but I don't know how to optionally require a local package.

Just to reiterate, I want to require my package like so:

"require": {
    "php": "^7.1.3",
    /* ... */
    "my-namespace/my-package": "dev-master",
},

And be able to list this package somehow as sometimes being locally available. If I do this:

"repositories": [
    {
        "type": "path",
        "url": "./path/to/my/package"
    },
]

I can make it locally required, but this means in my other development environment (where the package does not locally exist, nor do I want it to), composer will fail to require my package.

Once this is said and done, I'd like my local environment (the one with my packages locally hosted) to require the local version of the package, and I'd like my other environment (the one without my packages locally hosted) to require the packagist version of the package.

Any ideas?

Tyler Reed
  • 442
  • 3
  • 15
  • Possible duplicate of [How to deploy correctly when using Composer's develop / production switch?](https://stackoverflow.com/questions/21721495/how-to-deploy-correctly-when-using-composers-develop-production-switch). – EternalHour Sep 17 '19 at 23:36
  • 1
    @EternalHour This question is not a duplicate of your referenced one. Your referenced question deals with the "--dev" and "--no-dev" flags for only installing certain packages on certain environments. For this issue, I want the package installed on BOTH environments, but I want the source of the package to be different, depending upon whether or not a local copy of the package exists. – Tyler Reed Sep 22 '19 at 15:06

1 Answers1

-1

I think you don't want to "optionally require", because the package is essential. You want to always include it, but only download it if unavailable locally.

There is this article describing the procedure: https://johannespichler.com/developing-composer-packages-locally/

In short: You add a "repositories" entry in your application that points to your local library repository:

"repositories": {
  "dev-package": {
    "type": "path",
    "url": "relative/or/absolute/path/to/my/dev-package",
    "options": {
      "symlink": true
    }
  }
}

The "url" points to the path of your local lib repo. The options.symlink is important, otherwise Composer would copy the repo instead of symlinking it (forcing you to constantly update the copy instead of automagically getting all changes).

On other machines, the path points to nowhere, Composer will not detect the lib there, and search for other sources, probably downloading it from the repo server.

Sven
  • 69,403
  • 10
  • 107
  • 109
  • 1
    I understand this, but I technically have to list my package twice, as the "production" version is hosted using a specific VCS. When I list both the VCS and path versions, composer always grabs the one from the repository listed first, and remembers what it used for install (which fails on environments that don't have the local repo). – Tyler Reed Nov 13 '19 at 20:15