问题描述
我试图找到任何有关使用Symfony Flex的文档,但到目前为止还算不上运气.
几乎所有文档都指向安装使用symfony Flex的捆绑软件,而不是如何创建使用它的捆绑软件.
我什至试图对某些软件包进行逆向工程,但是再一次,没有运气.
我的目标是在config/packages/my_bundle.yaml
中为我的捆绑包生成一个默认配置文件.
我需要知道的是我需要在哪里放置它以及我可以使用哪些env变量(如果有)?
什么是Flex食谱?
请记住,flex配方是软件包存储库中的单独的存储库,需要托管在Flex服务器上.此过程使配方对于第三方捆绑包(IMO)的用处大大降低.对于官方" Symfony食谱而言,它非常整洁,但对其他用户而言却不那么重要.
很有可能,您必须将食谱提交到 contrib存储库,它已批准并合并,因此可以作为社区食谱使用. (另一种选择是拥有一个自托管的Flex服务器,但这仅在测试配方(如有必要).
此外,请务必记住,大多数用户默认情况下不会启用contrib存储库.因此,如果这对于安装此捆绑包很重要,则您应该告诉用户如何安装食谱(例如,在捆绑包的 readme 文件中).
将其排除在外:基本上,Flex配方是一个带有manifest.json
文件的存储库,该文件具有用于启用某些配置程序"的特定键.
可用的manifest.json
配置器是:
捆绑包
应在bundles.php
上启用哪个捆绑软件.这些在安装配方时添加,在卸载配方时被删除.
{
"bundles": {
"Symfony\\Bundle\\DebugBundle\\DebugBundle": ["dev", "test"],
"Symfony\\Bundle\\MonologBundle\\MonologBundle": ["all"]
}
}
配置
配置"配置器处理两个键:copy-from-recipe
和copy-from-package
.第一个可以从 recipe 存储库复制文件,第二个可以从 package 存储库复制文件.
{
"copy-from-package": {
"bin/check.php": "%BIN_DIR%/check.php"
},
"copy-from-recipe": {
"config/": "%CONFIG_DIR%/",
"src/": "%SRC_DIR%/"
}
}
在此示例中,程序包中的文件bin/check.php
将被复制到项目%BIN_DIR%
中,并且配方程序包中的config
和src
的内容将被复制.相应的目录.
例如,这是提供默认配置文件的典型用例.根据您的要求,这是您要创建弹性配方的既定目的.
Env Vars
此配置器仅将适当的环境变量值添加到项目的.env
和.env.dist
中. (同样,如果您卸载了配方,这些内容将被删除)
{
"env": {
"APP_ENV": "dev",
"APP_DEBUG": "1"
}
}
作曲家脚本
此配置器将任务从项目的composer.json
添加到scripts:auto-scripts
阵列. auto-scripts
是在项目中每次执行composer update
或composer install
时都会执行的任务.
{
"composer-scripts": {
"vendor/bin/security-checker security:check": "php-script",
"make cache-warmup": "script",
"assets:install --symlink --relative %PUBLIC_DIR%": "symfony-cmd"
}
}
每行的第二部分指定命令的种类:常规的PHP脚本(php-script
),shell脚本(script
)或Symfony命令(,通过bin/console
执行.)
Gitignore
这会将条目添加到项目的.gitignore
文件中.
{
"gitignore": [
"/phpunit.xml"
]
}
manifest.json
的完整示例(摘自此处,就像这篇文章中的其他大多数示例一样):
{
"bundles": {
"Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle": ["all"]
},
"copy-from-recipe": {
"config/": "%CONFIG_DIR%/",
"public/": "%PUBLIC_DIR%/",
"src/": "%SRC_DIR%/"
},
"composer-scripts": {
"make cache-warmup": "script",
"assets:install --symlink --relative %PUBLIC_DIR%": "symfony-cmd"
},
"env": {
"APP_ENV": "dev",
"APP_DEBUG": "1",
"APP_SECRET": "%generate(secret)%"
},
"gitignore": [
".env",
"/public/bundles/",
"/var/",
"/vendor/"
]
}
其他配置器
有两个不依赖manifest.json
文件的配置器:
安装后输出.
如果配方包中存在名为post-install.txt
的文件,则安装完成后将显示其内容.您甚至可以使用此处定义的 样式,以获得更多的美感/令人讨厌./p>
示例:
<bg=green;fg=white> </>
<bg=green;fg=white> Much success!! </>
<bg=green;fg=white> </>
* <fg=yellow>Next steps:</>
1. Foo
2. <comment>bar</>;
3. Baz <comment>https://example.com/</>.
这将在安装完成后呈现给用户.
Makefile
如果配方存储库中存在名为Makefile
的文件,则此处定义的任务将被添加到项目的Makefile中(如果不存在则创建Makefile).
cache-clear:
@test -f bin/console && bin/console cache:clear --no-warmup || rm -rf var/cache/*
.PHONY: cache-clear
就这么简单.我猜比大多数软件包都不需要makefile命令,因此它比其他配置程序的使用要少得多.
您可以在此处阅读完整文档.
>I tried to find any documentation about using Symfony Flex but so far no luck.
Almost all docs point to installing a bundle that uses symfony Flex, not how to create a bundle that is using it.
I even tried to reverse engineer some of the packages but again, no luck.
My goal is to generate a default configuration file for my bundle in config/packages/my_bundle.yaml
.
What I need to know is where do I need to put it and what env variables (if any) will I have available?
What is a Flex Recipe?
Keep in mind that the flex recipe is a separate repository from your package repository, that needs to be hosted on a Flex server. The process make recipes much less useful for third party bundles, IMO. It's very neat for "official" Symfony recipes, but not so much for other users.
Most likely, you'll have to submit your recipe to the contrib repository, get it approved and merged so it's available as a community recipe. (The other option would be to have a self-hosted Flex server, but that's only useful while testing the recipe, if necessary).
Additionally, it's important to remember that most users will not have the contrib repository enabled by default. So if this is important for installing this bundle, you should tell your users how to do so before they install your recipe (e.g. in your bundle's readme file).
With that out of the way: Basically, a Flex recipe is a repository with a manifest.json
file with specific keys to enable certain "configurators".
The available manifest.json
configurators are:
Bundles
Which bundles should be enabled on bundles.php
. These are added when the recipe is installed, and removed when the recipe is uninstalled.
{
"bundles": {
"Symfony\\Bundle\\DebugBundle\\DebugBundle": ["dev", "test"],
"Symfony\\Bundle\\MonologBundle\\MonologBundle": ["all"]
}
}
Configuration
The "configuration" configurator deals with two keys: copy-from-recipe
and copy-from-package
. The first one can copy files from the recipe repository, the second one copies files from the package repository.
{
"copy-from-package": {
"bin/check.php": "%BIN_DIR%/check.php"
},
"copy-from-recipe": {
"config/": "%CONFIG_DIR%/",
"src/": "%SRC_DIR%/"
}
}
In this example, a file bin/check.php
in the package will be copied to the projects %BIN_DIR%
, and the contents of config
and src
on the recipe package will be copied the corresponding directory.
This is the typical use case to provide default configuration files, for example. From what you ask, this is your stated purpose for wanting to create a flex recipe.
Env Vars
This configurator simply adds the appropriate environment variable values to the project's .env
and .env.dist
. (Again, these would be removed if you uninstalled the recipe)
{
"env": {
"APP_ENV": "dev",
"APP_DEBUG": "1"
}
}
Composer Scripts
This configurator adds tasks to the scripts:auto-scripts
array from the project's composer.json
. The auto-scripts
are tasks that are executed every time composer update
or composer install
are executed in the project.
{
"composer-scripts": {
"vendor/bin/security-checker security:check": "php-script",
"make cache-warmup": "script",
"assets:install --symlink --relative %PUBLIC_DIR%": "symfony-cmd"
}
}
The second part on each line specifies what kind of command it is: a regular PHP script (php-script
), a shell script (script
), or a Symfony command (symfony-cmd
, executed via bin/console
).
Gitignore
This will add entries to the project's .gitignore
file.
{
"gitignore": [
"/phpunit.xml"
]
}
A complete example of a manifest.json
(lifted from here, as most other examples on this post):
{
"bundles": {
"Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle": ["all"]
},
"copy-from-recipe": {
"config/": "%CONFIG_DIR%/",
"public/": "%PUBLIC_DIR%/",
"src/": "%SRC_DIR%/"
},
"composer-scripts": {
"make cache-warmup": "script",
"assets:install --symlink --relative %PUBLIC_DIR%": "symfony-cmd"
},
"env": {
"APP_ENV": "dev",
"APP_DEBUG": "1",
"APP_SECRET": "%generate(secret)%"
},
"gitignore": [
".env",
"/public/bundles/",
"/var/",
"/vendor/"
]
}
Additional configurators
There are two configurators which do not rely on the manifest.json
file:
Post-install output.
If a file named post-install.txt
exists in the recipe's package, its content is displayed when installation is complete. You can even use styles as defined here, for additional prettiness/obnoxiousness.
Example:
<bg=green;fg=white> </>
<bg=green;fg=white> Much success!! </>
<bg=green;fg=white> </>
* <fg=yellow>Next steps:</>
1. Foo
2. <comment>bar</>;
3. Baz <comment>https://example.com/</>.
This will be presented to the user after the installation is complete.
Makefile
If a file named Makefile
exists in the recipe's repository, the tasks defined here would be added to the project's Makefile (creating the Makefile if it didn't exist).
cache-clear:
@test -f bin/console && bin/console cache:clear --no-warmup || rm -rf var/cache/*
.PHONY: cache-clear
Simple as that. I guess than most packages would not need a makefile command, so this would have much less use than other configurators.
You can read the full documentation here.
这篇关于如何为新捆绑包编写Symfony Flex配方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!