webfactor/laravel-generators

Laravel generators for quickly creating entities.

3.3.6 2020-05-14 16:09 UTC

README

Latest Version on Packagist Software License StyleCI Build Status Coverage Status Quality Score Total Downloads

This is a package developed by us for internal use. It is supposed to help us during development and save plenty of time by automating many steps while creating typical CRUD entities with Laravel Backpack. You can write your own Services (they have to implement Webfactor\Laravel\Generators\Contracts\ServiceInterface) and register them in the generators.php config file, or use this package as an inspiration for your own implementation.

Install

Via Composer

This package is indended to be used only for development, not for production. Because of that we recommend to use require-dev:

composer require --dev webfactor/laravel-generators

Usage

php artisan make:entity {entity_name} {--schema=} {--migrate} {--ide=} {--git} 
  • entity_name: Recommendation: use singular for entity. see "Naming" for more information
  • --schema=: Here you can provide a schema-String
  • --migrate: will automatically call php artisan migrate after creating the migration file
  • --ide=: will open all files in your prefered IDE
  • --git: will add all files to git

If you want to add Services, Naming classes, Field Types, or IDE Opener you have to publish the config-file:

php artisan vendor:publish --provider="Webfactor\Laravel\Generators\GeneratorsServiceProvider" 

Services

All Services defined in the config file have to implement Webfactor\Laravel\Generators\Contracts\ServiceInterface and will then be called in the given order.

Included Services

Can be removed or extended by publishing config file:

  • MigrationService
  • FactoryService
  • SeederService
  • Backpack CRUD:
    • BackpackCrudModelService (incl. $fillable)
    • BackpackCrudRequestService (incl. rules())
    • BackpackCrudControllerService (incl. CrudColumns and CrudFields, more coming soon)
    • SidebarService
  • LanguageFileService
  • RouteFileService

Always available (activated by option):

  • OpenIdeService
  • AddToGitService

Schema

The intention of this package concerning Laravel Backpack CRUD is to provide an easy way to define standard Field Types with some default options and override them if necessary.

Example:

php artisan make:entity blog --schema="title:string,text:summernote" 

This will use the StringType and SummernoteType classes to create (besides all other files):

  • Blog.php with $fillable = ['title', 'text']
  • BlogRequest.php with predefined rules for each field
  • BlogCrudController.php with columns and fields for title and text

If you want to add/overwrite certain options you can use something like this:

php artisan make:entity blog --schema="title:string(unique|default:title);rule(required|min:3|max:64),text:summernote;field(label:Content);column(label:Content)" 

Field Types

Currently available Field Types (more coming soon):

  • Date
  • Number
  • String
  • Summernote (as a proof of concept)
  • Text

The available definitions in the Field Type classes currently are:

public $validationRule; // 'required|min:5'...
  
public $migrationField = [
    'type' => 'string', // any type available for a migration file
    // optional:
    // 'unique' => true
    // 'default' => 'value'
    // 'nullable' => true
    // etc.
];

public $crudColumn = [
    'type' => 'text', // or date, number, any backpack column
    // 'label' => 'Name of label'
    // ... any option
];

public $crudField = [
    'type' => 'text', // or date, number, any backpack field
    // 'label' => 'Name of label'
    // ... prefix, suffix... any option
];

Naming

You can provide your own naming convention classes by registering them in the config file. This classes should extend Webfactor\Laravel\Generators\Contracts\NamingAbstract to provide a certain base functionality.

Example for Webfactor\Laravel\Generators\Schemas\Naming\CrudController:

<?php

namespace Webfactor\Laravel\Generators\Schemas\Naming;

use Webfactor\Laravel\Generators\Contracts\NamingAbstract;

class CrudController extends NamingAbstract
{
    /**
     * @return string
     */
    public function getNamespace(): string
    {
        return $this->getAppNamespace() . 'Http\\Controllers\\Admin';
    }

    /**
     * @return string
     */
    public function getClassName(): string
    {
        return ucfirst($this->entity) . 'CrudController';
    }

    /**
     * @return string
     */
    public function getFileName(): string
    {
        return $this->getClassName() . '.php';
    }

    /**
     * @return string
     */
    public function getPath(): string
    {
        return app_path('Http/Controllers/Admin');
    }

    /**
     * @return string
     */
    public function getStub(): string
    {
        return __DIR__ . '/../../../stubs/crud-controller.stub';
    }
}

All naming classes defined in the config file will be parsed and saved with their keys to the $naming-array of the command. As the entire command is available in each service class, you can access ALL naming conventions everywhere!

For example you need the Request-namespace in the CrudController: $this->command->naming['crudRequest']->getNamespace().

Furthermore there is a helper to keep things a bit simpler if you are IN the service class of the coresponding naming class! Just define $key and you can access the naming conventions directly through $this->naming:

<?php

namespace Webfactor\Laravel\Generators\Services;

class MyService extends ServiceAbstract implements ServiceInterface
{
    protected $key = 'myNaming';

    protected function call()
    {
        echo $this->naming;
        // same to
        echo $this->command->naming['myNaming'];
        // but you can additionally access
        echo $this->command->naming['otherNaming'];
    }
}

Add files to git

With {--git} option all generated files will be added to git automatically. In your service class you have to add the generated file. You can:

  • use $this->command->addFile(SplileInfo $file) or
  • use $this->addGeneratedFileToIdeStack() if you use a naming key or
  • use the Webfactor\Laravel\Generators\Traits\CanGenerateFile define a naming key and just implement a buildFileContent() method

Open files in IDE

If specified we will automatically open all generated files in the IDE of your choice.
There are three options to use this feature (applied in this order):

  • {--ide=} command option
  • .env variable APP_EDITOR
  • config value config('app.editor')

The keys in the ides-Array of the config file are possible values for the command option. Per default we provide:

  • phpstorm: will open all files with pstorm CLI helper of PhpStorm
  • pstorm: will open all files with pstorm CLI helper of PhpStorm
  • sublime: will open all files with subl CLI helper of Sublime
  • subl: will open all files with subl CLI helper of Sublime
  • vscode: will open all files with code CLI helper of VSCode
  • code: will open all files with code CLI helper of VSCode

You can add other IDE-Opener classes. They have to implement Webfactor\Laravel\Generators\Contracts\OpenInIdeInterface.

In your service class you have to add the generated file to a stack (see "Add files to git" section)

Adaption

Feel free to write your own Services that fit your purposes!

Change log

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email thomas.swonke@webfactor.de instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.