Skip to content

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

Notifications You must be signed in to change notification settings

Vagento/File-Generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vagento - File Generator

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.

About

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

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages