2017 © Pedro Peláez
 

library rsp-architecture

RSP Architecture for Laravel

image

cals/rsp-architecture

RSP Architecture for Laravel

  • Thursday, June 8, 2017
  • by Cals
  • Repository
  • 1 Watchers
  • 0 Stars
  • 12 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 10 Versions
  • 0 % Grown

The README.md

RSP

About RSP

RSP is a web application library used to make things easier while you building a plain Laravel application. Besides, it provides some efficient commands to let you free while you're coding., (*1)

RSP is short for Repository Service and Presenter., (*2)

How to use

Installation

You can run composer require cals/rsp to install RSP from Packagist., (*3)

Or, you can add "cals/rsp": "~2.0" to your composer.json and then run composer update in your terminal to install it., (*4)

Configuration

After you install the RSP, you should put Cals\RSP\RSPServiceProvider::class in your config/app.php providers array to make it work., (*5)

If you use Laravel 5.5 and after, you don't need do it cause laravel will auto load the provider., (*6)

Then you should run php artisan vendor:publish --tag=rsp to publish rsp.php., (*7)

Usage

Repository

You can use make:repository to create a repository and its' interface, or you can create them manually if you want to. Remember that the repository should extends Cals\RSP\Repositories\Implementations\Repository if you create it manually., (*8)

The repository is like below:, (*9)

<?php

namespace App\Repositories\Implementations;

use App\Models\Example;
use App\Repositories\Interfaces\ExampleRepositoryInterface;
use Cals\RSP\Repositories\Implementations\Repository;

class ExampleRepository extends Repository implements ExampleRepositoryInterface
{
    /**
     * Get the full name of model.
     *
     * @return mixed
     */
    function model()
    {
        return Example::class;
    }

    // Put your code here...
}

And the interface is like below:, (*10)

<?php

namespace App\Repositories\Interfaces;

interface ExampleRepositoryInterface
{
    // Put your code here...
}

As you can see, RSP use Eloquent ORM to operate database, so you should create models to map tables. We recommend you and only put your models in app/Models/ instead of app/ which will make your project more plain., (*11)

The repository we extended provides nine methods:, (*12)

  • store(array $inputs) You can use it to store data. The type of returned value is subclass of Illuminate\Database\Eloquent\Model.
  • all() You can use it to get all records. One thing you should notice is that the type of returned value is Illuminate\Database\Eloquent\Collection rather than array.
  • paginate(array $credentials = null, $page, $perPage = 15) You can use it to paginate records. One thing you should notice is that the type of returned value is Illuminate\Pagination\LengthAwarePaginator rather than array.
  • get(array $credentials = null, array $columns = ['*']) You can use it to get records. One thing you should notice is that the type of returned value is Illuminate\Database\Eloquent\Collection rather than array.
  • getRecordsSortBy(array $credentials = null, array $columns = ['*'], $field = 'id', $asc = true) You can use it to get records sort by the field you give. One thing you should notice is that while $asc is true means the returned value is ascending sorted otherwise is descending sorted. The other is that the type of returned value is Illuminate\Database\Eloquent\Collection rather than array.
  • find(array $credentials = null) You can use it to find a record which satisfy credentials. The type of returned value is subclass of Illuminate\Database\Eloquent\Model.
  • update(array $credentials, array $inputs) You can use it to update data which satisfy credentials. The type of returned value is boolean.
  • destroy(array $credentials) You can destroy data which satisfy credentials. The type of returned value is boolean.
  • builder(array $credentials = null) You can use it to create you own method. The type of returned value is Illuminate\Database\Eloquent\Builder.

While the returned value has only one record when you use get(array $columns = ['*'],array $crendentials = null), it is still an instance of Illuminate\Database\Eloquent\Collection. So if you want to find only one record and wish its' type is Illuminate\Database\Eloquent\Model, you can use find(array $credentials = null) or finish it yourself using the method builder() we provided., (*13)

While we use RSP, we do not use repository directly in controller. Repository should always provide methods to let service to use it., (*14)

Service

You can use make:service to create a service and its' interface, or you can create them manually if you want to. Remember that the service should extends Cals\RSP\Services\Implementations\Service if you create it manually., (*15)

The service is like below:, (*16)

<?php

namespace App\Services\Implementations;

use App\Repositories\Interfaces\ExampleRepositoryInterface;
use App\Services\Interfaces\ExampleServiceInterface;
use Cals\RSP\Services\Implementations\Service;

class ExampleService extends Service implements ExampleServiceInterface
{
    /**
     * ExampleService constructor.
     *
     * @param ExampleRepositoryInterface $repository
     */
    public function __construct(ExampleRepositoryInterface $repository)
    {
        $this->repository = $repository;
    }

    // Put your code here...
}

And the interface is like below:, (*17)

<?php

namespace App\Services\Interfaces;

interface ExampleServiceInterface
{
    // Put your code here...
}

The service we extended provides eight methods:, (*18)

  • store(array $inputs)
  • all()
  • paginate(array $credentials = null, $page, $perPage = 15)
  • get(array $credentials = null, array $columns = ['*'])
  • getRecordsSortBy(array $credentials = null, array $columns = ['*'], $field = 'id', $asc = true)
  • find(array $credentials = null)
  • update(array $credentials, array $inputs)
  • destroy(array $credentials)

These eight methods call methods provided by repository simply. So you can override it to satisfy your needs. And the type of returned value is like above listed in Repository., (*19)

Presenter

You can use make:presenter to create a presenter and its' interface, or you can create them manually if you want to. Remember that the presenter should extends Cals\RSP\Presenters\Implementations\Presenter if you create it manually., (*20)

The presenter is like below:, (*21)

<?php

namespace App\Presenters\Implementations;

use App\Presenters\Interfaces\ExamplePresenterInterface;
use Cals\RSP\Presenters\Implementations\Presenter;

class ExamplePresenter extends Presenter implements ExamplePresenterInterface
{
    // Put your code here...
}

And the interface is like below:, (*22)

<?php

namespace App\Presenters\Interfaces;

interface ExamplePresenterInterface
{
    // Put your code here...
}

The presenter we extended provides two methods:, (*23)

  • limitLength($field, $length = 40)
  • differentiateForHumans(Carbon $carbon)

The first method is used to limit length and the second method is used to show different form the time you give to now., (*24)

Bind

After you create your class, you need put it in rsp.php:, (*25)

<?php
/**
 * Created by PhpStorm.
 * User: Cals
 * Date: 2017/3/8
 * Time: 14:03
 */

return [

    /*
    |--------------------------------------------------------------------------
    | Repositories
    |--------------------------------------------------------------------------
    |
    | This array is the list of your repository. The key should be the full
    | name of interface and the value should be the full name of
    | implementation.
    */

    'repositories' => [],

    /*
    |--------------------------------------------------------------------------
    | Services
    |--------------------------------------------------------------------------
    |
    | This array is the list of your service. The key should be the full name
    | of interface and the value should be the full name of implementation.
    */

    'services' => [],

    /*
    |--------------------------------------------------------------------------
    | Presenters
    |--------------------------------------------------------------------------
    |
    | This array is the list of your presenter. The key should be the full name
    | of interface and the value should be the full name of implementation.
    */
    'presenters' => []
];

Such as:, (*26)

    'repositories' => [
        'App\Repositories\Interfaces\ExampleRepositoryInterface' => 'App\Repository\Implementations\ExampleRepository'
    ],

Then Laravel can bind the interface on the implementation., (*27)

Commands

We provide some commands for you to create files which have some basic codes, and you can put your own code into it to satisfy your needs., (*28)

The commands list below:, (*29)

  • make:repository You can use it to create repository class and its' interface.
  • make:service You can use it to create service class and its' interface.
  • make:presenter You can use it to create presenter class and its' interface.
  • rsp:generate You can use it to generate repositories, services and presenters based on registration.

Contributing

Thank you for considering contributing to the RSP Architecture library!, (*30)

Author

Cals Ranna, (*31)

License

The RSP Architecture is an open-sourced library licensed under the MIT license., (*32)

The Versions

08/06 2017

dev-master

9999999-dev

RSP Architecture for Laravel

  Sources   Download

MIT

The Requires

 

by CalsRanna

08/06 2017

v1.0

1.0.0.0

RSP Architecture for Laravel

  Sources   Download

MIT

The Requires

 

by CalsRanna

21/04 2017

v0.1-rc.8

0.1.0.0-RC8

RSP Architecture for Laravel

  Sources   Download

MIT

The Requires

 

by CalsRanna

21/04 2017

v0.1-rc.7

0.1.0.0-RC7

RSP Architecture for Laravel

  Sources   Download

MIT

The Requires

 

by CalsRanna

21/04 2017

v0.1-rc.6

0.1.0.0-RC6

RSP Architecture for Laravel

  Sources   Download

MIT

The Requires

 

by CalsRanna

20/04 2017

v0.1-rc.5

0.1.0.0-RC5

RSP Architecture for Laravel

  Sources   Download

MIT

The Requires

 

by CalsRanna

20/04 2017

v0.1-rc.4

0.1.0.0-RC4

RSP Architecture for Laravel

  Sources   Download

MIT

The Requires

 

by CalsRanna

20/04 2017

v0.1-rc.3

0.1.0.0-RC3

RSP Architecture for Laravel

  Sources   Download

MIT

The Requires

 

by CalsRanna

20/04 2017

v0.1-rc.2

0.1.0.0-RC2

RSP Architecture for Laravel

  Sources   Download

MIT

The Requires

 

by CalsRanna

08/03 2017

v0.1-rc

0.1.0.0-RC

RSP Architecture for Laravel

  Sources   Download

MIT

by CalsRanna