如何在Symfony 2.1中使用Composer安装自己的软件包?

7

我刚刚转向使用Symfony 2.1,但是我不知道如何使用Composer安装自己的Bundle?

在2.0.x版本中,使用deps非常简单:

[MyOwnBundle]
  git=git@git.weboshin.ru:weboshin_cms_bundle.git
  target=/bundles/My/OwnBundle

之后我只需要触发 bin/vendors update,就完成了!

但现在没有了 deps 文件,我应该使用 Composer 完成所有操作。请给我任何提示。


在Symfony 2.1中,您还可以使用旧的依赖项管理器!您需要创建一个composer.json文件来告诉Composer您的依赖关系是什么。您应该查看Symfony标准版的文件。然后为您的bundle创建另一个composer.json文件。 - Florent
@Florent,请注意已经有一个Composer工具的标签,即composer-php - Charles
2个回答

6

我已找到答案。

// my_project/compose.json:
{
    "repositories": [
        {
            "type": "vcs",
            "url": "own_repository_url"
        }
    ],

    // ...

    "require": {
        // ...
        "own/bundle": "dev-master"
    }
},

 

// own_repository/own_bundle/compose.json:
{
    "name": "own/bundle"
}

5

将composer.json文件添加到您的包中。例如,我为我的一个bundle编写了以下内容:

{
    "name":        "cg/kint-bundle",
    "type":        "symfony-bundle",
    "description": "This bundle lets you use the Kint function in your Twig templates. Kint is a print_r() replacement which produces a structured, collapsible and escaped output",
    "keywords":    ["kint", "debug", "symfony", "bundle", "twig"],
    "homepage":    "http://github.com/barelon/CgKintBundle",
    "license":     "MIT",

    "authors": [
        {
            "name": "Carlos Granados",
            "homepage": "http://github.com/barelon"
        },
        {
            "name": "Symfony Community",
            "homepage": "http://github.com/barelon/CgKintBundle"
        }
    ],

    "require": {
        "php":                      ">=5.3.2",
        "symfony/framework-bundle": ">=2.0.0",
        "raveren/kint":             "dev-master"
    },

    "minimum-stability": "dev",

    "autoload": {
        "psr-0": {
            "Cg\\KintBundle": ""
        }
    },

    "target-dir": "Cg/KintBundle"
}

然后将您的 bundle 添加到 packagist.org。这非常简单,您只需要提供您的 git 地址,它将处理其他事情。

一旦您的 bundle 在 packagist 上可用,那么只需将其作为 Symfony 项目的 composer.json 文件中的依赖项添加即可。在我的案例中,我有:

"require": {
    ....
    "cg/kint-bundle": "*"
},

只需要在Symfony目录中运行“composer update”即可,就这么简单!甚至无需更新自动加载文件,Composer会为您完成。剩下的唯一事情就是在appkernel.php中加载bundle。


3
请注意,“添加到Packagist”仅适用于开源包,对于闭源包,请参阅https://getcomposer.org/doc/05-repositories.md#vcs和https://getcomposer.org/doc/articles/handling-private-packages-with-satis.md。 - Seldaek
6
жңүдәәзҹҘйҒ“symfony-bundleж„Ҹе‘ізқҖд»Җд№Ҳеҗ—пјҹдҪҝз”Ёиҝҷз§Қзұ»еһӢдјҡеёҰжқҘд»Җд№ҲеҗҺжһңпјҹ - greg0ire

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接