girolando/repositories-pattern 问题修复 & 功能扩展

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

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

girolando/repositories-pattern

Composer 安装命令:

composer require girolando/repositories-pattern

包简介

Laravel Repositories Pattern Implementation

README 文档

README

This package allows you to implement the Repositories design pattern in your laravel application. It makes easy to use a service layer too.

##Instalation Its simply to install, just run:

composer require andersonef/repositories-pattern

##Service Provider You must register this package service provider at you config/app.php file. Just add this line at your $providers array:

Andersonef\Repositories\Providers\RepositoryProvider::class,

##Creating your Repositories and Services: At your console, enter the following command:

php artisan make:repository BlogNamespace/Post --entity=App/Models/Post

Remember to use the complete namespace to your entity path. If your are using a custom namespace, use:

php artisan make:repository BlogNamespace/Post --entity=CustomNamespace/Models/Post

This will create the following file structure in your app directory:

app/

Repositories/

	BlogNamespace/
	
		PostRepository.php
		
Services/

	BlogNamespace/
	
		PostService.php

##Repository file structure Your repository file will be created with the following code:

namespace Inet\Repositories\BlogNamespace;

use Andersonef\Repositories\Abstracts\RepositoryAbstract;
use \Post;

/**
* Data repository to work with entity Post.
*
* Class PostRepository
* @package Inet\Repositories\BlogNamespace
*/
class PostRepository extends RepositoryAbstract{


public function entity()
{
    return \Post::class;
}

}

##Service file structure And your PostService.php file wil be created with the following code:

namespace Inet\Services\BlogNamespace;

use Andersonef\Repositories\Abstracts\ServiceAbstract;
use Illuminate\Database\DatabaseManager;
use \Inet\Repositories\BlogNamespace\PostRepository;

/**
* Service layer that will applies all application rules to work with Post class.
*
* Class PostService
* @package Inet\Services\BlogNamespace
*/
class PostService extends ServiceAbstract{

  /**
   * This constructor will receive by dependency injection a instance of PostRepository and DatabaseManager.
   *
   * @param PostRepository $repository
   * @param DatabaseManager $db
   */
  public function __construct(PostRepository $repository, DatabaseManager $db)
  {
      parent::__construct($repository, $db);
  }
}

##Usage Using this pattern you wil be able to separate your application rules from your data access rules and you will be able to reuse your code in a very simple way. Imagine that you have an app that users can register using the public pages AND the admin register users using the admin panel. The rules to register an user are the same on both cases, but the admin panel must require an admin logged user AND has an option to isent the new user to pay his subscription value. In the old way you would have to replicate the code or use a laravel command to isolate the user registration rules. Using the service layer and the repository pattern you will write the user registration rules inside the UserService and on both controllers (public page registration and admin panel) you would call the $userService->create($request->all()). If you must implement an api to save users from an android interface, you can reuse your service layer and just change the way your controller respond to the client.

##Repository Inherited Methods: Your repository have some inherited methods from RepositoryAbstract class. They are:

  • create(array $data);: Tries to create a new instance of your specified entity. WARNING: Your entity must declare the $fillable field
  • update(array $data, $id);: Update specified entity.
  • delete($id);: Delete the specified entity
  • find($id, array $columns = ['*']);: Find an instance of specified entity by id
  • findBy(array $fields, array $columns = ['*']);: Find a collection of specified instances using the fields (see phpdocs).

##Service Inherited Methods: Your service will be similar to your repository. The main difference is that your service layer must implement application logic, so it have transaction in its methods:

  • create(array $data): Open a transaction and try to create an instance using the $data array. You can override it
  • update(array $data, $id): Open a transaction and try to update the instance using $data array.
  • delete($id): OPen a transaction and try to delete an instance of specified entity.

##Magic Methods: For convenience, we can use magic methods on both service and repository classes:

$yourservice->repositoryMethod(); // this will be the same as: $yourservice->getRepository()->repositoryMethod();
$yourRepository->entityMethod(); // this will be the same as: $tyourRepository->getEntity()->entityMethod();

##Using Criterias: You can implement criteria to reuse your application query rules. This package brings you one default criteria, the FindUsingLikeCriteria. Lets think you must implement a search field on your blog, and must bring all your posts that have some text like $query variable. You can simply do:

$result = $postService->findByCriteria(new FindUsingLikeCriteria($request->get('textQuery')))->paginate(10);

This will returns to you a collection of posts that has title, or content, or author like the text inside 'textQuery' request attribute. You can create your own criterias, its really simple to do it: ##Creating your own criteria This wil be your custom criteria:

namespace Andersonef\Repositories\Criteria;

use Andersonef\Repositories\Abstracts\CriteriaAbstract;
use Andersonef\Repositories\Contracts\RepositoryContract;
use Illuminate\Database\Eloquent\Model;

class UnreadRecentPostsCriteria extends CriteriaAbstract{

public function apply(Model $model, RepositoryContract $repository)
{
    $model
    ->where('created_at','>',(new \DateTime())->sub(new \DateInterval('P3D'))->format('Y-m-d'))
    ->where('status_read', '=', 1);
    return $model;
}
}

##Credits This package has been created based on this other: https://github.com/prettus/l5-repository I just implement a few more options and make some fews changes to turn it better for the company I work.

girolando/repositories-pattern 适用场景与选型建议

girolando/repositories-pattern 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 231 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 10 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 girolando/repositories-pattern 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2019-10-09