Skip to content

Latest commit

 

History

50 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FileGenerator

Generate files using template config files.

Installation

composer require tsquare/file-generator

Usage:

Create a template file e.g., ./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 name used to fill placeholders.
 */
$template->name('Example');


/**
 * Define the file name.
 */
$template->fileName('{name}File.php');


/**
 * Define the contents of the file.
 */
$template->fileContent(
    <<<'FILE'

namespace App\Foo\{name};

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

function foo{name}() {
    return '{title}';
}

FILE
);

// Optionally, you can prepend the PHP start tag to avoid unwanted editor highlighting.
$template->prepend('<?php' . PHP_EOL);


/*
 * 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');

/**
 * FileEditor will use regular expressions, if the isRegex() method is called after an action.
 */
$editor->replace('/^foo/', 'bar')->isRegex();

$template->fileEditor($editor);
Initialize FileTemplate with the path to the template file, pass it to FileGenerator, and call create.
<?php

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

$template = FileTemplate::init(__DIR__ . '/template-config/Example.php');

$generator = new FileGenerator($template);

$generator->create();
The following template replace tokens are available.
{name}        : ExampleName
{camel}       : exampleName
{pascal}      : ExampleName
{underscore}  : example_name
{dash}        : example-name
{plural}      : ExampleNames
{singular}    : ExampleName
{upper}       : EXAMPLENAME
{lower}       : examplename
{title}       : Example Name

About

Generate files using template config files.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages