braincrafted / static-site-bundle
CocurBuildBundle
Requires
- php: >=5.4
- braincrafted/json: ~0.2
Requires (Dev)
- mikey179/vfsstream: ~1.2
- mockery/mockery: ~0.8
- phpunit/phpunit: ~3.7.
- symfony/assetic-bundle: ~2.3
- symfony/console: ~2.4
- symfony/finder: ~2.4
This package is not auto-updated.
Last update: 2020-01-05 16:37:35 UTC
README
Symfony2 的静态站点生成器插件。 早期开发版本。
目录
动机
BraincraftedBootstrapBundle 的文档是一个用于测试和展示插件功能的 Symfony2 项目。我不想再在我的服务器上维护(并付费)另一个 Symfony2 项目,因此将其迁移到了 Github Pages。CocurBuildBundle 可以从 Symfony2 控制器创建静态 HTML 页面。
安装
您可以使用 Composer 安装 CocurBuildBundle。将其添加到您的 composer.json
{ "require": { "cocur/build-bundle": "dev-master" } }
您还需要将插件添加到您的 AppKernel.php
// app/AppKernel.php class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new Cocur\Bundle\BuildBundle\CocurBuildBundle(), ); // ... return $bundles; } // ... }
配置
enable_assetic
:当此选项为true
时,在构建站点时执行assetic:dump
命令。如果没有定义此选项,并且已安装AsseticBundle
,则将其激活。build_directory
:构建站点的保存目录。base_url
:静态站点的基准 URL。当 HTML 保存不在根目录时非常有用。大多数命令都有一个选项可以单独覆盖此设置。index_name
:如果一个路由不包含文件名,则此值附加到路由。默认值是index.html
。
默认配置如下
# app/config/config.yml cocur_build: build_directory: "%kernel.root_dir%/../build/site" base_url: '' index_name: index.html generators: ~
生成器
如果一个动作需要参数,您可以使用生成器从各种来源加载参数。该插件包含四个默认生成器
- 文件生成器:文件中的每一行都是一个参数;只能与需要单个参数的动作一起使用
- 目录生成器:给定目录中每个文件或目录的名称都是一个参数;只能与需要单个参数的动作一起使用
- JSON 生成器:包含对象数组的文件;每个对象代表一个动作的参数;可以用于需要多个参数的动作
- CSV 生成器:每行包含动作的参数;必须包含包含参数名称的标题行;可用于需要多个参数的动作
您可以在应用的 config.yml
中按路由配置生成器。
文件生成器
参数由文件生成。
必需选项
filename
parameter
在以下示例中,我们有一个名为 acme_demo_page
的路由,路径为 /p/{page}
,我们希望从名为 data.txt
的文件中生成 page
参数。
# app/config/config.yml cocur_build: generators: page: route: acme_demo_page generator: cocur_build.file_generator options: filename: "%kernel.root_dir%/../data.txt" parameter: page
我们现在需要包含每行一个参数的 data.txt
文件。
products
about
contact
CocurBuildBundle将渲染以下页面
/p/products
/p/about
/p/contact
目录生成器
参数由目录中文件的名称生成。
必需选项
directory_name
parameter
在这个例子中,我们有一个路由acme_demo_article
,路径为/article/{slug}
,我们想要渲染目录中每个文件的页面。
# app/config/config.yml cocur_build: generators: article: route: acme_demo_article generator: cocur_build.directory_generator options: directory_name: "%kernel.root_dir%/../articles" parameter: slug
目录articles/
包含以下文件
articles/
⊢ 2013-12-03-bootstrap-bundle-2-0.md
⊢ 2013-12-04-cocur-bundle-0-1.md
CocurBuildBundle将渲染以下页面
article/2013-12-03-bootstrap-bundle-2-0
article/2013-12-04-cocur-bundle-0-1
JSON生成器
参数由JSON文件生成。
必需选项
filename
可选选项
parameters
:仅使用这些参数生成页面
考虑一个路由acme_demo_categorypage
,路径为/page/{category}/{page}
。我们需要两个参数category
和page
,我们希望从名为data.json
的JSON文件中生成。
# app/config/config.yml cocur_build: generators: categorypage: route: acme_demo_categorypage generator: cocur_build.json_generator options: filename: "%kernel.root_dir%/../data.json"
data.json
文件必须包含一个数组,其中每个元素都是一个具有category
和page
属性的对象
[ { "category": "foo", "page": "bar" }, { "category": "foo", "page": "baz" } ]
CocurBuildBundle将渲染以下页面
/pages/foo/bar
/pages/foo/baz
CSV生成器
参数由CSV文件生成。
必需选项
filename
可选选项
delimiter
(默认值为,
)enclosure
(默认值为"
)escape
(默认值为\
)parameters
:仅使用这些参数生成页面
现在我们想要使用CSV文件persons.csv
渲染路由acme_demo_person
,模式为/person/{name}/{age}/{city}
。
# app/config/config.yml cocur_build: generators: person: route: acme_demo_person generator: cocur_build.csv_generator options: filename: "%kernel.root_dir%/../persons.csv"
CSV文件必须包含三列和一个包含name
、age
和city
的标题行。
"name", "age", "city"
"Florian", "27", "Vienna"
"Daniela", "22", "Vienna"
CocurBuildBundle将渲染以下页面
/person/Florian/27/Vienna
/person/Daniela/22/Vienna
YAML生成器
参数由YAML文件生成。
必需选项
filename
可选选项
parameters
:仅使用这些参数生成页面
如果我们想要渲染路由acme_demo_person
,模式为/person/{name}/{age}/{city}
,我们也可以使用YAML文件persons.yaml
。
# app/config/config.yml cocur_build: generators: person: route: acme_demo_person generator: cocur_build.yaml_generator options: filename: "%kernel.root_dir%/../persons.yml"
YAML文件persons.yml
必须为每个人包含一个列表元素,并为每个参数包含一个命名属性。
- name: Florian age: 27 city: Vienna - name: Daniela age: 22 city: Vienna
CocurBuildBundle将渲染以下页面
/person/Florian/27/Vienna
/person/Daniela/22/Vienna
前端主体生成器
参数由目录中文件的标题生成。
必需选项
directory_name
可选选项
parameters
:仅使用这些参数生成页面
在这个例子中,我们有一个路由acme_demo_article
,路径为/article/{category}/{slug}
,我们想要渲染目录中每个文件的页面。
# app/config/config.yml cocur_build: generators: article: route: acme_demo_article generator: cocur_build.front_matter_generator options: directory_name: "%kernel.root_dir%/../articles"
例如,文件articles/2013-12-03-bootstrap-bundle-2-0.md
可能看起来像
--- category: dev slug: bootstrap-bundle-2-0 --- This is the rest of the file. Just some text.
CocurBuildBundle将渲染以下页面
article/dev/bootstrap-bundle-2-0
用法
构建命令是CocurBuildBundle提供的主要命令。它渲染所有页面并将资产存放到构建目录中。
$ php app/console cocur:build
HTML代码将保存在由cocur_build.build_directory
配置的目录中。
注意:CocurBuildBundle可以处理参数化动作,如果为这些路由配置了生成器。
当你使用构建命令时,CocurBuildBundle使用Symfony2内核模拟对页面的请求。内核在命令调用的环境中启动。如果你想要在生产环境中构建页面,你需要在prod
环境中构建它们。
$ php app/console cocur:build -e prod
如果cocur:build
在prod环境中调用,则在渲染之前清除缓存。
你可以使用clean
命令从构建目录中删除所有文件
$ php app/console cocur:clean
作者
许可证
本软件包遵循MIT许可协议。查看MIT许可协议。更多信息请参阅LICENSE文件。