定制 manchenkoff/laravel-repositories 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

manchenkoff/laravel-repositories

最新稳定版本:v0.0.9

Composer 安装命令:

composer require manchenkoff/laravel-repositories

包简介

Repository pattern implementation for your Laravel application

README 文档

README

Packagist Version Packagist Downloads Packagist License Packagist Dependency Version

Package provides a basic implementation of Repository pattern with artisan command to generate classes.

Features:

  • Repository class with basic methods like all, find, create, update, delete
  • Generic type comments to pass PHPStan checks
  • Artisan make:repository command to generate repository class with model and interface

Installation

To install this package, you need to install Composer first, and then run:

composer require manchenkoff/laravel-repositories

or add this line to composer.json:

"manchenkoff/laravel-repositories": "*"

and run composer update in the terminal.

Package should automatically register its service provider in your application, but you can do it manually in config/app.php:

'providers' => ServiceProvider::defaultProviders()
    ->merge([
        // Package Service Providers
        \Manchenkoff\Laravel\Repositories\ServiceProvider::class,

        // Application Service Providers
        // ...
    ])
    ->toArray(),

Usage

First of all, you need to create a model class for your repository. You can do it manually or use artisan command:

php artisan make:model Post

Then you can create a repository class for your model:

# repository name - PostRepository
# model name - Post
php artisan make:repository PostRepository Post

This command will create a repository class in app/Repositories directory and PostRepositoryInterface contract class in app/Contracts/Repositories.

Now you can use existing methods in your services or extend with custom functionality:

<?php

namespace App\Services;

use Illuminate\Database\Eloquent\Collection;
use App\Contracts\Repositories\PostRepositoryInterface;
use App\Contracts\Services\PostServiceInterface;

final class PostService implements PostServiceInterface
{
    private readonly PostRepositoryInterface $repository;

    public function __construct(PostRepositoryInterface $repository)
    {
        $this->repository = $repository;
    }

    public function getAllPosts(): Collection
    {
        return $this->repository->all();
    }
}

Implementation

All repository methods use protected query() method to get Eloquent\Builder instance. You can override this method in your repository class to add custom logic, e.g. when you always need some relations to be loaded or custom sorting applied.

protected function query(): Builder
{
    return parent::query()->with('comments')->orderBy('created_at', 'desc');
}

Here is a list of available methods with a quick description:

Method Description
paginated(): LengthAwarePaginator returns paginated collection
all(): Collection returns all entities
find(mixed $id): ?Model returns entity by id or null
get(mixed $id): Model returns entity by id or throws ModelNotFoundException
create(array $data): Model creates new entity with given data
update(Model $entity, array $data): Model updates existing entity with given data
updateMany(array $ids, array $data): void updates many entities with given data by ids
delete(Model $entity): Model deletes existing entity
deleteMany(array $ids): void deletes many entities by ids

Also you might want to adjust class template for your needs. You can do it by publishing package views:

php artisan vendor:publish --provider="Manchenkoff\Laravel\Repositories\ServiceProvider"

Configuration

By default, repositories are created in app/Repositories and interfaces in app/Contracts/Repositories. You can customize these paths by publishing and modifying the config file:

php artisan vendor:publish --provider="Manchenkoff\Laravel\Repositories\ServiceProvider"

Then edit config/repositories.php:

return [
    'paths' => [
        'repositories' => 'Domain/Users/Repositories',
        'interfaces' => 'Domain/Users/Contracts',
    ],
];

Now running php artisan make:repository PostRepository Post will create:

  • app/Domain/Users/Repositories/PostRepository.php
  • app/Domain/Users/Contracts/PostRepositoryInterface.php

Development

This package is completely open-source, so any contributions are welcome!

Clone this repository to your local machine, install dependencies and run tests:

git clone https://github.com/manchenkoff/laravel-repositories
cd laravel-repositories
composer install
composer test

There are some useful composer scripts:

Script Description
composer fmt Apply Laravel Pint code style rules
composer test Run tests with Testbench package
composer lint Run PHP Stan analysis against package codebase
composer rector Run Rector analysis against package codebase
composer rector:fix Apply available Rector suggestions

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固