noitran / micro-repositories by noitran

Repositories package for laravel / lumen projects
1
0
1
Package Data
Maintainer Username: noitran
Maintainer Contact: noitran.black@gmail.com (Noitran.Black)
Package Create Date: 2019-02-07
Package Last Update: 2020-05-30
Language: PHP
License: LGPL-3.0
Last Refreshed: 2024-11-15 15:10:03
Package Statistics
Total Downloads: 1
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 0
Total Watchers: 1
Total Forks: 0
Total Open Issues: 0

MicroRepositories

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