yish/generators

This package extends the core file generators that are included with Laravel 5

2.1.0 2019-03-02 15:43 UTC

README

Build Status Total Downloads Latest Stable Version License License

This package extends the core file generators that are included with Laravel 5 / 6

Requirement

PHP >= 7

Laravel >= 5

  • 5.4 before using branch 1.1.x
  • 5.5 ~ 5.7 using branch 2.0.x
  • 5.8 or later using branch 2.1.x

Installation

Install via composer

$ composer require yish/generators

Registing Service Provider

If you are using laravel 5.5 or later, you can use auto discover, you don't need put in service provider to app.php.

<?php
//app.php
'providers' => [
    \Yish\Generators\GeneratorsServiceProvider::class,
],

Generating Service

It can be generating class service.

$ php artisan make:service UserService
<?php
namespace App\Services;

use Yish\Generators\Foundation\Service\Service;

class UserService
{
    protected $repository;

    //
}

Also, it supports abstract service. You should inject your repository or model and then use it.

all()
create($attributes)
first()
firstBy($column, $value)
find($id)
findBy($column, $value)
get()
getBy($column, $value)
update($id, $attributes)
updateBy($column, $value, $attributes)
destroy($id)
destroyBy($column, $value)
paginate($page = 12)
paginateBy($column, $value, $page = 12)

Generating Repository

It can be generating class repository.

$ php artisan make:repository UserRepository
<?php
namespace App\Repositories;

use Yish\Generators\Foundation\Repository\Repository;

class UserRepository
{
    protected $model;

    //
}

Also, it supports abstract repository. You should inject your model and then use it.

all($columns = ['*'])
create($attributes)
update($id, array $attributes, array $options = [])
updateBy($column, $value, array $attributes = [], array $options = [])
first($columns = ['*'])
firstBy($column, $value, $columns = ['*'])
find($id, $columns = ['*'])
findBy($column, $value, $columns = ['*'])
get($columns = ['*'])
getBy($column, $value, $columns = ['*'])
destroy($ids)
destroyBy($column, $value)
paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
paginateBy($column, $value, $perPage = null, $columns = ['*'], $pageName = 'page', $page = null)

Generating Transformer

It can be generating class transformer.

$ php artisan make:transformer UserTransformer

Support

TransformContract

<?php
namespace Yish\Generators\Foundation\Transform;
interface TransformContract
{
    public function transform($attributes);
}

Helper / transformer()

// $instance => Transformer class.
// $attributes => Need transform data, maybe array or collection etc.
transformer(UserTransformer::class, $data);

Generating Formatter

It can be generating class formatter.

$ php artisan make:formatter UserFormatter
<?php

namespace App\Formatters;

use Illuminate\Http\Request;
use Yish\Generators\Foundation\Format\FormatContract;
use Yish\Generators\Foundation\Format\Statusable;

class PostFormatter implements FormatContract
{
    public function format(Request $request, $items = [], $message = '', $status = 200)
    {
        //
    }
}

Support

FormatContract

<?php
namespace Yish\Generators\Foundation\Format;
use Illuminate\Http\Request;
interface FormatContract
{
    public function format(Request $request, $items = []);
}

Statusable

You can use Statusable trait to help you faster building formalize format. Set property $status = true, you can get success format. $status must be boolean, if not you will get exception.

<?php

namespace App\Formatters;

use Illuminate\Http\Request;
use Yish\Generators\Foundation\Format\FormatContract;
use Yish\Generators\Foundation\Format\Statusable;

class PostFormatter implements FormatContract
{
    use Statusable;

    protected $status = true;
}

If not, you can set false to get failed format.

<?php

namespace App\Formatters;

use Illuminate\Http\Request;
use Yish\Generators\Foundation\Format\FormatContract;
use Yish\Generators\Foundation\Format\Statusable;

class PostFormatter implements FormatContract
{
    use Statusable;
    
    protected $status = false;
}

If you need customize message, you can do:

<?php

namespace App\Formatters;

use Illuminate\Http\Request;
use Yish\Generators\Foundation\Format\FormatContract;
use Yish\Generators\Foundation\Format\Statusable;

class PostFormatter implements FormatContract
{
    use Statusable;
    
    protected $status = false;
    
    public function message()
    {
        return 'hello world'.
    }
}

Or you can customize status code, you can do:

<?php

namespace App\Formatters;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Yish\Generators\Foundation\Format\FormatContract;
use Yish\Generators\Foundation\Format\Statusable;
use Yish\Generators\Foundation\Format\Formatter;

class Success extends Formatter implements FormatContract
{
   use Statusable;

    protected $status = false;

    public function code()
    {
        return Response::HTTP_ACCEPTED;
    }
}

If you need to customize what you need, check out Yish\Generators\Foundation\Format\Statusable get more detail.

Helper / formatter()

// $request => Must instance of `Illuminate\Http\Request`.
// $instance => Formatter class.
// $items => data.
formatter(request(), UserFormatter::class, $data);

Generating Presenter

It can be generating class presenter.

$ php artisan make:presenter UserPresenter
<?php

namespace App\Presenters;

class UserPresenter
{
    //
}

Generating Foundation

It can be generating class foundation.

$ php artisan make:foundation Taggable
<?php

namespace App\Foundation;

class Taggable
{
  //
}