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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21523481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:13:36  来源:igfitidea点击:

Symfony2 - creating own vendor bundle - project and git strategy

phpgitsymfonybundle

提问by ex3v

We're considering creating our own commonbundle for entity mapping and services for use within few separate apps. A bundle should be easy to modify, run, include and test. I know about Best Practices for Structuring Bundles, but I don't know what gitstrategy to use when it comes to development.

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

Should we create commonbundle as a whole project and commit whole repository to our git server, or is it better to start source control only for root of commonbundle and push only its contents? I see this approach in bundles available on github, but I don't know easy and comfortable way to develop bundles that way.

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

回答by VBee

Create a new empty symfony project

创建一个新的空 symfony 项目

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

Generate a new bundle

生成一个新的包

(for example src/Company/DemoBundle)

(例如src/Company/DemoBundle

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

Init your github repository in src/Company/DemoBundle

初始化你的 github 仓库 src/Company/DemoBundle

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

Add a composer.json file

添加 composer.json 文件

src/Company/DemoBundle/composer.json:

src/Company/DemoBundle/composer.json

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

Now you have the base structure of your bundle

现在你有你的包的基本结构

Use it in another project

在另一个项目中使用它

composer.json:

作曲家.json:

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

Do:

做:

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

app/AppKernel:

应用程序/应用程序内核:

new Company\DemoBundle\CompanyDemoBundle(),

Work on it

在...上下功夫

  • You can clone your DemoBundle in the src/Companyfolder, then manually install it
  • You can use symlink
  • 您可以在src/Company文件夹中克隆您的 DemoBundle ,然后手动安装它
  • 您可以使用符号链接

Conclusion

结论

You can develop and test your bundle in your first project and use it with github and composer in your second project.

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

回答by flouflou2000

An important point to know is that you can commit into your repo from the /vendor. Indeed, composer creates a second remote called "composer" for each bundle (or package) that references the repo of the package, in order you can work on it in a working context. So the good practice is to register your package in your composer.json for all your projects and commit from your /vendor/MyCompany/MyBundle, from any project.

要知道的重要一点是,您可以从 /vendor 提交到您的存储库。实际上,composer 为每个包(或包)创建了一个名为“composer”的远程,它引用包的 repo,以便您可以在工作上下文中处理它。因此,好的做法是在您的 composer.json 中为您的所有项目注册您的包,并从您的/vendor/MyCompany/MyBundle, 从任何项目提交。

As a proof, just run git remote -vfrom any bundle in your vendor.

作为证明,只需git remote -v从您的供应商中的任何捆绑包运行即可。

The bad practice would be to consider your bundle as a separate project and have symlinks with it. The main reason why this is the bad practice is that you won't be able to declare dependancies with your bundle. Moreover you'll have some difficulties with the deployment of your projects.

不好的做法是将您的包视为一个单独的项目并与它有符号链接。这是不好的做法的主要原因是您将无法声明与您的包的依赖关系。此外,您在部署项目时会遇到一些困难。

回答by Manolo

In Symfony4, generate:bundlecommand is no longer available. Instead, you may follow this tutorial.

在 Symfony4 中,generate:bundle命令不再可用。相反,您可以按照本教程进行操作

First create a project with:

首先创建一个项目:

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

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

Then, create a my-project/lib/AcmeFooBundle/srcdirectory. Here will live your bundle. The namespace for this directory will be Acme\AcmeFooBundle, so if you create a service class at lib/AcmeFooBundle/src/Service/Foo.php, its namespace would be Acme\AcmeFooBundle\Service.

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

Now we need to tell composer autoloader to look for new classes at that new directory, so we need to edit composer.jsonautoloadsection:

现在我们需要告诉 composer autoloader 在那个新目录中寻找新的类,所以我们需要编辑composer.jsonautoload部分:

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

and run composer dump-autoload.

并运行composer dump-autoload

Now you only need to add your bundle class to config/bundles.php:

现在您只需要将您的包类添加到config/bundles.php

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

and dependency injection to load configuration from your bundle.

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

If you want to check your services before adding dependency injection, you can just autowire them at config/services.yml:

如果你想在添加依赖注入之前检查你的服务,你可以在以下位置自动装配它们config/services.yml

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

That's all. Follow best practicesand go on coding.

就这样。遵循最佳实践并继续编码。

PS: I've published a post with a few tips for developing Symfony reusable bundles.

PS:我已经发表了一篇关于开发 Symfony 可重用包的一些技巧的帖子。