简体   繁体   中英

Is there a way to optionally require a local composer package?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM