2017 © Pedro Peláez
 

library repository

Flexible Laravel 5 repository package.

image

eilander/repository

Flexible Laravel 5 repository package.

  • Friday, February 9, 2018
  • by markeilander
  • Repository
  • 0 Watchers
  • 0 Stars
  • 113 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 14 Versions
  • 3 % Grown

The README.md

Laravel 5 Repository

An laravel implementation of the Repository Pattern, (*1)

The repository mediates between the data source layer and the business layers of the application. It queries the data source for the data, maps the data from the data source to a business entity, and persists changes in the business entity to the data source. A repository separates the business logic from the interactions with the underlying data source or Web service.

Further reading: http://ryantablada.com/post/two-design-patterns-that-will-make-your-applications-better, (*2)

Table of contents

[TOC], (*3)

Usage

Step 1: Add the Service Provider

In your config/app.php add Eilander\Repository\Providers\RepositoryServiceProvider:class to the end of the providers array:, (*4)

<?php
'providers' => [
    ...
    Eilander\Repository\Providers\RepositoryServiceProvider::class,
],

Step 2: Add package to composer.json for autoloading

Add the package to the main composer.json for autoloading and run composer dump-autoload, like so:, (*5)

<?php
   "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/",
            "Eilander\\Repository\\": "../library/eilander/repository/src/"
        }
    },
#!json

composer dump-autoload

Step 3: Bind interface to implementation in ApiServiceProvider

<?php
    public function register()
    {
        // Bind FruitRepository interface to Elasticsearch\FruitRepository implementation
        $this->app->bind('Modules\Api\Repositories\FruitRepository', 'Modules\Api\Repositories\Elasticsearch\FruitRepository');
        $this->app->bind('Modules\Api\Repositories\FruitRepository', 'Modules\Api\Repositories\Eloquent\FruitRepository');
    }

Elasticsearch

Methods

Search in Elasticsearch Repository, (*6)

$posts = $this->repository->query($selection = '');

When u make use of the ElasticsearchGateway filters will be automagically parsed from the url in this format:, (*7)

http://www.url.nl?filter={"provider":["kpn","vodafone"],"betweenDate":["2015-01-01","2015-12-31"]}

betweenDate

BetweenDate is a special function that makes using datehistograms in Elasticseaerch a breeze. It expects a start and end date and parses this to a fully functional datehistogram., (*8)

Eloquent

Methods

Find all results in Repository, (*9)

$posts = $this->repository->all();

Find all results in Repository with pagination, (*10)

$posts = $this->repository->paginate($limit = null, $columns = ['*']);

Find by result by id, (*11)

$post = $this->repository->find($id);

Loading the Model relationships, (*12)

$post = $this->repository->with(['state'])->find($id);

Find by result by field name, (*13)

$posts = $this->repository->findByField('country_id','15');

Find by result by multiple fields, (*14)

$posts = $this->repository->where([
    //Default Condition =
    'state_id'=>'10',
    'country_id'=>'15',
    //Custom Condition
    ['columnName','>','10']
]);

Create new entry in Repository, (*15)

$post = $this->repository->create( Input::all() );

Update entry in Repository, (*16)

$post = $this->repository->update( Input::all(), $id );

Delete entry in Repository, (*17)

$this->repository->delete($id)

Using the Filter

The repository code is smart enough to perform filtering and searchin from parameters sent in the request., (*18)

You can perform a dynamic search, filter the data and customize the queries., (*19)

Enabling in your Repository

<?php

namespace App\Repositories\Eloquent;

use Eilander\Repository\Eloquent\BaseRepository;
use Eilander\Repository\Contracts\CacheableInterface;
use App\Repositories\FruitRepository as Repository;
use App\Presenters\FruitPresenter;
use App\Entities\Fruit;

/**
 * Class FruitRepository
 */
class FruitRepository extends BaseRepository implements Repository
{
     /**
     * @var array
     */
    protected $fieldSearchable = [
        'name',
        'email'
    ];
    ...
}

You can set the type of condition which will be used to perform the query, the default condition is "=", (*20)

protected $fieldSearchable = [
    'name'=>'like',
    'email', // Default Condition "="
    'your_field'=>'condition'
];

Examples

Request all data without filter by request, (*21)

http://stash.directsurvey.nl/api/v1/users, (*22)

[
    {
        "id": 1,
        "name": "John Doe",
        "email": "john@gmail.com",
        "created_at": "-0001-11-30 00:00:00",
        "updated_at": "-0001-11-30 00:00:00"
    },
    {
        "id": 2,
        "name": "Lorem Ipsum",
        "email": "lorem@ipsum.com",
        "created_at": "-0001-11-30 00:00:00",
        "updated_at": "-0001-11-30 00:00:00"
    },
    {
        "id": 3,
        "name": "Laravel",
        "email": "laravel@gmail.com",
        "created_at": "-0001-11-30 00:00:00",
        "updated_at": "-0001-11-30 00:00:00"
    }
]

