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

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

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

yuriitatur/repository-laravel

Composer 安装命令:

composer require yuriitatur/repository-laravel

包简介

Laravel repository drivers and more

README 文档

README

A Laravel integration package for the yuriitatur/repository repository pattern library. Provides Eloquent and raw-table drivers, fluent repository builders, transaction middleware, and Laravel event bridging.

Installation

composer require yuriitatur/repository-laravel

Register the service provider (auto-discovered in Laravel 5.5+):

// config/app.php
'providers' => [
    YuriiTatur\Repository\Laravel\Providers\RepositoryServiceProvider::class,
],

Building a Repository

Eloquent driver

use YuriiTatur\Repository\Laravel\Services\EloquentRepositoryBuilder;

$repository = EloquentRepositoryBuilder::create(UserModel::class)
    ->forEntity(UserEntity::class)   // required unless the model itself is the entity
    ->build();

The builder auto-selects a hydration strategy based on what the model implements (see Hydration strategies).

Raw-table driver

use YuriiTatur\Repository\Laravel\Services\TableRepositoryBuilder;

$repository = TableRepositoryBuilder::create('users')
    ->onConnection('pgsql')          // optional, defaults to the default connection
    ->withPrimaryKey('uuid')         // optional, auto-detected from schema if omitted
    ->forEntity(UserEntity::class)
    ->build();

Custom column matcher or hydrator

Both builders accept overrides:

EloquentRepositoryBuilder::create(UserModel::class)
    ->withColumnMatcher(MyCustomMatcher::class)
    ->withHydrator(MyCustomHydrator::class)
    ->build();

Hydration Strategies

StrategyRequirementHow it works
ModelAsEntityHydratorModel implements EntityModel is used directly as the entity — no conversion
ModelEntityHydratorModel implements HydratorModelInterfaceModel provides toEntity() / fromEntity(Entity $e)
EloquentEntityHydrator->forEntity(MyEntity::class) passed to builderJMS Serializer maps model attributes to the entity class

HydratorModelInterface contract:

interface HydratorModelInterface
{
    public function toEntity(): Entity;
    public function fromEntity(Entity $entity): void;
}

Transaction Middleware

The service provider registers a transaction middleware alias that wraps the entire request in a database transaction:

// routes/api.php
Route::middleware('transaction')->group(function () {
    Route::post('/orders', OrderController::class);
});

It can also be applied per-route or in a controller constructor.

Events

Repository operations dispatch the following events through Laravel's Event facade (bridged from the Symfony EventDispatcher used internally):

EventFired when
EntitySavedEventAn entity is persisted
EntityDeletedEventAn entity is deleted
EntityHydratedEventA single entity is hydrated from a DB record
CollectionHydratedEventA collection of entities is hydrated
RunningQueryEventA query is about to execute

Listen to them via standard Laravel listeners:

Event::listen(EntitySavedEvent::class, function (EntitySavedEvent $event) {
    // ...
});

Testing

composer test

License

MIT — see LICENSE.

统计信息

  • 总下载量: 31
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固