guardian360/repository 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

guardian360/repository

Composer 安装命令:

composer require guardian360/repository

包简介

Repository pattern used to abstract the database layer.

README 文档

README

Build Status Coverage Status

A base repository for Eloquent models.

Requirements

  • PHP >=7.0.0
  • PHP MongoDB driver (optional)

Installation

Install via composer.

$ composer require guardian360/repository

Usage

First, you should create your repository class. You can do this manually or by using Artisan by doing $ php artisan make:repository UserRepository. Your repository must extend \Guardian360\Repository\AbstractRepository and implement the model() method.

<?php

namespace App\Repositories;

use App\User;
use Guardian360\Repository\AbstractRepository as Repository;

class UserRepository extends Repository
{
    /**
     * Specify the model's class name.
     *
     * @return string
     */
    public function model()
    {
        return User::class;
    }
}

By implementing the model() method, you are telling the repository what model class you want to use. Next, in our case, it seems only logical to have a User model. Why don't we create one?

<?php

namespace App;

use Illuminate\Database\Eloquent\Model

class User extends Model
{
    //
}

Finally, you may use the repository in your controller, or anywhere else really.

<?php

namespace App\Http\Controllers;

use App\Repositories\UserRepository;

class UserController extends Controller
{
    /**
     * @var \App\Repositories\UserRepository
     */
    protected $users;

    /**
     * @return void
     */
    public function __construct(UserRepository $users)
    {
        $this->users = $users;
    }

    /**
     * Display a listing of the users.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return response()->json($this->users->all());
    }
}

Examples

Fetch a list of all users.

$this->users->all();

Find a user.

$this->users->find(1);

Find a user by an attribute.

$this->users->findBy('name', 'Joe');

Find all users by an attribute.

$this->users->findAllBy('role', 'admin');
$this->users->findAllBy('role', ['admin', 'guest']);

Create a new user.

$this->users->create($request->all());

Update an existing user.

$this->users->update($user, $request->all());
$this->users->update(1, $request->all());

Delete an existing user.

$this->users->delete($user);
$this->users->delete(1);
$this->users->delete([1, 2]);

Specifications

Specifications allow you to apply very specific conditions to the repository's query. You may use Artisan to generate a Specification class for you, using $ php artisan make:specification UserIsAdmin, or you may do so manually. Your specification must satisfy the \Guardian360\Repository\Contracts\SpecificationContract contract.

<?php

namespace App\Specifications;

use Guardian360\Repository\Contracts\SpecificationContract as Specification

class UserIsAdmin implements Specification
{
    /**
     * Apply the specification.
     *
     * @param  mixed  $query
     * @return mixed
     */
    public function apply($query)
    {
        return $query->where('role', 'admin');
    }
}

Then, you may push the Specification to the repository in your controller. You may use as many Specifications as you like.

<?php

namespace App\Http\Controllers;

use App\Specifications\UserIsAdmin;
use App\Repositories\UserRepository;

class UserController extends Controller
{
    /**
     * @var \App\Repositories\UserRepository
     */
    protected $users;

    /**
     * @return void
     */
    public function __construct(UserRepository $user)
    {
        $this->users = $users;
    }

    /**
     * Display a listing of the admins.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $admins = $this->users->pushSpec(new UserIsAdmin)->all();

        return response()->json($admins);
    }
}

Credits

This package is inspired by the following awesome packages:

Thanks guys, I could not have done this without you. :)

guardian360/repository 适用场景与选型建议

guardian360/repository 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.21k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 10 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-10-17