lordfm/raw 问题修复 & 功能扩展

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

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

lordfm/raw

Composer 安装命令:

composer require lordfm/raw

包简介

Repository pattern boilerplate based on Annotated Models

README 文档

README

Raw is a quick repository pattern scaffolding package for laravel. It mixes Doctrine Annotations with Eloquent ORM to handle the structure of your project from the models.

Do you have something in mind? You should be able to build it from the very bottom without suffer.

Install

composer require lordfm/raw

Add the provider to your app.php

'providers' = [
	/*
	* Lots of providers
	*
	*/
	LoRDFM\Raw\RawServiceProvider::class
]

Publish the raw.php configuiration file

php artisan vendor:publish --provider="LoRDFM\Raw\RawServiceProvider"

Annotate your model

Declare the use of LoRDFM\Raw\Annotations\Rawable and add the annotation on top of your model. Be sure to not leave blank lines between the annotation and the class.

MyModel.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

use LoRDFM\Raw\Annotations\Rawable;

/**
*
* @Rawable
*/
class MyModel extends Model
{

}

Now, just run

php artisan raw:repository

What happened?. We just built our typical --resource Controller along with a Contract interface to declare all crud related methods and a Repository class to implement them.

Don't forget to bind your repositories in your Providers\AppServiceProvider.php

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        
        // Other bindings

        $this->app->bind('App\Repositories\Contracts\MyModelContract', 'App\Repositories\MyModelRepository');
         
    }

By default they will be stored in app\Http\Controllers , app\Repositories and app\Repositories\Contracts .

As you may be expecting, y can create complex models with relationships. We support the nex relationships

Realation Annotation
one to many @HasMany
many to one @BelongsTo
has one @HasOne

Annotations

The annotations are pretty straight forward, ech one handles the relation of the model to another model and the declaration of the files holding the repository pattern.

@Rawable

Each model you want to be handled by the package should have this annotation.

@HasMany

An array of models with related as One to Many from the current model

@HasMany(models = {"Articles", "Books", ...})

@HasOne

An array of models with related as One to One from the current model

@HasOne(models = {"Profile", ...})

@BelongsTo

An array of models with related as Many to One from the current model

@BelongsTo(models = {"Author", "Publisher", ...})

@RawableContract

Handles the cration of the interface class (aka 'contract') with the methods your repository and controller should implement. This is the core of the customization of the repository @RawableController and @RawableRepository requires the namespace of this annotation as argument Supports path and a namespace to store and declare the interface

@RawableContract(path = "app\Repository\Contracts", namespace = "App\Repository\Contracts")

@RawableController

Handles the creation of the controllers. Supports path, namespace and contract to store and declare the --resource style controller for your model.

* @RawableController(path = "app\Http\Controllers\Api", namespace = "App\Http\Controllers\Api", contract = "App\Repository\Contracts")

@RawableRepository

Handles the repository class creation. Supports path, namespace and contract to store and delcare the class

@RawableRepository(path = "app\Repository", namespace = "App\Repository", contract = "App\Repository\Contracts")

Now What?

Long story short, we are going to use a very well known example of related entities to demonstrate the use of the pluging. Classic Author -> Articles example:

Author model

<?php
namespace  App\Models;

use Illuminate\Database\Eloquent\Model;

use LoRDFM\Raw\Annotations\Rawable;
use LoRDFM\Raw\Annotations\HasMany;
use LoRDFM\Raw\Annotations\HasOne;

/**
*
* @Rawable
* @HasMany(models = {"Article"})
* @HasOne(models = {"Profile"})
*/
class  Author  extends  Model
{

    public  function  articles()
    {
	    return  $this->hasMany('App\Models\Article');
    }

    public  function  profile()
    {
	    return  $this->hasOne('App\Models\Profile');
    }
    
}

The Author holds a One to One relation to a Profilemodel nad One to Many to the Article model

Article model

<?php

namespace  App\Models;

use Illuminate\Database\Eloquent\Model;

use LoRDFM\Raw\Annotations\Rawable;
use LoRDFM\Raw\Annotations\BelongsTo;

/**
*
* @Rawable
* @BelongsTo(models = {"Author"})
*/
class  Article  extends  Model
{
    public  function  author()
    {
    return  $this->belongsTo('App\Models\Author');
    }
}

The Article holds Many to One to their author

Profile model

<?php

namespace  App\Models;

