constant-null / backstubber
帮助从模板(存根)生成完整的PHP文件
Requires
- php: >=5.4.0
- illuminate/support: ^5.2
Requires (Dev)
- bossa/phpspec2-expect: ~1.0
- mikey179/vfsstream: ^1.6
- phpspec/phpspec: ~2.5
This package is not auto-updated.
Last update: 2024-09-14 18:53:02 UTC
README
Backstubber使从模板生成PHP代码文件变得快速且简单。说再见吧,大量str_replace
。
安装
可以使用composer轻松安装Backstubber。要这样做,只需运行php composer.phar require-dev constant-null/backstubber
。或者您可以将以下内容添加到您的composer.json
文件中
{ "require-dev": { "constant-null/backstubber": "~0.1" } }
然后运行
$ php composer.phar update
使用方法
文件生成器
基本替换
假设我们有一个DummyStarship.stub模板
<?php class DummyClass { protected $captain = DummyCaptain; protected $officers = DummyOfficers; protected $crew = DummyCrew; }
其中$captain
应该是一个字符串,$officers
是一个数组,$crew
是数字。好吧,只需使用set()
方法将所需数据提供给backstubber!
use ConstantNull/Backstubber/FileGenerator as Generator; $generator = new Generator(); $generator->useStub('some/path/to/stubs/DummyStarship.stub') ->set('DummyOfficers', ['James T Kirk', 'Mr. Spock', 'Scott Montgomery']) ->set('DummyCaptain', 'James T. Kirk') ->set('DummyCrew', 430) ... // or pass an array ->set([ 'DummyOfficers' => ['James T Kirk', 'Mr. Spock', 'Scott Montgomery'], 'DummyCaptain' => 'James T. Kirk', 'DummyCrew' => 430 ]) ... // saving new file ->generate('path/to/generated/classes/EnterpriseClass.php');
set()
方法中的第一个参数是需要替换模板文件中的字符串,而第二个参数是需要插入的变量。Backstubber将根据它们的类型插入值,因此输出将类似于以下内容
protected $captain = 'James T. Kirk'; protected $officers = ['James T Kirk', 'Mr. Spock', 'Scott Montgomery']; protected $crew = 430;
但有时我们需要将变量原样插入,例如类或命名空间名称。为此,Backstubber有setRaw()
方法。此方法具有相同的签名,但只接收字符串。让我们更新前面的示例
use ConstantNull/Backstubber/FileGenerator as Generator; $generator = new Generator(); $generator->useStub('some/path/to/stubs/DummyStarship.stub') ->set('DummyOfficers', ['James T Kirk', 'Mr. Spock', 'Scott Montgomery']) ->set('DummyCaptain', 'James T. Kirk') ->set('DummyCrew', 430) // newly added methods ->setRaw('DummyClass', 'Enterprise') ->setRaw('DummyClassNamespace', 'Federation\\Ships') // or pass an array ->setRaw([ 'DummyClass' => 'Enterprise', 'DummyClassNamespace' => 'Federation\\Ships' ]) // saving new file ->generate('path/to/generated/classes/EnterpriseClass.php');
因此,在结果文件EnterpriseClass.php
中将包含以下内容
<?php namespace Federation\Ships; class Enterprise { protected $captain = 'James T. Kirk'; protected $officers = ['James T Kirk', 'Mr. Spock', 'Scott Montgomery']; protected $crew = 430; }
使用前缀
如果您为所有占位符使用相同的前缀(就像上面示例中的“Dummy”前缀一样),则可以使用withPrefix
方法。因此,以下代码和上面显示的代码将产生相同的结果。
use ConstantNull/Backstubber/FileGenerator as Generator; $generator = new Generator(); $generator->useStub('some/path/to/stubs/DummyStarship.stub') ->withPrefix('Dummy') ->set('Officers', ['James T Kirk', 'Mr. Spock', 'Scott Montgomery']) ->set('Captain', 'James T. Kirk') ->set('Crew', 430) // newly added methods ->setRaw('Class', 'Enterprise') ->setRaw('ClassNamespace', 'Federation\\Ships') // saving new file ->generate('path/to/generated/classes/EnterpriseClass.php');
具有分隔符的模板
使用上面示例中“Dummy”等前缀的基本文本替换,很好,但有时您可能希望更明确地指定要替换的部分。这就是分隔符发挥作用的地方!
例如,让我们使用Laravel Blade样式的分隔符"{{"和"}}",然后我们的模板文件将如下所示
<?php namespace {{ namespace }}; class {{ class }} { protected $captain = {{ captain }}; protected $officers = {{ officers }}; protected $crew = {{ crew }}; }
现在我们只需告诉backstubber使用我们的分隔符
use ConstantNull/Backstubber/FileGenerator as Generator; $generator = new Generator(); $generator->useStub('some/path/to/stubs/DummyStarship.stub') // set delimiters ->withDelimiters('{{', '}}') // assign substitutions ->set('officers', ['James T Kirk', 'Mr. Spock', 'Scott Montgomery']) ->set('captain', 'James T. Kirk') ->set('crew', 430) ->setRaw('class', 'Enterprise') ->setRaw('namespace', 'Federation\\Ships') // saving new file ->generate('path/to/generated/classes/EnterpriseClass.php');
就是这样!
多行数组
从版本0.3.0开始,backstubber支持多行数组格式。
默认情况下,关联数组将使用多行格式进行格式化,非关联数组将使用单行格式(就像上面示例中所示),因此以下内容
... $officers = [ 'Captain' => 'Jean Luc Picard', 'First officer' => 'William T. Riker', 'Tactical Officer' => 'Tasha Yar' ]; $generator->set('officers', $officers)
将生成以下内容
$officers = [ 'Captain' => 'Jean Luc Picard', 'First officer' => 'William T. Riker', 'Tactical Officer' => 'Tasha Yar' ];
默认行为可以通过使用ConstantNull\Backstubber\Utility\Formatter::setArrayMode
来更改。目前,此方法接受三个参数之一,它们是
Formatter::ARR_MODE_AUTO
用于默认行为Formatter::ARR_MODE_MULTILINE
强制backstabber以多行格式格式化所有数组Formatter::ARR_MODE_SINGLELINE
强制backstabber以单行格式格式化所有数组
作者
这个库是由我开发的,Mark Belotskiy。特别感谢(如承诺)我的朋友Dmitriy Shulgin帮助命名这个库。