icanhazstring / tempa-php
模板文件解析库
2.2.0
2023-02-21 10:26 UTC
Requires
- php: ^8.1
- symfony/console: ^5.4|^6.0
Requires (Dev)
- adlawson/vfs: ^0.12.1
- phpunit/phpunit: ^9.6.3
- squizlabs/php_codesniffer: ^3.7.1
README
用途
你们可能都遇到过一些 *.dist
或 *.skel
文件。这个库可以用来替换这些文件中的所有替换内容,也可以简单地列出它们。
安装
要安装这个库,只需使用 composer。
$ composer require icanhazstring/tempa-php
完成此操作后,您需要在项目中创建一个 tempa.json
文件。该文件包含有关模板文件的一些基本信息。
以下是一个示例
{ "fileExtensions": ["dist", "skel"], "prefix": "{$", "suffix": "}" }
使用方法
扫描
您可以使用 scan 命令扫描一个目录或单个文件。
$ vendor/bin/tempa file:scan <dir> [<config>]
您可以将其他任何配置传递给命令。默认情况下,脚本将从您执行脚本的位置获取 tempa.json
。
$ vendor/bin/tempa file:scan test/Demo $ vendor/bin/tempa file:scan test/Demo test/Demo/tempa.json Scanning for template files in: /home/icanhazstring/tempa-php/test/Demo ======================================================================= 2/2 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100% /home/ando/tempa-php/test/Demo/Sub/test.php.dist ------------------------------------------------ Line 3 : 'database' => '{$database}', Line 4 : 'username' => '{$user}' /home/ando/tempa-php/test/Demo/test.php.dist -------------------------------------------- Line 3 : 'placeholder' => '{$superAwesome}'
替换
要替换内容,请运行以下命令
$ vendor/bin/tempa file:substitute [-f|--mapfile [MAPFILE]] [--] <dir> [<config>] [<map>]...
$ vendor/bin/tempa file:substitute test/Demo/
$ vendor/bin/tempa file:substitute test/Demo/ --mapfile=test/Demo/maps/map.[json|php]
$ vendor/bin/tempa file:substitute test/Demo/ test/Demo/tempa.json database=localhost user=icanhazstring superAwesome=mega
Processing template files in: /home/icanhazstring/tempa-php/test/Demo
=====================================================================
2/2 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
交互式
您可以使用交互模式替换所有内容
$ vendor/bin/tempa file:interactive test/Demo $ vendor/bin/tempa file:interactive test/Demo test/Demo/tempa.json Interactive substitution for template files in: /home/icanhazstring/tempa-php/test/Demo ======================================================================================= Found 3 substitutes ------------------- database: > ...