Conducting research in the repository, (*23)

http://stash.directsurvey.nl/api/v1/users?search=John%20Doe, (*24)

OR, (*25)

http://stash.directsurvey.nl/api/v1/users?search=John&searchFields=name:like, (*26)

OR, (*27)

http://stash.directsurvey.nl/api/v1/users?search=john@gmail.com&searchFields=email:=, (*28)

OR, (*29)

http://stash.directsurvey.nl/api/v1/users?search=name:John Doe;email:john@gmail.com, (*30)

OR, (*31)

http://stash.directsurvey.nl/api/v1/users?search=name:John;email:john@gmail.com&searchFields=name:like;email:=, (*32)

[
    {
        "id": 1,
        "name": "John Doe",
        "email": "john@gmail.com",
        "created_at": "-0001-11-30 00:00:00",
        "updated_at": "-0001-11-30 00:00:00"
    }
]

Filtering fields http://stash.directsurvey.nl/api/v1/users?filter=id;name, (*33)

[
    {
        "id": 1,
        "name": "John Doe"
    },
    {
        "id": 2,
        "name": "Lorem Ipsum"
    },
    {
        "id": 3,
        "name": "Laravel"
    }
]

Sorting the results http://stash.directsurvey.nl/api/v1/users?filter=id;name&orderBy=id&sortedBy=desc, (*34)

[
    {
        "id": 3,
        "name": "Laravel"
    },
    {
        "id": 2,
        "name": "Lorem Ipsum"
    },
    {
        "id": 1,
        "name": "John Doe"
    }
]

Relationships, (*35)

http://stash.directsurvey.nl/api/v1/users?include=groups, (*36)

Cache

Add a fully automated cache layer to your repository, (*37)

Cache Usage

Implements the interface CacheableInterface and use CacheableRepository Trait., (*38)

<?php

namespace App\Repositories\Eloquent;

use Eilander\Repository\Eloquent\BaseRepository;
use Eilander\Repository\Contracts\CacheableInterface;
use Eilander\Repository\Traits\CacheableRepository;
use App\Repositories\FruitRepository as Repository;
use App\Entities\Fruit;

/**
 * Class FruitRepository
 */
class FruitRepository extends BaseRepository implements Repository, CacheableInterface
{
    use CacheableRepository;

    ...
}

The repository cache will be cleared whenever an item is created, added or deleted., (*39)

Cache Config

You can change cache settings in the config file config/repository.php or directly on your repository., (*40)

config/repository.php, (*41)

    /*
    |--------------------------------------------------------------------------
    | Cache Config
    |--------------------------------------------------------------------------
    |
    */
    'cache'=>[
        /*
         |--------------------------------------------------------------------------
         | Cache Status
         |--------------------------------------------------------------------------
         |
         | Enable or disable cache
         |
         */
        'enabled'   => true,

        /*
         |--------------------------------------------------------------------------
         | Cache Minutes
         |--------------------------------------------------------------------------
         |
         | Time of expiration cache
         |
         */
        'minutes'   => 30,

        /*
         |--------------------------------------------------------------------------
         | Cache Store Path
         |--------------------------------------------------------------------------
         |
         | Path to store cache keys in..
         |
         */
        'store'   => [
            'path' => storage_path('eilander/repository/'),
            'file' => 'cached-keys.json'
        ],

        /*
        |--------------------------------------------------------------------------
        | Methods Allowed
        |--------------------------------------------------------------------------
        |
        | methods cacheable : all, paginate, find, findByField, findWhere
        |
        | Ex:
        |
        | 'only'  =>['all','paginate'],
        |
        | or
        |
        | 'except'  =>['find'],
        */
         'allowed'=>[
             'only'  =>null,
             'except'=>null
         ],

        /*
          |--------------------------------------------------------------------------
          | Cache Clean Listener
          |--------------------------------------------------------------------------
          |
          |
          |
          */
        'clean'     => [

            /*
              |--------------------------------------------------------------------------
              | Enable clear cache on repository changes
              |--------------------------------------------------------------------------
              |
              */
            'enabled' => true,

            /*
              |--------------------------------------------------------------------------
              | Actions in Repository
              |--------------------------------------------------------------------------
              |
              | create : Clear Cache on create Entry in repository
              | update : Clear Cache on update Entry in repository
              | delete : Clear Cache on delete Entry in repository
              |
              */
            'on' => [
                'create'=>true,
                'update'=>true,
                'delete'=>true,
            ]
        ]
    ]

Directly in repository, (*42)

<?php
namespace App\Repositories\Eloquent;

use Eilander\Repository\Eloquent\BaseRepository;
use Eilander\Repository\Contracts\CacheableInterface;
use Eilander\Repository\Traits\CacheableRepository;
use App\Repositories\FruitRepository as Repository;
use App\Entities\Fruit;

/**
 * Class FruitRepository
 */
class FruitRepository extends BaseRepository implements Repository, CacheableInterface
{
    // Setting the lifetime of the cache to a repository specifically
    protected $cacheMinutes = 90;

    protected $cacheOnly = ['all', ...];
    //or
    protected $cacheExcept = ['find', ...];

    use CacheableRepository;

    ...
}

The cacheable methods are: all, paginate, find, findByField, findWhere. Lifetime can also been set using: minutes, hours, days prior to one of the select methods in the controller., (*43)

<?php
namespace App\Http\Controllers;

use Eilander\Repository\Listeners\EloquentClearCache;
use App\Http\Controllers\Controller;
use App\Http\Requests\FruitRequest;
use App\Repositories\FruitRepository;

class FruitController extends Controller {

    /**
     * @var FruitRepository
     */
    protected $repository;

    public function __construct(FruitRepository $repository){
        $this->repository = $repository;
    }
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        // cache for 15 minutes
        $fruits = $this->repository->minutes(15)->paginate(50);
        or
        // cache for 6 hours
        $fruits = $this->repository->hours(6)->paginate(50);
        or
        // cache for 4 days
        $fruits = $this->repository->days(4)->paginate(50);


        return view('fruits.index', compact('fruits'));
    }

The Versions

09/02 2018

5.5.x-dev

5.5.9999999.9999999-dev

Flexible Laravel 5 repository package.

  Sources   Download

MIT

The Requires

 

by Mark Eilander

laravel repository laravel5 fractal

09/02 2018

5.6.x-dev

5.6.9999999.9999999-dev

Flexible Laravel 5 repository package.

  Sources   Download

MIT

The Requires

 

by Mark Eilander

laravel repository laravel5 fractal

09/02 2018

dev-master

9999999-dev

Flexible Laravel 5 repository package.

  Sources   Download

MIT

The Requires

 

by Mark Eilander

laravel repository laravel5 fractal

09/02 2018

v5.6.0

5.6.0.0

Flexible Laravel 5 repository package.

  Sources   Download

MIT

The Requires

 

by Mark Eilander

laravel repository laravel5 fractal

04/09 2017

v5.5.0

5.5.0.0

Flexible Laravel 5 repository package.

  Sources   Download

MIT

The Requires

 

by Mark Eilander

laravel repository laravel5 fractal

31/01 2017

v5.4.0

5.4.0.0

Flexible Laravel 5 repository package.

  Sources   Download

MIT

The Requires

 

by Mark Eilander

laravel repository laravel5 fractal

30/12 2016

v1.0.7

1.0.7.0

Flexible Laravel 5 repository package.

  Sources   Download

MIT

The Requires

 

by Mark Eilander

laravel repository laravel5 fractal

30/12 2016

v1.0.6

1.0.6.0

Flexible Laravel 5 repository package.

  Sources   Download

MIT

The Requires

 

by Mark Eilander

laravel repository laravel5 fractal

10/12 2016

v1.0.5

1.0.5.0

Flexible Laravel 5 repository package.

  Sources   Download

MIT

The Requires

 

by Mark Eilander

laravel repository laravel5 fractal

17/11 2016

v1.0.4

1.0.4.0

Flexible Laravel 5 repository package.

  Sources   Download

MIT

The Requires

 

by Mark Eilander

laravel repository laravel5 fractal

29/08 2016

v1.0.3

1.0.3.0

Flexible Laravel 5 repository package.

  Sources   Download

MIT

The Requires

 

by Mark Eilander

laravel repository laravel5 fractal

22/03 2016

v1.0.2

1.0.2.0

Flexible Laravel 5 repository package.

  Sources   Download

MIT

The Requires

 

by Mark Eilander

laravel repository laravel5 fractal

22/03 2016

v1.0.1

1.0.1.0

Flexible Laravel 5 repository package.

  Sources   Download

MIT

The Requires

 

by Mark Eilander

laravel repository laravel5 fractal

16/02 2016

v1.0.0

1.0.0.0

Flexible Laravel 5 repository package.

  Sources   Download

MIT

The Requires

 

by Mark Eilander

laravel repository laravel5 fractal