paulhenri-l / generator
从规范生成文件
2.0.0
2020-10-27 21:38 UTC
Requires
- php: ^7.3
- illuminate/filesystem: ^8.0
Requires (Dev)
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-09-28 05:54:04 UTC
README
从规范生成文件。此工具在脚手架命令中非常有用。
安装
只需引入该包即可。
composer require paulhenri-l/generator
用法
生成器使用规范来生成新文件。规范是实现了PaulhenriL\Generator\GeneratorSpecification
接口的类。
然后您将此规范传递给Generator
类的generate
方法
$generator = new \PaulhenriL\Generator\Generator(); $spec = new HelloWorld('Paul-Henri'); $generator->generate($spec);
定义规范
以下是一个示例规范。
<?php use PaulhenriL\Generator\GeneratorSpecification; class HelloWorld implements GeneratorSpecification { /** * The Name to greet. * * @var string */ protected $name; /** * HelloWorld constructor. */ public function __construct(string $name) { $this->name = $name; } /** * The template to use for generation. */ public function getTemplate(): string { // You may also store the template in another file and return it here // return file_get_contents('path/to/template'); return "<h1>{{ name }}</h1>"; } /** * Return the target path for the generated file. */ public function getTargetPath(): string { return getcwd() . '/welcome.html'; } /** * Return the replacements */ public function getReplacements(): array { return [ 'name' => $this->name ]; } /** * Return template processors. */ public function getProcessors(): array { return [ // Processors are callable that can process the file right before it // gets written to disk. Invokable classes are a good fit for this. // // new MyCustomProcessor(), ]; } }