Symfony2 - 创建自己的供应商包 - 项目和git策略

时间:2014-02-03 09:19:54

标签: php git symfony bundle

我们正在考虑为实体映射和服务创建我们自己的common捆绑包,以便在几个单独的应用中使用。捆绑包应该易于修改,运行,包含和测试。我知道Best Practices for Structuring Bundles,但我不知道在开发时使用什么git策略。

我们应该创建common bundle作为一个整个项目并将整个存储库提交给我们的git服务器,还是最好只为common bundle的root启动源代码控制并仅推送其内容?我在github上看到了捆绑包中的这种方法,但我不知道以这种方式开发捆绑包的简单方便。

2 个答案:

答案 0 :(得分:172)

创建一个新的空symfony项目

php composer.phar create-project symfony/framework-standard-edition demo/ 2.4.1
cd demo

生成新捆绑

(例如src/Company/DemoBundle

php app/console generate:bundle
cd src/Company/DemoBundle/

src/Company/DemoBundle

中初始化您的github存储库
git init
touch README.md
git add .
git commit -m "initial commit"
git remote add origin https://github.com/YourAccount/DemoBundle.git
git push -u origin master

添加composer.json文件

src/Company/DemoBundle/composer.json

{
    "name" : "company/demobundle",
    "description" : "A demo bundle",
    "type" : "symfony-bundle",
    "authors" : [{
        "name" : "demo",
        "email" : "demo@company.com"
    }],
    "keywords" : [
        "demo bundle"
    ],
    "license" : [
        "MIT"
    ],
    "require" : {
    },
    "autoload" : {
        "psr-0" : {
            "Company\\DemoBundle" : ""
        }
    },
    "target-dir" : "Company/DemoBundle",
    "repositories" : [{
    }],
    "extra" : {
    "branch-alias" : {
            "dev-master" : "some_version-dev"
        }
    }
}

现在您拥有捆绑包的基本结构

在另一个项目中使用它

composer.json:

    [...]
    "require" : {
        [...]
        "company/demobundle" : "dev-master"
    },
    "repositories" : [{
        "type" : "vcs",
        "url" : "https://github.com/Company/DemoBundle.git"
    }],
    [...]

执行:

curl -sS https://getcomposer.org/installer | php
php composer.phar update company/demobundle

应用程序/ AppKernel:

new Company\DemoBundle\CompanyDemoBundle(),

开展工作

  • 您可以在src/Company文件夹中克隆DemoBundle,然后手动安装
  • 您可以使用符号链接

结论

您可以在第一个项目中开发和测试您的软件包,并在第二个项目中将它与github和composer一起使用。

答案 1 :(得分:3)

在Symfony4中,generate:bundle命令不再可用。相反,您可以关注this tutorial

首先创建一个项目:

composer create-project symfony/website-skeleton my-project

然后,创建一个my-project/lib/AcmeFooBundle/src目录。这将是你的捆绑。此目录的命名空间为Acme\AcmeFooBundle,因此如果您在lib/AcmeFooBundle/src/Service/Foo.php创建服务类,其名称空间将为Acme\AcmeFooBundle\Service

现在我们需要告诉编辑器自动加载器在该新目录中查找新类,因此我们需要编辑composer.json autoload部分:

"autoload": {
    "psr-4": {
        "Acme\\AcmeFooBundle\\": "lib/AcmeFooBundle/src/",
    }
}, 

并运行composer dump-autoload

现在您只需将捆绑类添加到config/bundles.php

return [
    ...
    Acme\AcmeFooBundle\AcmeFooBundle::class => ['all' => true],
];

和依赖注入从您的包中加载配置。

如果要在添加依赖项注入之前检查服务,可以在config/services.yml处自动加载它们:

services:
    ...
    Acme\AcmeFooBundle\Services\Foo: ~

这就是全部。关注best practices并继续编码。

PS:我发布了a few tips for developing Symfony reusable bundles的帖子。

相关问题