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.
- will support command line generator
- will support schema validation
- will cover tests
- will support webpage realtime preview
This package requires the following dependencies:
- Laravel 5.x
- php > 7 (if you need support for version below 7, please create issue ticket)
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
]
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
Pass the template to the publish function from LaravelFileGenerator facade class
LaravelFileGenerator::publish(
new ExampleSimpleInterfaceTemplate(),
new ExampleSimpleTraitTemplate(),
new ExampleSimpleInterfaceTemplate()
...
); // publish() supports multiple parameters
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.
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()
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()'
]
];
}
public function getTemplateData()
{
return [
'directory' => app_path() . '/Test',
'interface_name' => 'ExampleInterface',
'namespace' => 'App\Example',
'annotations'=[],
'functions' => [
'public function get()',
'public function update()'
]
];
}
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'=[]
];
}
If you discover any security related issues, please email ryandadeng@gmail.com instead of using the issue tracker.
MIT. Please see the license file for more information.