noitran/micro-repositories

Repositories package for laravel / lumen projects

v0.1.0 2019-02-13 20:32 UTC

This package is auto-updated.

Last update: 2024-11-29 05:45:00 UTC


README

Coverage Status Quality Score Software License Latest Version Total Downloads

About

  • Package adds support to work with Repository Design pattern in laravel and lumen applications. Package was created from scratch as other versions of repository pattern packages were unmaintained or poor quality. Contributions are welcome!

Install

  • Install as composer package
$ composer require noitran/micro-repositories

Laravel

  • Laravel uses provider auto discovery. Config file can be published using command
$ artisan vendor:publish --provider="Noitran\Repositories\ServiceProvider"

Lumen

  • Open your bootstrap/app.php and register as service provider
$app->register(Noitran\Repositories\ServiceProvider::class);
  • Config file should be loaded manually in bootstrap/app.php
$app->configure('repositories');

Repositories

Creating Eloquent model

  • Create Eloquent model like you do it initially. Using laravel generators or manually. Example model what I will use:
<?php

namespace App\Data\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class User extends Model
{
    /**
     * The attributes that aren't mass assignable.
     *
     * @var array
     */
    protected $guarded = [
        'id',
    ];
}

Creating Eloquent Repository implementation and Repository Interface

  • First let's create Interface of our Repository
<?php

namespace App\Data\Repositories\User;

use Noitran\Repositories\Contracts\Repository\RepositoryInterface;

/**
 * Interface UserRepository
 */
interface UserRepository extends RepositoryInterface
{
    //
}
  • Second step will be to create implementation of Repository Interface. We will extend SqlRepository which adds support for querying with table names. If you use jenssegers/laravel-mongodb package, then your repository implementation should extend MongoRepository class
<?php

namespace App\Data\Repositories\User;

use Noitran\Repositories\Repositories\SqlRepository;
use App\Data\Models\User;

/**
 * Class UserRepositoryEloquent
 */
class UserRepositoryEloquent extends SqlRepository implements UserRepository
{
    /**
     * Specify Model class name.
     *
     * @return string
     */
    public function getModelClassName(): string
    {
        return User::class;
    }

    /**
     * Boot up the repository, pushing criteria.
     */
    public function boot(): void
    {
        //
    }
}
  • After Repository and Interface were created, they should be loaded using laravel IoC binding. Fastest way to do it is to add binding into App\Providers\AppServiceProvider class.
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Data\Repositories\User\UserRepository;
use App\Data\Repositories\User\UserRepositoryEloquent;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register(): void
    {
        $this->app->bind(UserRepository::class, UserRepositoryEloquent::class);
    }
}

Filters

  • Filters are sitting on top of Repositories and uses Criteria to enable query filtering abilities with built in and / or custom Criteria.

  • Creating filter class is easy. Example:

<?php

namespace App\Data\Filters\User;

use Noitran\Repositories\Filters\AbstractFilter;
use App\Data\Repositories\User\UserRepository;

class UserFilter extends AbstractFilter
{
    /**
     * @var array
     */
    protected $queryFilters = [
        // List of custom Criteria filters 
    ];

    /**
     * UserFilter constructor.
     *
     * @param UserRepository $userRepository
     */
    public function __construct(UserRepository $userRepository)
    {
        parent::__construct();

        $this->setRepository($userRepository)
            ->setQueryFilters($this->queryFilters);
    }

    /**
     * @param array $requestAttributes
     *
     * @return mixed
     */
    public function filter(array $requestAttributes)
    {
        $input = $this->getInput(
            $this->queryFilters,
            $requestAttributes
        );

        $this->repository = $this->pushFilters($this->repository, $input);

        return $this;
    }
}
  • After creating UserFilter you are now able to use built in Filtering Criteria and add your custom. Filters acts as layer between HTTP Requests and Repositories giving you ability to use RQL (Request Query Language), ordering, sorting and pagination capabilities out of the box.

Criteria

@todo