vagento/file-generator

Installs: 3

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 0

Open Issues: 0

Type:vagento-module

0.0.1 2021-12-11 19:32 UTC

This package is auto-updated.

Last update: 2024-05-12 03:38:35 UTC


README

Version License: MIT Twitter: WWoshid

This package adds file generators. It requires the Laravel framework.

Installation

composer require vagento/file-generator

Usage

Stub File Generator - From class
  1. 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];
    }
}
  1. Create a new stub file, which must end with .blade.php.
    You can use the blade syntax:
Filename: file.blade.php
Always: {{ $always }}
  1. 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
  1. 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 }}
  1. 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.

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2021 Valentin Wotschel.
This project is MIT licensed.