grizzlylab / assets-version-bundle
自动化更新 Symfony 5 项目中仍然使用 parameters.yml 的资产版本过程
Requires
- php: ^7.4
- symfony/framework-bundle: ^5.2
Requires (Dev)
- phpunit/phpunit: ~9.4
- symfony/filesystem: ^5.2
- symfony/finder: ^5.2
Suggests
- symfony/assetic-bundle: For combining and compressing assets
README
在每个部署中手动更新资产版本真的很痛苦。这个 Symfony2 & Symfony3 扩展自动处理这个过程,从而使你的生活更加愉快。
该扩展可以从 Symfony 控制台读取和写入 assets_version
参数到 app/config/parameters.yml
(或任何其他 *.yml
文件)。原始文件格式被仔细保留,所以如果存在的话,你不会丢失参数组之间的注释或空行。
想象一下你的项目配置如下所示
app/config/config.yml
# Symfony >=2.7, >=3.0 framework: # ... assets: version: "%assets_version%" # Symfony <=2.6 framework: # ... templating: { engines: ['twig'], assets_version: "%assets_version%" } # ...
app/config/parameters.yml
parameters: # ... assets_version: v042 # ...
你只需调用 bin/console assets-version:increment
,v042
将变为 v043
,所有你的资产都将获得新的 URL:my_cosy_homepage.css?v042
→ my_cosy_homepage.css?v043
。更多功能将在下面描述。
更新资产版本后,清空 prod
缓存很重要,以便更改生效(就像其他任何应用程序参数一样)。
为项目资产进行版本控制是一种常见的良好做法。有关 assets_version
参数的更多信息,请参阅 Symfony 文档
https://symfony.com.cn/doc/2.6/reference/configuration/framework.html#assets-version
安装
运行 composer require kachkaev/assets-version-bundle
在 app/AppKernel.php
中注册该扩展
$bundles = array( // ... new Kachkaev\AssetsVersionBundle\KachkaevAssetsVersionBundle(), // ... );
新安装第三方扩展?Symfony 文档会帮助你
https://symfony.com.cn/doc/current/cookbook/bundles/installation.html
配置
以下是该扩展的默认配置
kachkaev_assets_version: # path to the file that contains the assets version parameter file_path: '%kernel.root_dir%/config/parameters.yml' # name of the parameter to work with parameter_name: assets_version # name of the class that reads and writes the assets version parameter manager: Kachkaev\AssetsVersionBundle\AssetsVersionManager
不需要在 app/config/config.yml
中添加任何内容,以使这些值生效。
选项 1(简单):服务器上执行资产版本控制
如果你没有使用 AsseticBundle 压缩你的 CSS 和 JS 文件,或者你在生产服务器上调用 assetic:dump
,你通常不希望 assets_version
的更改出现在你的 Git 仓库中。那么你只需要做以下几步
- 修改你本地的
parameters.yml.dist
和parameters.yml
parameters: # ... assets_version: v000
-
在
app/config/config.yml
中启用%assets_version%
(见此文件顶部) -
提交和推送本地更改(这将包括
app/AppKernel.php
中的新行以及composer.json
和composer.lock
中的编辑) -
转到服务器,运行
git pull
和composer install
。由于你在parameters.yml.dist
中有新的条目,你将需要确认是否要将assets_version: v000
复制到app/config/parameters.yml
。你确实需要这样做。按回车键。 -
全部完成!现在每次你想更新资产版本时,只需在服务器上调用以下命令
bin/console assets-version:increment --env=prod
bin/console cache:clear --env=prod
# bin/console assetic:dump --env=prod # if you are using assetic
注意:如果你还没有切换到 Symfony3,请将 bin/console
替换为 app/console
。
选项 2(推荐):源代码控制下的资产版本控制
如果您的应用在多个生产服务器上运行,或者您有很多CSS和JS需要使用assetic:dump
进行压缩,那么将编译后的资源及其版本保留在项目的Git仓库中会很有益处。部署准备会花费更多一些时间,但其余的过程几乎是瞬间完成的。您将不需要在托管服务器上使用UglifyCSS、UglifyJS或其他assetic过滤器,并且可以瞬间切换到任何稳定的项目版本。廉价的服务器在编译资源时可能会遇到困难,因为有时这需要大量的处理器时间,所以您也可以避免这种潜在的问题。
由于app/config/parameters.yml
被列在.gitignore
中,因此assets_version
应该存储在其他位置。
- 创建
app/config/assets_version.yml
,并从app/config/config.yml
中链接到它
app/config/assets_version.yml
parameters: assets_version: v000
app/config/config.yml
imports: - { resource: assets_version.yml } # ...
不要将app/config/assets_version.yml
添加到.gitignore
!
-
在
app/config/config.yml
中启用%assets_version%
(见此文件顶部) -
将以下行添加到
app/config/config.yml
kachkaev_assets_version: file_path: "%kernel.root_dir%/config/assets_version.yml"
- 这样,您就准备好提交您的内容了!现在,每次您想要在服务器上更新资源时,请遵循以下流程
在本地机器上
bin/console assets-version:increment
bin/console cache:clear --env=prod
bin/console assetic:dump --env=prod
git commit # if you are doing this from a shell
在生产服务器上
bin/console cache:clear --env=prod git pull
确保编译后的资源不在.gitignore
中!
提示:通过在shell脚本中保留常见的命令序列,以更少的输入完成更多的工作。例如
bin/refresh_prod
(用于本地机器)
#!/bin/sh PROJECT_DIR=$( cd "$( dirname "$0" )" && pwd )/.. if [ "$1" = 'v' ]; then $PROJECT_DIR/bin/console assets-version:increment --env=prod fi $PROJECT_DIR/bin/console cache:clear --env=prod # rm $PROJECT_DIR/web/compiled_assets/* $PROJECT_DIR/bin/console assetic:dump --env=prod cat $PROJECT_DIR/app/config/assets_version.yml
bin/update_from_repo
(用于服务器)
#!/bin/sh PROJECT_DIR=$( cd "$( dirname "$0" )" && pwd )/.. cd $PROJECT_DIR & git pull cd $PROJECT_DIR & composer install --prefer-dist --optimize-autoloader rm -rf $PROJECT_DIR/app/cache/prod rm -rf $PROJECT_DIR/app/cache/dev $PROJECT_DIR/bin/console cache:clear --env=prod
控制台命令
捆绑程序向symfony控制台添加了两个命令:assets-version:increment
和assets-version:set
。
使用示例
# Increments assets version by 1 (e.g. was v1, became v2; was 0042, became 0043 - leading letters and zeros are kept) bin/console assets-version:increment # Increments assets version by 10 (e.g. was v1, became v11; was 0042, became 0052) bin/console assets-version:increment 10 # Sets version to "1970-01-01_0000" bin/console assets-version:set 1970-01-01_0000 # Sets version to "abcDEF-something_else" (no numeric part, so assets_version:increment will stop working) bin/console assets-version:set abcDEF-something_else # Decrements assets version by 10 (e.g. was 0052, became 0042; was lorem.ipsum.0.15, became lorem.ipsum.0.5) # Note two dashes before the argument that prevent symfony from parsing -1 as an option name bin/console assets-version:increment -- -10 # Decrementing version by a number bigger than current version results 0 (e.g. was v0010, became v0000) bin/console assets-version:increment -- -1000
资源版本的值必须仅由字母、数字和以下字符组成:.-_
。只有当当前参数值是整数或以数字结尾时,递增才起作用。
请务必通过调用bin/console cache:clear --env=prod
来清除缓存,以便更改在生产环境中生效。
Capifony集成
如果您使用Capifony,您可以在deploy.rb
中放置以下代码来自动化部署过程中assets_version
的递增。
before "symfony:cache:warmup", "assets_version:increment", "symfony:cache:clear" namespace :assets_version do task :increment do capifony_pretty_print "--> Increase assets_version" run "#{latest_release}/bin/console assets-version:increment --env=#{symfony_env_prod}" capifony_puts_ok end end