This package adds file generators. It requires the Laravel framework.
composer require vagento/file-generator
Stub File Generator - From class
- Create a new class which extends the
\Vagento\FileGenerator\AbstractStubFileGenerator
class.
And add the getter methods to the class:
use Vagento\FileGenerator\AbstractStubFileGenerator;
class MyFileGenerator extends AbstractStubFileGenerator
{
// Required: This is the path to the file that will be generated
public function getPath(): string
{
return 'path/to/fileToGenerate.php';
}
// Required: This is the path to the stub file
public function getStubPath(): string
{
return __DIR__ . '/stubs/file.blade.php';
}
// Optional: This is the data that will always be passed to the stub file
public function getStubData(): array
{
// [Key (string) => Value (mixed)]
return ['always' => 5];
}
}
- Create a new stub file, which must end with
.blade.php
.
You can use the blade syntax:
Filename: file.blade.php
Always: {{ $always }}
- Create a new instance of the class and call the
generate()
method.
You can also chain the methods.
$generator = new MyFileGenerator();
// Generate the file
$generator->generate();
Which will result in the following file:
Filename: file.blade.php
Always: 5
The generate()
method will generate the file and overwrite any existing file.
Stub File Generator - From setters
- Create a new stub file, which must end with
.blade.php
.
You can use the blade syntax:
Filename: file.blade.php
Data to add: {{ $dataToAdd }}
- Create a new instance of the class and call the
generate()
method.
You can use the constructor or use the methods (or chain them).
$generator = new \Vagento\FileGenerator\Generators\StubFileGenerator(
'path/to/fileToGenerate.php', // Path to the file which will be generated
__DIR__ . '/stubs/file.blade.php', // Path to the stub file
['dataToAdd' => 10] // Optional: Data that will be passed to the stub file
);
// Generate the file
$generator->generate();
// OR
$generator = new \Vagento\FileGenerator\Generators\StubFileGenerator();
// Set the path to the file which will be generated
$generator->setPath('path/to/fileToGenerate.php');
// Set the path to the stub file
$generator->setStubPath(__DIR__ . '/stubs/file.blade.php');
// Optional: Add data to the stub file
$generator->addStubData(['dataToAdd', 10]);
// Generate the file
$generator->generate();
// OR
// Chain methods
$generator->setPath('path/to/fileToGenerate.php')
->setStubPath(__DIR__ . '/stubs/file.blade.php')
->addStubData(['dataToAdd', 10])
->generate();
Which will result in the following file:
Filename: file.blade.php
Data to add: 10
The generate()
method will generate the file and overwrite any existing file.
Give a ⭐️ if this project helped you!
Copyright © 2021 Valentin Wotschel.
This project is MIT licensed.