nameau/filegen

PHP 文件和目录生成工具

0.0.5 2017-07-04 16:43 UTC

This package is not auto-updated.

Last update: 2024-09-12 15:11:09 UTC


README

Build Status SensioLabsInsight Scrutinizer Code Quality

一个辅助创建(复杂)文件和目录布局的小工具。

use Naneau\FileGen\Structure;
use Naneau\FileGen\Generator;

// Specify a structure to be generated
$structure = new Structure;
$structure
    ->directory('foo')
    ->file('bar/baz', 'these are the file contents');
    ->link('/some/file/somewhere', 'qux');

// Generate the structure
$generator = new Generator('/output/directory');
$generator->generate($structure);

输出

- /output/directory/
    - foo/
    - bar/
        - baz
    - qux => /some/file/somewhere

示例

复制文件

将现有文件复制到要生成的结构中的新文件很容易。

use Naneau\FileGen\Structure;
use Naneau\FileGen\File\Contents\Copy;

$structure = new Structure;
$structure->file('foo', new Copy('/from/this/file'));

使用 Twig 模板

可以使用 Twig 模板为文件提供内容。

use Naneau\FileGen\Structure;
use Naneau\FileGen\File\Contents\Twig;

// $twig = ...

// Load a template
$template = $twig->load('some_template.twig');

// Parameters for the template
$parameters = array('foo' => 'bar')

$structure = new Structure;
$structure->file('foo', new Twig($template, $parameters));

在结构旁边设置参数规范

在某些情况下,您可能需要在生成结构之前指定要使用的参数。然后可以使用控制台助手(见下文)查询这些参数,并在 Twig 模板中使用它们。

use Naneau\FileGen\Generator;
use Naneau\FileGen\Structure;
use Naneau\FileGen\File\Contents\Twig;

// $twig = ...
$template = ;
$structure = new Structure;
$structure
    // A parameter "foo" is expected
    ->param('foo')

    // A bar parameter with a description
    ->param('bar', 'Please specify "bar"')

    // Can use {{ foo }} and {{ bar }}
    ->file('someFile', new Twig($twig->load('someFile.twig'));

    // Can also use {{ foo }} and {{ bar }}
    ->file('anotherFile', new Twig($twig->load('anotherFile.twig'));

// Set a default value for foo
$structure->getParameterDefinition()->get('foo')->setDefaultValue('Foo!');

// Pass values for the structure's parameters to the generator
$generator = new Generator('/output/directory', array(
    'foo' => 'foo!'
    'bar' => 12345
));

// Generate the structure
$generator->generate($structure);

控制台助手

FileGen 附带了一个 (Symfony Console Helper](https://symfony.ac.cn/doc/current/components/console/introduction.html#console-helpers),它将使用 内置的问题助手 来请求参数值。

只需将助手添加到您的控制台助手集合中

use Naneau\FileGen\Console\Helper\ParameterHelper;

// $application = ...
$application->getHelperSet()->set(new ParameterHelper, 'filegenParameters');

并在您的命令中使用它

protected function execute(InputInterface $input, OutputInterface $output)
{
    // $structure = ...

    $helper = $this->getHelper('filegenParameters');

    // Ask for all parameters one by one
    $parameters = $helper->askParameters($structure, $input, $output);

    // Ask for a single parameter
    $fooParameter = $structure->getParameterDefinition()->get('foo');
    $fooValue = $helper->askParameter($fooParameter, $input, $output);
}