Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Latest commit

 

History

History
361 lines (303 loc) · 7.49 KB

README.md

File metadata and controls

361 lines (303 loc) · 7.49 KB

Laravel Oh Generators

Build Status Total Downloads Latest Stable Version License License

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

Requirement

PHP >= 7

Laravel >= 5

  • 5.4 before using branch 1.1.x
  • 5.5 ~ 5.7 using branch 2.0.x
  • 5.8 - 8.x using branch 3.x.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
{
  //
}

Generating Transport

It can be generating class transport.

$ php artisan make:transport UserTransport
<?php

namespace App\Transports;

class UserTransport
{
  //
}

Generating Parser

It can be generating class parser.

$ php artisan make:parser UserParser
<?php

namespace App\Parsers;

use Yish\Generators\Foundation\Parser\Parser;

class UserParser extends Parser
{
    public function parse(array $items)
    {
        return parent::parse($items);
    }

    public function keys()
    {
        return [
            'name',
            'ages',
            'location'
        ];
    }
}
$parser = app(UserParser::class)->parse(['Yish', 30, 'Taipei']);
// ['name' => 'Yish', 'ages' => 30, 'location' => 'Taipei'];

Generating Response

It can be generating class response.

$ php artisan make:response UserResponse
<?php
namespace App\Responses;
use Illuminate\Contracts\Support\Responsable;
class UserResponse implements Responsable
{
    public function toResponse($request)
    {
        //
    }
}