PHP code example of tsquare / file-generator

1. Go to this page and download the library: Download tsquare/file-generator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tsquare / file-generator example snippets




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_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);



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

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

$generator = new FileGenerator($template);

$generator->create();