如何在 Heroku 上使用 MongoDB ext 部署 PHP7?

How to deploy PHP7 with MongoDB ext on Heroku?

我正在尝试在 Heroku 上部署应用程序,但我 运行 遇到了与作曲家依赖项冲突相关的问题。它在我的本地开发机器上工作正常,我可以 composer update 没有任何问题。

这是我的示例 composer.json:

"require": {
    "php": "7.1.*",
    "ext-mongo": "*",
    "ext-mongodb": "^1.2",
    "symfony/symfony": "3.2.*",
    "doctrine/orm": "^2.5",
    "doctrine/doctrine-bundle": "^1.6",
    "doctrine/doctrine-cache-bundle": "^1.2",
    "symfony/swiftmailer-bundle": "^2.3.10",
    "symfony/monolog-bundle": "^3.0.2",
    "symfony/polyfill-apcu": "^1.0",
    "sensio/distribution-bundle": "^5.0",
    "sensio/framework-extra-bundle": "^3.0.2",
    "incenteev/composer-parameter-handler": "^2.0",
    "twig/twig": "^1.0||^2.0",
    "mongodb/mongodb": "^1.1",
    "doctrine/mongodb-odm-bundle": "^3.2",
    "alcaeus/mongo-php-adapter": "^1.0",
    "friendsofsymfony/user-bundle": "2.0.0-beta2",
    "friendsofsymfony/oauth-server-bundle": "^1.5",
    "symfony/assetic-bundle": "^2.8",
    "twig/extensions": "^1.4",
    "jms/serializer-bundle": "^1.2",
    "friendsofsymfony/rest-bundle": "^2.1"
},

...这是 Heroku 的错误输出:

> Loading repositories with available runtimes and extensions
> Updating dependencies
> Your requirements could not be resolved to an installable set of packages.
>
>   Problem 1
>     - Installation request for doctrine/annotations v1.4.0 -> satisfiable by doctrine/annotations[v1.4.0].
>     - Installation request for twig/twig v2.3.2 -> satisfiable by twig/twig[v2.3.2].
>     - Conclusion: don't install php 7.1.3
>     - Installation request for doctrine/mongodb 1.4.0 -> satisfiable by doctrine/mongodb[1.4.0].
>     - doctrine/annotations v1.4.0 requires php ^5.6 || ^7.0 -> satisfiable by php[5.6.30, 7.0.15, 7.0.16, 7.0.17, 7.1.1, 7.1.2, 7.1.3].
>     - don't install php 7.1.1|don't install php 5.5.38
>     - don't install php 7.1.2|don't install php 5.5.38
>     - don't install php 7.0.15|don't install php 5.5.38
>     - don't install php 7.0.16|don't install php 5.5.38
>     - don't install php 7.0.17|don't install php 5.5.38
>         - ext-mongo 1.6.14 requires php 5.5.* -> satisfiable by php[5.5.38].
>     - doctrine/mongodb 1.4.0 requires ext-mongo ^1.5 -> satisfiable by ext-mongo[1.6.14].
>     - ext-mongo 1.6.14 requires php 5.6.* -> satisfiable by php[5.6.30].
>     - Conclusion: don't install php 5.6.30

这是一个更大的问题。让我们把它分成两部分:

第 1 部分:

Ext-mongo 是 legacy driver that does not exist for PHP7.x. But Doctrine-ODM requires ext-mongo. The workaround is to use an adapter that provides an interface for the old driver that uses the new MongoDB driver. There is an explanation in the Doctrine-ODM documentation,但此解释不是最新的,因此 incomplete/incorrect。

在您的情况下,这意味着您从作曲家文件的 "require" 部分删除 "ext-mongo""ext-mongodb" 并保留 "alcaeus/mongo-php-adapter"(适配器将需要本身 "ext-mongodb" - 新驱动程序。)不幸的是,这仍然行不通,我们将进入第 2 部分。

第 2 部分:

集成目前由 Packagist 提供的 alcaeus/mongo-php-adapter 将产生作曲家依赖冲突。有一些关于 exactly this problem and also if this might be a general composer issue (see here). Also, several solutions are suggested. One solution 对我有用的讨论(我也在 Heroku 上部署):

alcaeus/mongo-php-adapter的composer.json中替换:

"provide": {
    "ext-mongo": "1.6.14"
},

作者:

"replace": {
    "ext-mongo": "1.6.14"
},

在 GitHub 上,使用 "replace" 版本的 master branch of alcaeus/mongo-php-adapter uses the "provide"-version, but there is also a composer-replace 分支。不幸的是,这个分支没有保持最新。

您可以使用那个 "replace" 分支作为 alcaeus/mongo-php-adapter 在您的 composer 文件中的存储库:

"repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/alcaeus/mongo-php-adapter"
        }
    ],
"require": {
    "alcaeus/mongo-php-adapter": "dev-composer-replace",
    ...

或者,您可以分叉 master 分支,将 "provide" 替换为 "replace" 并将该分叉用作存储库。