tsquare/scaffolding

生成脚手架。

3.1.1 2021-02-28 19:47 UTC

This package is auto-updated.

Last update: 2024-09-17 21:59:14 UTC


README

使用 tsquare/file-generator 生成的类或类集的 CLI 组件。

安装

composer require tsquare/scaffolding

用法

创建一个模板文件,例如,./template-config/Example.php
<?php

use Tsquare\FileGenerator\FileTemplate;
use Tsquare\FileGenerator\FileEditor;

/**
 * @var FileTemplate $template
 */


/**
 * Define the application root.
 */
$template->appBasePath(dirname(__DIR__, 1));


/**
 * Define the base path for the file.
 */
$template->destinationPath(dirname(__DIR__, 1) . '/Sample');


/**
 * Define the file name. If not defined, the name specified in the command will be used.
 */
$template->fileName('{name}Extension');


/**
 * Define the contents of the file.
 */
$template->fileContent(<<<'FILE'
namespace App\Foo\{name};

$foo = '{underscore}s';
$bar = '{dash}';

function foo{name}() {
    return true;
}

FILE);


/*
 * Editing actions can be added, that will be used if the file already exists.
 */
$editor = new FileEditor();

$editor->insertBefore('
function inserted{pascal}Function() {
    return true;
}

', 'function foo{name}()');

$editor->insertAfter('
function another{pascal}Function()  {
    return true;
}
', 'function foo{name}() {
        return true;
    }
');

$editor->replace('another{pascal}Function', 'someOther{pascal}Function');

$template->ifFileExists($editor);
在应用根目录下创建一个文件,例如,scaffolding.php
#!/usr/bin/env php
<?php

require __DIR__ . '/vendor/autoload.php';

use Tsquare\Scaffolding\App;

$command = new App('1.0.0', __DIR__ . '/template-config');
$command->run();
以下命令将创建一个名为 SampleFile 的文件,使用模板 Example.php
php scaffolding.php make:file SampleFile Example
以下命令将创建一组文件,使用 template-config/ 目录中的文件目录中的模板
php scaffolding.php make:set Sample files
以下模板替换令牌可用。
{name}        : ExampleName (the first argument provided)
{camel}       : exampleName
{pascal}      : ExampleName
{underscore}  : example_name
{dash}        : example-name
{name:plural} : ExampleNames