Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/czim/laravel-repository
Repository pattern implementation for Laravel
https://github.com/czim/laravel-repository
eloquent laravel laravel-repositories repository repository-pattern
Last synced: 2 days ago
JSON representation
Repository pattern implementation for Laravel
- Host: GitHub
- URL: https://github.com/czim/laravel-repository
- Owner: czim
- License: mit
- Created: 2015-08-29T15:40:08.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-07-30T13:24:17.000Z (5 months ago)
- Last Synced: 2024-12-25T07:04:34.447Z (9 days ago)
- Topics: eloquent, laravel, laravel-repositories, repository, repository-pattern
- Language: PHP
- Homepage:
- Size: 205 KB
- Stars: 51
- Watchers: 3
- Forks: 19
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Laravel Repository
[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]](LICENSE.md)
[![Latest Stable Version](http://img.shields.io/packagist/v/czim/laravel-repository.svg)](https://packagist.org/packages/czim/laravel-repository)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/029ab930-9064-4acf-8602-87f8c010f387/mini.png)](https://insight.sensiolabs.com/projects/029ab930-9064-4acf-8602-87f8c010f387)Repository setup inspired by the Bosnadev/Repository package. This package is an extended, adjusted (but entirely independent) version of that, with its own interfaces.
One major difference to the Bosnadev repository is that this one is able to deal with repeated and varying calls to the same repository instance, without breaking down or undesirable repeated application of Criteria.
You can instantiate a repository once and do anything with it in any order, and both queries and model manipulation methods will keep working.Among the added functionality is the ability to override or 'temporarily' set and remove Criteria, post-processing models after retrieval.
I'm well aware that there is *much* to say against using Repositories like this (and the repository pattern in general), but I find they have their uses.
I prefer using them to make for easier unit testing in large projects.> Note: I recommand against using this package. I'm making some updates for my personal legacy projects,
> but I consider this approach to be a serious antipattern (at least with Eloquent).## Version Compatibility
| Laravel | Package |
|:-----------|:---------|
| 5.1 | 1.0 |
| 5.2 | 1.2 |
| 5.3 | 1.2 |
| 5.4 to 5.8 | 1.4 |
| 6.0 | 2.0 |
| 7.0, 8.0 | 2.1 |
| 9.0 | 3.0, 4.0 |### Warning
Version 4.0 has many breaking changes.
Refer to the [Changelog](CHANGELOG.md) for details.## Install
Via Composer
``` bash
$ composer require czim/laravel-repository
```If you want to use the repository generator through the `make:repository` Artisan command, add the `RepositoryServiceProvider` to your `config/app.php`:
``` php
Czim\Repository\RepositoryServiceProvider::class,
```Publish the repostory configuration file.
``` bash
php artisan vendor:publish --tag="repository"
```## Basic Usage
Simply extend the (abstract) repository class of your choice, either `Czim\Repository\BaseRepository`, `Czim\Repository\ExtendedRepository` or `Czim\Repository\ExtendedPostProcessingRepository`.
The only abstract method that must be provided is the `model` method (this is just like the way Bosnadev's repositories are used).
### Base- and Extended Repositories
Depending on what you require, three different abstract repository classes may be extended:
* `BaseRepository`
Only has the retrieval and simple manipulation methods (`create()`, `update()` and `delete()`), and Criteria handling.
* `ExtendedRepository`
Handles an **active** check for Models, which will by default exclude any model which will not have its `active` attribute set to true (configurable by setting `hasActive` and/or `activeColumn`).
Handles caching, using [dwightwatson/rememberable](https://github.com/dwightwatson/rememberable) by default (but you can use your own Caching Criteria if desired).
Allows you to set Model scopes, for when you want to use an Eloquent model scope to build your query.### Using the repository to retrieve models
Apart from the basic stuff (inspired by Bosnadev), there are some added methods for retrieval:
* `query()`: returns an Eloquent\Builder object reflecting the active criteria, for added flexibility
* `count()`
* `first()`
* `findOrFail()`: just like `find()`, but throws an exception if nothing found
* `firstOrFail()`: just like `first()`, but throws an exception if nothing foundEvery retrieval method takes into account the currently active Criteria (including one-time overrides), see below.
For the `ExtendedPostProcessingRepository` goes that postprocessors affect all models returned, and so are applied in all the retrieval methods (`find()`, `firstOrFail()`, `all()`, `allCallback`, etc).
The `query()` method returns a Builder object and therefore circumvents postprocessing. If you want to manually use the postprocessors, simply call `postProcess()` on any Model or Collection of models.#### Handling Criteria
Just like Bosnadev's repository, Criteria may be pushed onto the repository to build queries.
It is also possible to set default Criteria for the repository by overriding the `defaultCriteria()` method and returning a Collection of Criteria instances.Criteria may be defined or pushed onto the repository by **key**, like so:
``` php
$repository->pushCriteria(new SomeCriteria(), 'KeyForCriteria');
```This allows you to later remove the Criteria by referring to its key:
``` php
// you can remove Criteria by key
$repository->removeCriteria('KeyForCriteria');
```To change the Criteria that are to be used only for one call, there are helper methods that will preserve your currently active Criteria.
If you use any of the following, the active Criteria are applied (insofar they are not removed or overridden), and additional Criteria are applied only for the next retrieval method.``` php
// you can push one-time Criteria
$repository->pushCriteriaOnce(new SomeOtherCriteria());// you can override active criteria once by using its key
$repository->pushCriteriaOnce(new SomeOtherCriteria(), 'KeyForCriteria');// you can remove Criteria *only* for the next retrieval, by key
$repository->removeCriteriaOnce('KeyForCriteria');
```Note that this means that *only* Criteria that have keys can be removed or overridden this way.
A `CriteriaKey` Enum is provided to more easily refer to the standard keys used in the `ExtendedRepository`, such as 'active', 'cache' and 'scope'.## Configuration
No configuration is required to start using the repository. You use it by extending an abstract repository class of your choice.### Extending the classes
Some properties and methods may be extended for tweaking the way things work.
For now there is no documentation about this (I will add some later), but the repository classes contain many comments to help you find your way (mainly check the `ExtendedRepository` class).### Traits
Additionally, there are some traits that may be used to extend the functionality of the repositories, see `Czim\Repository\Traits`:* `FindsModelsByTranslationTrait` (only useful in combination with the [dimsav/laravel-translatable](https://github.com/dimsav/laravel-translatable) package)
* `HandlesEloquentRelationManipulationTrait`
* `HandlesEloquentSavingTrait`
* `HandlesListifyModelsTrait` (only useful in combination with the [lookitsatravis/listify](https://github.com/lookitsatravis/listify) package)I've added these mainly because they may help in using the repository pattern as a means to make unit testing possible without having to mock Eloquent models.
## Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
## Credits
- [Coen Zimmerman][link-author]
- [All Contributors][link-contributors]## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
[ico-version]: https://img.shields.io/packagist/v/czim/laravel-repository.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/czim/laravel-repository.svg?style=flat-square[link-packagist]: https://packagist.org/packages/czim/laravel-repository
[link-downloads]: https://packagist.org/packages/czim/laravel-repository
[link-author]: https://github.com/czim
[link-contributors]: ../../contributors