noitran / micro-repositories
Repositories package for laravel / lumen projects
v0.1.0
2019-02-13 20:32 UTC
Requires
- php: >=7.1
- illuminate/support: ~5.7
Requires (Dev)
- orchestra/testbench: ~3.4.2|~3.5.0|~3.6.0|~3.7.0
- phpunit/phpunit: ~7.0
This package is auto-updated.
Last update: 2024-10-29 05:28:37 UTC
README
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 usejenssegers/laravel-mongodb
package, then your repository implementation should extendMongoRepository
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