timehunter/laravel-file-generator

A Laravel File Generator to automate creation of interface, trait and class files.

1.8.0 2018-12-12 12:12 UTC

This package is auto-updated.

Last update: 2022-06-12 20:28:58 UTC


README

Latest Version on Packagist Total Downloads

A Laravel File Generator which allows you to:

  1. quickly generate trait, class and interface
  2. automate the creation of repeated php files
  3. help you generate code base structure (modular,pattern etc.)

The main logic is using Blade template engine to populate the files. Feel free to look into the source code.

Preview Demo

Generation Demo

Next Release features

  1. will support command line generator
  2. will support schema validation
  3. will cover tests
  4. will support webpage realtime preview

Requirement

This package requires the following dependencies:

  • Laravel 5.x
  • php > 7 (if you need support for version below 7, please create issue ticket)

Installation

Via Composer

$ composer require timehunter/laravel-file-generator "^1.8.0"

If your Laravel framework version <= 5.4, please register the service provider in your config file: /config/app.php, otherwise please skip it.

'providers'=[
    ....,
    TimeHunter\LaravelFileGenerator\LaravelFileGeneratorServiceProvider::class
]

And also ( Laravel framework version <= 5.4)

'aliases'=[
     ....,
      'LaravelFileGenerator' => TimeHunter\LaravelFileGenerator\Facades\LaravelFileGenerator::class
 ]

Usage

Step 1

Create a file template in which you tell the generator what information will be appended to your file. e.g. the following is an example of Interface file template which implements InterfaceSimpleTemplateInterface:

<?php

namespace App\Structure;

use TimeHunter\LaravelFileGenerator\Interfaces\InterfaceSimpleTemplateInterface;

class ExampleSimpleInterfaceTemplate implements InterfaceSimpleTemplateInterface
{

    public function getTemplateData()
    {
        return [
            'directory' => app_path() . '/Example',
            'interface_name' => 'ExampleInterface',
            'namespace' => 'App\Example',
            'functions' => [
                'public function get()',
                'public function update()'
            ],
            'annotations'=[]
        ];
    }
}
  • getTemplateData() : return your interface details, please refer to Schema section

Step 2

Pass the template to the publish function from LaravelFileGenerator facade class

 LaravelFileGenerator::publish(
     new ExampleSimpleInterfaceTemplate(),
     new ExampleSimpleTraitTemplate(),
     new ExampleSimpleInterfaceTemplate()
     ...
 ); // publish() supports multiple parameters

Step 3

Check your folders if the file is generated.

You can also review the class before publishing:

 return LaravelFileGenerator::preview(new ExampleSimpleInterfaceTemplate());

The function returns a View, so you can include it in any controller to see the outcome.

Interfaces

Interface Usage Description
ClassSimpleTemplateInterface array schema type Class file
InterfaceSimpleTemplateInterface array schema type Interface file
TraitSimpleTemplateInterface array schema type Trait file

For array schema type, they have the same function which returns schema of template:

getTemplateData()

Trait Schema

    public function getTemplateData()
    {
        return [
            'directory' => app_path() . '/Example',
            'namespace' => 'App\Example',
            'use' => [
                'App\Http\Controllers\Controller'
            ],
            'trait_name' => 'ExampleTrait',
            'traits' => [
                'ExampleTrait'
            ],
            'annotations'=[],
            'functions' => [
                'public function get()',
                'public function update()'
            ]
        ];
    }

Interface Schema

    public function getTemplateData()
    {
        return [
            'directory' => app_path() . '/Test',
            'interface_name' => 'ExampleInterface',
            'namespace' => 'App\Example',
            'annotations'=[],
            'functions' => [
                'public function get()',
                'public function update()'
            ]
        ];
    }

Class Schema

    public function getTemplateData()
    {
        return [
            'class_type' => 'abstract class',
            'directory' => app_path() . '/Example',
            'namespace' =>'App\Example',
            'use' => [
                'App\Http\Controllers\Controller',
            ],
            'class_name' => 'ExampleClass',
            'extends' => 'Controller',
            'implements' => ['sss', 'sss'],
            'traits' => [
                'ExampleTrait'
            ],
            'properties' => [
                'protected $repo'
            ],
            'functions' => [
                'public function get()',
                'public function update()'
            ],
            'annotations'=[]
        ];
    }

Security

If you discover any security related issues, please email ryandadeng@gmail.com instead of using the issue tracker.

License

MIT. Please see the license file for more information.