首页 文章

在GitHub上托管Symfony2捆绑包并使用Composer进行安装

提问于
浏览
2

背景

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

我尝试了什么

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

{
    "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"
        }
    }
}

这里是捆绑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致命错误:在第20行的... / symfony2_AsyncTweetBundle / app / AppKernel.php中找不到类'AlexisLefebvre \ Bundle \ AlexisLefebvreAsyncTweetsBundle'

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

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

3 回答

  • 1

    以下是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;

    所有的路径和选择都令人困惑 . 我通过让作曲家首先直接从github而不是packagist加载来完成它 . 更容易以这种方式对composer.json进行更改 .

  • 0

    看着你的packagist repository和你的github repository . 看来您的包的存储库点没有文件 . 因此,首先尝试在这两个位置合并您的文件 .

    然后,如果您想为每个文件更改供应商名称/命名空间更改命名空间以保持一致性,并且您已经知道如何创建自己的包 .

    如果混乱,你仍然可以看看Creating your very own Composer Package .

  • 1

    解决方案是在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\\" : ""
            }
        }
    }
    

    这是基础包类:

    <?php
    
    namespace AlexisLefebvre\Bundle\AsyncTweetsBundle;
    
    use Symfony\Component\HttpKernel\Bundle\Bundle;
    
    class AsyncTweetsBundle extends Bundle
    {
    }
    

    我可以通过将它添加到app / AppKernel.php来加载Symfony2中的包:

    new AlexisLefebvre\Bundle\AsyncTweetsBundle\AsyncTweetsBundle(),
    

相关问题