在 GitHub 上托管 Symfony2 包并使用 Composer 安装它

Host Symfony2 bundle on GitHub and install it with Composer

背景

我创建了一个 Symfony2 application hosted on GitHub。现在我想把它做成一个包而不是一个应用程序,以便与 Composer 一起使用它。

我试过的

我在 GitHub 上创建了一个名为 AsyncTweetsBundle 的新存储库,我认为我的供应商名称应该是 AlexisLefebvre 和包名称 AsyncTweetsBundle。但是我不明白如何配置composer.json文件,这里是文件的当前内容:

{
    "name" : "alexislefebvre/async-tweets-bundle",
    "type" : "symfony-bundle",
    "description" : "PHP Twitter reader for asynchronous reading",
    "keywords" : ["twitter", "reader", "bundle"],
    "homepage": "http://asynctweets.alexislefebvre.com/",
    "license" : "MIT",
    "authors" : [{
        "name" : "Alexis Lefebvre",
        "homepage": "http://alexislefebvre.com/",
        "role": "Developer"
    }],
    "require" : {
        "php": ">=5.3.2",
        "abraham/twitteroauth": "0.5.0"
    },
    "autoload" : {
        "psr-0" : {
            "AlexisLefebvre\Bundle\AsyncTweetsBundle" : ""
        }
    },
    "target-dir": "AlexisLefebvre/Bundle/AsyncTweetsBundle",
    "extra": {
        "branch-alias": {
            "dev-master": "0.1-dev"
        }
    }
}

这里是包的基础 class AlexisLefebvreAsyncTweetsBundle :

<?php

namespace AlexisLefebvre\Bundle\AlexisLefebvreAsyncTweetsBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AlexisLefebvreAsyncTweetsBundle extends Bundle
{
}

我已经在 Packagist 上提交了这个包:alexislefebvre/async-tweets-bundle

所以我创建了一个 Symfony2 安装并添加了这个依赖项:

composer create-project \
symfony/framework-standard-edition symfony2_AsyncTweetBundle --prefer-dist
cd symfony2_AsyncTweetBundle/
composer require alexislefebvre/async-tweets-bundle

捆绑包已安装在 vendor/alexislefebvre/async-tweets-bundle/AlexisLefebvre/Bundle/AsyncTweetsBundle/AlexisLefebvreAsyncTweetsBundle.php 但我无法使用它。

问题

捆绑包已安装在 vendor/alexislefebvre/async-tweets-bundle/AlexisLefebvre/Bundle/AsyncTweetsBundle/AlexisLefebvreAsyncTweetsBundle.php 中。正确吗?

我在 app/AppKernel.php 中添加了 new AlexisLefebvre\Bundle\AlexisLefebvreAsyncTweetsBundle() 但是如果我 运行 一个命令,例如。 php app/console cache:clear --env=dev,它抛出一个错误:

PHP Fatal error: Class 'AlexisLefebvre\Bundle\AlexisLefebvreAsyncTweetsBundle' not found in .../symfony2_AsyncTweetBundle/app/AppKernel.php on line 20

捆绑包的正确名称是什么?名称空间 AlexisLefebvre 是否必须包含在包的 class 名称中?我应该使用 AlexisLefebvre 还是 AlexisLefebvre\Bundle 作为基本命名空间? "target-dir" 的正确值是多少?我没有找到任何关于 composer.json 文件不同部分之间链接的解释:"name""autoload" : {"psr-0" : {}}"target-dir" .

我尝试使用 "autoload" : {"psr-4" : {...}}psr-0 已被新项目弃用),更糟糕的是,捆绑包根本没有安装。

这是 Cerad 命名空间下的 PersonBundle 的工作示例。

{
"name":        "cerad/person-bundle",
"type":        "symfony-bundle",
"description": "Symfony Cerad Person Bundle",
"keywords":    ["cerad","zayso"],
"homepage":    "http://github.com/cerad/PersonBundle",
"license":     "MIT",
"authors": [
    {
        "name": "",
        "email": "",
        "homepage": "http://zayso.org",
        "role": "Developer"
    }
],
"require": {
    "php": ">=5.3.3"
},
"minimum-stability": "dev",
"autoload": {
    "psr-0": { "Cerad\Bundle\PersonBundle\": "" }
},
"target-dir": "Cerad/Bundle/PersonBundle"
}

代码安装在 vendor\cerad\person-bundle\Cerad\Bundle\PersonBundle

我有一个 CeradPersonBundle.php 文件 命名空间为 Cerad\Bundle\PersonBundle;

所有路径和选项都令人困惑。我通过让 composer 首先直接从 github 而不是 packagist 加载来完成它。这样更容易对 composer.json 进行更改。

看着你的packagist repository and your github repository。您的包的存储库点似乎没有文件。所以,首先尝试在这两个位置合并您的文件。

然后,如果您想更改您的供应商 name/namespace 更改每个文件的命名空间以保持统一,并且您已经知道如何创建自己的程序包。

还可以看看Creating your very own Composer Package 以防混淆。

解决方案是在 composer.json 中使用 psr-4:

{
    "name" : "alexislefebvre/async-tweets-bundle",
    "type" : "symfony-bundle",
    "description" : "PHP Twitter reader for asynchronous reading",
    "keywords" : ["twitter", "reader", "bundle"],
    [...]
    "autoload" : {
        "psr-4" : {
            "AlexisLefebvre\Bundle\AsyncTweetsBundle\" : ""
        }
    }
}

这是基本包 class:

<?php

namespace AlexisLefebvre\Bundle\AsyncTweetsBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AsyncTweetsBundle extends Bundle
{
}

通过将此添加到 app/AppKernel,我能够在 Symfony2 中加载捆绑包。php:

new AlexisLefebvre\Bundle\AsyncTweetsBundle\AsyncTweetsBundle(),