【问题标题】:How do I correctly declare a Dependency to another Bundle within a Bundle?如何正确声明对 Bundle 中另一个 Bundle 的依赖关系?
【发布时间】:2015-05-10 07:00:19
【问题描述】:

我正在尝试在 Bundle 中使用 Bundle,但不知何故它失败了。

"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/myname/mybundle"
    }
],
"require": {
    "php": ">=5.3.3",
    "symfony/symfony": "2.1.*",
    (...)
    "myname/mybundle": "*"
},

到目前为止,这似乎有效。但我不知道如何在“myname/mybundle”中声明另一个依赖项。

我在 myname/mybundle 的 composer.json 文件中尝试了以下操作,但没有一个起作用:(

"repositories": [
    {
        "type": "vcs",
        "url": "url": "https://github.com/drymek/PheanstalkBundle"
    }
],
"require": {
    (...)
    "drymek/PheanstalkBundle": "dev-master"
}

"repositories": [
    {
        "type": "package",
        "package": {
            "name": "drymek/PheanstalkBundle",
            "version": "dev-master",
            "source": {
                "url": "https://github.com/drymek/PheanstalkBundle.git",
                "type": "git",
                "reference": "master"
            }
        }
    }
],
"require": {
    (...)
    "drymek/PheanstalkBundle": "dev-master"
}

当我朗姆酒composer.phar update 我得到的只是

- myname/mybundle dev-master requires drymek/pheanstalkbundle dev-master -> no matching package found.

【问题讨论】:

标签: symfony symfony-2.1 composer-php


【解决方案1】:

好的,我找到了答案here

上面写着:Repositories are not resolved recursively. You can only add them to your main composer.json. Repository declarations of dependencies' composer.jsons are ignored

这太糟糕了......但现在至少我知道将我的依赖项放在哪里(在根 composer.json 文件中)

【讨论】:

  • 这意味着 composer 并没有那个比旧的 deps 文件好很多。我想知道递归解决它是一个坏主意,还是只是他们还没有实现。
  • 至少你可以考虑你的 bundle 的 composer.json 文件。在类似的情况下,我面临着一个非常奇怪的问题。我的项目需要检索一些自定义 zip 包,在这些包中,composer.json 文件定义了其他要求。这些需求的存储库在根 composer.json 文件中声明。问题是,在下载 zip 文件后,解压缩并放置在供应商目录中,composer 完全忽略了它的 composer.json 定义包的要求......知道吗?
  • 嘿 giuliano,对不起,但我不知道答案......但我看到你在你的线程上得到了答案。 ;) stackoverflow.com/questions/15023126/…
  • 现在 composer 确实会在 bundles 的 composer.json 文件中检查所需的依赖项。
  • @BigJ:根据 Composer 常见问题解答(如答案中的链接),VCS 存储库中的依赖项并非如此。
【解决方案2】:

对于捆绑依赖,请参阅我的库https://github.com/AshleyDawson/MultiBundle。例如,扩展 MultiBundle 并实现 getBundles() 方法,如下所示:

<?php

namespace Acme\MyBundle;

use AshleyDawson\MultiBundle\AbstractMultiBundle;

class AcmeMyBundle extends AbstractMultiBundle
{
     /**
      * Optional: define a protected constructor to stop instantiation     outside of registerInto()
      */
     protected function __construct()
     {

     }

    /**
     * Define bundles that this bundle depends on
     */
    protected static function getBundles()
    {
        return array(
           new Acme\FooBundle\AcmeFooBundle(),
           new Acme\BarBundle\AcmeBarBundle(),
        );
    }
}

然后在 AppKernel 中注册 bundle 和它的依赖:

// app/AppKernel.php

// ...

class AppKernel extends Kernel
{
    // ...

    public function registerBundles()
    {
        $bundles = array(
            // ...,
        );

        // Register my bundle and its dependencies
        \Acme\MyBundle\AcmeMyBundle::registerInto($bundles);

        // ...
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 2014-09-14
    • 2019-09-10
    • 2019-03-28
    • 1970-01-01
    • 2013-02-01
    • 1970-01-01
    相关资源
    最近更新 更多