timehunter / laravel-file-generator
A Laravel File Generator to automate creation of interface, trait and class files.
Requires
- php: >=7.0
Requires (Dev)
- mockery/mockery: ^1.1
- orchestra/testbench: ~3.0
- phpunit/phpunit: ~7.0
- sempro/phpunit-pretty-print: ^1.0
README
A Laravel File Generator which allows you to:
- quickly generate trait, class and interface
- automate the creation of repeated php files
- 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
- will support command line generator
- will support schema validation
- will cover tests
- 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
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.