承接 czim/laravel-repository 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

czim/laravel-repository

Composer 安装命令:

composer require czim/laravel-repository

包简介

Repository for Laravel (inspired by and indebted to Bosnadev/Repositories)

README 文档

README

Latest Version on Packagist Software License Latest Stable Version SensioLabsInsight

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
10.0 4.2
11.0 4.3
12.0 4.4

Warning

Version 4.0 has many breaking changes. Refer to the Changelog for details.

Install

Via Composer

$ 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:

Czim\Repository\RepositoryServiceProvider::class,

Publish the repostory configuration file.

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 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 found

Every 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:

    $repository->pushCriteria(new SomeCriteria(), 'KeyForCriteria');

This allows you to later remove the Criteria by referring to its key:

    // 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.

    // 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 package)
  • HandlesEloquentRelationManipulationTrait
  • HandlesEloquentSavingTrait
  • HandlesListifyModelsTrait (only useful in combination with the 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 for details.

Credits

License

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

czim/laravel-repository 适用场景与选型建议

czim/laravel-repository 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 113.22k 次下载、GitHub Stars 达 52, 最近一次更新时间为 2015 年 08 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「database」 「repository」 「laravel」 「repositories」 「eloquent」 「criteria」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 czim/laravel-repository 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 czim/laravel-repository 我们能提供哪些服务?
定制开发 / 二次开发

基于 czim/laravel-repository 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 113.22k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 54
  • 点击次数: 31
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 52
  • Watchers: 2
  • Forks: 21
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-08-29