定制 zoiloreyes/trest 二次开发

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

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

zoiloreyes/trest

Composer 安装命令:

composer require zoiloreyes/trest

包简介

TRest is an ORM that maps REST resources to PHP objects. Designed to use in applications that need to consume data from a RESTful API.

README 文档

README

TRest is an ORM that maps REST resources to PHP objects. Designed to use in applications that need to consume data from a RESTful API.

Usage

There is a sample file name trest_init.php which contains a default configuration which might help you understrand how you can initialize the library before start using it.

<?php

/**
* You should allways define the constant `TREST_DEFAULT_CACHE_TTL`, this constant the default time that items will be     * cached in seconds, if you don't wanna catch your request define it and provide a value of 0/
*/
define('TREST_DEFAULT_CACHE_TTL', 120);

/**
 * The config named 'default' will be used for the models without the property
 * => protected static $configName = 'StackOverflow';
 */
ConfigFactory::add('default', new Config(array(
    'apiUrl' => 'http://pixelpt-sandwich-api.herokuapp.com/',
    'singleItemNode' => 'sandwich',
    /**
    * Here you can provide a cache adapter to your connection.
    *
    * To create your own cache adapters, implement the interface TRest\Cache\CacheAdapterInterface
    * and provide an instance of your class to the configuration key named cacheAdapter of your connection.
    *
    * Here should be passed an instance of a class that implements the interface TRest\Cache\CacheAdapterInterface.
    *
    */
    'cacheAdapter' => new ClassImplementingCacheAdapterInterface()
)));

Introduction

The basic concept is simple, you have a REST service (http://pixelpt-sandwich-api.herokuapp.com/) and you want to interact with it through a simple ActiveRecord style interface.

First we can retrieve a sandwich:

$sandwich = Sandwich::findOne($id); // GET http://pixelpt-sandwich-api.herokuapp.com/sandwich/$id

Now we can change some properties of that sandwich:

$sandwich->name = 'Double bacon cheese';
$sandwich->price = 9000;

Once we're done we can simply save it and the appropriate REST call will be made:

$sandwich->save(); // PUT http://pixelpt-sandwich-api.herokuapp.com/sandwich/$id (title=Double bacon cheese, price=9000)

Model

The REST client is an ActiveRecord style implementation for working with REST services. All you need to do is define some PHP classes that are mapped to some REST service on the web. Here is an example where we map a Sandwich to http://pixelpt-sandwich-api.herokuapp.com/sandwich:

<?php

namespace Entities;

use TRest\Models\Model;

class Sandwich extends Model
{

    /**
    * The name of the resource
    */
    protected static $resource = 'sandwiches';

    /**
    * In the case of this particular API, a single item in returned inside a node named with the resource
    * name singularized
    */
    protected static $singleItemNode = 'sandwich';
    /**
    * In the case of this particular API, a list of items in returned inside a node named with the resource
    * name pluralized
    */
    protected static $listItemNode = 'sandwiches';

    /*
    * Field definition
    */
    public function fields() {
        return array(
            'id' => array(
                'type' => 'integer'
            ),
            'title' => array(
                'type' => 'string'
            ),
            'price' => array(
                'type' => 'integer'
            )
        );
    }

    /**
    * Retlations
    */
    public function relations() {
        return array(
            'ingredients' => array(
                'class' => 'Ingredient',
                'type' => self::HAS_MANY,
                'postOnSave' => true,
                'postSuffix' => '_attributes'
            )
        );
    }
}

class Ingredient extends Model{

    protected static $resource = 'ingredients';

    protected static $singleItemNode = 'ingredient';

    protected static $listItemNode = 'ingredients';

    public function relations() {
        return array(
            'sandwich' => array(
                'class' => 'Sandwich',
                'type' => self::BELONGS_TO
            )
        );
    }

    public function fields() {
        return array(
            'id' => array(
                'type' => 'integer'
            ),
            'name' => array(
                'type' => 'string'
            ),
            'quantity' => array(
                'type' => 'integer'
            )
        );
    }

}

Now when we perform some actions it will generate the appropriate REST request, execute it, transform the response and hydrate the results to your PHP objects.

$sandwich = new Sandwich();
$sandwich->title = "mark";
$sandwich->price = 9200;
$sandwich->ingredients = [
    new Ingredient(
        (object)['name' => 'Bacon', 'quantity' => 2]
    ),
    new Ingredient(
        (object)['name' => 'Cheese', 'quantity' => 1]
    )
];
$sandwich->save();
/**
* POST http://pixelpt-sandwich-api.herokuapp.com/
* Results {"id":154,"title":"mark","price":9200,
* "ingredients":[{"id":315,"name":"Bacon","quantity":2},{"id":316,"name":"Cheese","quantity":1}]}
*/

Now you can continue working with your model and make more changes to it, also from that point on you will be able to retrive the ids of the models

$sandwich->id; // => 154
$sandwich->ingrediends[0]->id; // => 315

We can retrieve that sandwich again now:

$sandwich = Sandwich::findOne($sandwich->id); // GET http://pixelpt-sandwich-api.herokuapp.com/154

Or you can retrieve all Sandwich objects:

$sandwiches = Sandwich::find()-all();

zoiloreyes/trest 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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