wimski/laravel-model-repositories

Laravel model repositories

v2.4.0 2022-06-03 12:53 UTC

This package is auto-updated.

Last update: 2022-06-03 12:55:11 UTC


README

Latest Stable Version Coverage Status PHPUnit PHPStan

Laravel Model Repositories

Generic repository pattern for Laravel Eloquent models.

Changelog

View the changelog.

Usage

Install package

composer require wimski/laravel-model-repositories

Setup service provider

This package is NOT loaded using package discovery. You should create your own service provider that extends the one from this package and add it to your config manually.

app/Providers/RepositoryServiceProvider.php

<?php

namespace App\Providers;

use Wimski\ModelRepositories\Providers\ModelRepositoryServiceProvider;

class RepositoryServiceProvider extends ModelRepositoryServiceProvider
{
    protected array $repositories = [
        // add your repository bindings here
    ];
}

config/app.php

<?php

return [
    'providers' => [
        /*
         * Application Service Providers...
         */
         App\Providers\RepositoryServiceProvider::class,
    ],
];

Generate a repository

php artisan make:repository App\\Models\\MyModel

This will create the following files:

  • MyModelRepositoryInterface.php
  • MyModelRepository.php

The namespace and file location depend on the namespace configuration.

Alternatively you can completely override the contract and repository FQN by using the command options. The namespace matching and class name suffixing will be skipped.

php artisan make:repository App\\Models\\MyModel --contract=Foo\\Bar --repository=Lorem\\Ipsum

Add repository binding

Set up the binding of your new repository in the service provider.

protected array $repositories = [
    MyModelRespositoryInterface::class => MyModelRespository::class,
];

Example usage

class SomeService
{
    protected MyModelRespositoryInterface $repository;
    
    public function __construct(MyModelRespositoryInterface $repository)
    {
        $this->repository = $repository;    
    }
    
    public function doSomething($id): void
    {
        $myModel = $this->repository->findOrFail($id);
    }
}

Namespace configuration

The namespaces configuration is used to determine what the namespaces of your repository classes - and locations of the subsequent files - should be. Each configuration exists of three parts:

  • models
  • contracts
  • repositories

When generating a repository for a model the command will look for a namespace configuration which has a models part that matches the supplied model. The contracts and repositories parts of that configuration are then used as the namespaces for the repository classes.

Assumptions

  • The namespaces follow the PSR-4 file location convention.
  • The namespaces are within the Laravel app directory.

Default configuration

'namespaces' => [
    [
        'models'       => 'App\\Models',
        'contracts'    => 'App\\Contracts\\Repositories',
        'repositories' => 'App\\Repositories',
    ],
],

When generating a repository for App\Models\MyModel, the following two classes will be created:

  • App\Contracts\Repositories\MyModelRepositoryInterface
  • App\Repositories\MyModelRepository

Multiple configurations

You can have multiple namespace configurations, for example when using domains.

'namespaces' => [
    [
        'models'       => 'App\\DomainA\\Models',
        'contracts'    => 'App\\DomainA\\Contracts\\Repositories',
        'repositories' => 'App\\DomainA\\Repositories',
    ],
    [
        'models'       => 'App\\DomainB\\Models',
        'contracts'    => 'App\\DomainB\\Contracts\\Repositories',
        'repositories' => 'App\\DomainB\\Repositories',
    ],
],

Specificity

The first match will be used so if you have overlapping namespace configurations, make sure to have the more specific ones on top.

'namespaces' => [
    [
        'models' => 'App\\Models\\SpecificModels',
        ...
    ],
    [
        'models' => 'App\\Models',
        ...
    ],
],

Available methods

See interface.

Stub customization

See Laravel's documentation about stub customization. This package adds the following stub files:

  • model.repository.interface.stub
  • model.repository.stub

PHPUnit

composer test

PHPStan

composer analyze

Credits

License

The MIT License (MIT). Please see License File for more information.