use Illuminate\Database\Eloquent\Model;

class  Profile  extends  Model
{
    public  function  author()
    {
    return  $this->belongsTo('App\Models\Author');
    }
}

The Profile model has no @Rawable implementation at all, not required for the moment.

Let's go!

Now, just run

 php artisan raw:repository

and it will tell you on wich model is working and creating its repository

...
Making repository for App\Models\Article
Making repository for App\Models\Author
...

If the repository for the model already exists you will be warned

Making repository for App\Models\Author
This Contract already exists. If you want to overwrite it use '--force'
This Repository already exists. If you want to overwrite it use '--force'
This Controller already exists. If you want to overwrite it use '--force'
This RouteGroup already exists. If you want to overwrite it use '--force'

You can overwrite them using --force

php artisan raw:repository --force

Be careful, the corresponding files will be completely overwritten. You may choose to run the command for just some models

php artisan raw:repository Author
php artisan raw:repository Author Article
php artisan raw:repository Author Article Publisher ...

Custom Structure

You can customize the contract, controller and repository for your model Just import the annotations and define them with your desired path and namespace Remember that RawableController and RawableRepository requires the namespace of their corresponding contract

<?php
namespace  App\Models;

use Illuminate\Database\Eloquent\Model;

use LoRDFM\Raw\Annotations\Rawable;
use LoRDFM\Raw\Annotations\RawableController;
use LoRDFM\Raw\Annotations\RawableContract;
use LoRDFM\Raw\Annotations\RawableRepository;
use LoRDFM\Raw\Annotations\HasMany;
use LoRDFM\Raw\Annotations\HasOne;

/**
*
* @Rawable
* @HasMany(models = {"Article"})
* @HasOne(models = {"Profile"})
*
* @RawableContract(path = "app\Repository\Contracts", namespace = "App\Repository\Contracts")
* @RawableController(path = "app\Http\Controllers", namespace = "App\Http\Controllers", contract = "App\Repository\Contracts")
* @RawableRepository(path = "app\Repository", namespace = "App\Repository", contract = "App\Repository\Contracts")
*/
class  Author  extends  Model
{

    public  function  articles()
    {
	    return  $this->hasMany('App\Models\Article');
    }

    public  function  profile()
    {
	    return  $this->hasOne('App\Models\Profile');
    }
    
}

Multiple Endpoints

You might want multiple endpoints with similar implementation, like a Guest and Api implementation for unauthenticated and authenticated users. Just define multiple groups of RawableContract, RawableController and RawableRepository Here is an example...

<?php

namespace  App\Models;

use Illuminate\Database\Eloquent\Model;

use LoRDFM\Raw\Annotations\Rawable;
use LoRDFM\Raw\Annotations\RawableController;
use LoRDFM\Raw\Annotations\RawableContract;
use LoRDFM\Raw\Annotations\RawableRepository;
use LoRDFM\Raw\Annotations\BelongsTo;

/**
*
* @Rawable
* @BelongsTo(models = {"Author"})
*
* @RawableContract(path = "app\Repository\Api\Contracts", namespace = "App\Repository\Api\Contracts")
* @RawableController(path = "app\Http\Controllers\Api", namespace = "App\Http\Controllers",  contract = "App\Repository\Api\Contracts")
* @RawableRepository(path = "app\Repository\Api", namespace = "App\Repository\Api", contract = "App\Repository\Api\Contracts")
*
* @RawableContract(path = "app\Repository\Guest\Contracts", namespace = "App\Repository\Guest\Contracts")
* @RawableController(path = "app\Http\Controllers\Guest", namespace = "App\Http\Controllers",  contract = "App\Repository\Guest\Contracts")
* @RawableRepository(path = "app\Repository\Guest", namespace = "App\Repository\Guest", contract = "App\Repository\Guest\Contracts")
*
*/
class  Article  extends  Model
{
    public  function  author()
    {
    return  $this->belongsTo('App\Models\Author');
    }
}

Test boilerplate

You can generate some Unit test boilerplate for you repository

 php artisan raw:repository --test

Gotchas

  • Write the comment with no space before the class declaration.
  • in @RawableController and @RawableController the contract argument is the namespace of the corresponding contract, not the final inflected class name
  • Do not forget to bind your repositories in your Providers\AppServiceProvider.php

lordfm/raw 适用场景与选型建议

lordfm/raw 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 247 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 12 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-12-05