使用 PHP Composer 开发本地包,然后在部署时复制它

2022-01-21 00:00:00 php composer-php

我有一个本地 PHP 项目(一个网站),我想使用我制作的另一个项目(一个小型​​ Web 框架)作为网站项目的 Composer 包.我正在同时开发它们,但希望将它们作为单独的项目保留;当我部署网站时,我想使用 Composer 将框架项目安装为一个包,就像我使用 Guzzle 或任何其他常见的 Composer 包一样.

I've got a local PHP project (a website), and I want to use another project I've made (a tiny web framework) as a Composer package for the website project. I'm developing them in tandem, but want to keep them as separate projects; when I deploy the website, I want to use Composer to install the framework project as a package, just like I would with Guzzle or any other common Composer package.

网站项目有如下composer.json:

{
    "repositories": [
        {
            "type": "path",
            "url": "/local/path/to/framework/package"
            "options": {
                "symlink": true
            }
        }
    ],
    "require": {
        "my/framework": "dev-master",
        "guzzlehttp/guzzle": "6.3.3"
    }
}

/local/path/to/framework/package/composer.json 我有:

{
    "name": "my/framework",
    "description": "A tiny framework.",
    "autoload": {
        "psr-4": {
            "MyFramework\": "src"
        }
    }
}

我运行 php composer.phar update 并在我的网站项目中设置到框架包的符号链接.如果我更新框架中的代码,它可以立即用于网站代码.这很棒.

I run php composer.phar update and it sets up a symlink to the framework package in my website project. If I update code in the framework, it's immediately available to the website code. This is great.

但是,稍后我想将网站项目部署到我的 Web 服务器.对于干净的部署,我有一个部署脚本,它导出网站 git repo 的相应分支的存档副本,然后运行 ​​php composer.phar install 以确保安装了所有正确的包.使用上面的配置,部署脚本会创建一个到框架包的符号链接,当部署工件被压缩并 scp'd 到 Web 服务器时,这显然是不好的.

However, later on I want to deploy the website project to my web server. For a clean deployment, I have a deploy script that exports an archive copy of the appropriate branch of the website git repo, and then runs php composer.phar install to make sure all the proper packages are installed. With the above config, the deploy script creates a symlink to the framework package, which is obviously no good when the deployment artifact is zipped up and scp'd out to the web server.

如果我删除上面的 symlink 选项,那么每次我在框架包中进行任何更改时都必须运行 php composer.phar update,这很烦人我正在同时开发这些软件包.但是在部署时,包被复制进来,而不是被符号链接进来;这对部署很有用,除了它总是在 master 分支中复制(因为我在 composer.json 中指定了 dev-master).

If I remove the symlink option above, then I have to run php composer.phar update every time I make any changes in the framework package, which is irritating since I'm developing the packages in tandem. But then on deploy, the package is copied in, rather than being symlinked in; this is good for deploy, except it always copies in the master branch (because I specified dev-master in composer.json).

有没有办法设置 Composer 以便我可以方便地使用本地符号链接包进行开发,但要让部署脚本获得框架包的干净副本,而无需更改 composer.json 每次我想部署?

Is there a way to set up Composer so that I can have the convenience of a local symlinked package for development, but to let the deployment script get a clean copy of the framework package, without having to change composer.json every time I want to deploy?

我也尝试将存储库类型更改为 vcs 而不是 path,但是 Composer 克隆 框架包存储库,我真的不在开发或部署期间,不需要在我的本地 vendor/ 树中克隆 repo.我只是希望能够在开发过程中拥有一个符号链接树(它可以让我即时更新),并安装一个干净的包用于 deplotment.

I also tried changing the repository type to vcs instead of path, but then Composer clones the framework package repository, and I really don't need a clone of the repo in my local vendor/ tree, either during development or deployment. I just want to be able to have a symlinked tree (which gets me instant updates) during development, and have a clean package installed for deplotment.

我可能不得不辞职正确地"开发框架项目,也就是说,作为一个 Composer 包,我用实际的发布版本号标记,然后更新我的网站项目以使用特定版本的时间进行部署.不过,我还没有找到最简单的方法.

I may have to resign myself to developing the framework project "properly," that is, as a Composer package that I tag with actual release version numbers, and then update my website project to use a specific version of when it comes time for a deployment. I still haven't figured out the easiest way to do that, though.

推荐答案

你可以使用 Composer Studio插件.

它使用单独的文件进行配置,并允许您在开发中使用符号链接并为产品部署安装常规 Composer,而无需接触 composer.json.

It uses a separate file for configuration and allows you to have symlinks in development and a regular Composer install for prod deploy, without touching composer.json.

有了它,您的网站 composer.json 将看起来像一个常规文件:

With it, your website composer.json will look like a regular file:

{
    "require": {
        "my/framework": "dev-master",
        "guzzlehttp/guzzle": "6.3.3"
    }
}

并在项目的根目录下加上studio.json,内容如下:

And with studio.json in the root of the project, with the following content:

{
    "version": 2,
    "paths": [
        "/local/path/to/framework/package"
    ]
}

你可以告诉插件在哪里找到my/framework.然后 Studio 将从提供的路径对包进行符号链接.

you can tell the plugin where to locate my/framework. Studio will then symlink the packages from the provided paths.

要进行常规安装,只需删除或重命名 studio.json,删除 vendor 文件夹和 composer.lock 文件并运行部署时重新安装.

To have a regular install, just remove or rename studio.json, remove the vendor folder and composer.lock file and run the install again when deploying.

相关文章