windawake/laravelremodel

创建远程驱动程序,将远程API请求转换为Laravel模型。

v1.0.1 2021-10-13 05:14 UTC

This package is auto-updated.

Last update: 2024-09-22 19:17:38 UTC


README

中文 日本語

创建远程驱动程序,将远程API请求转换为Laravel模型。

为了与我讨论技术,你可以把我加到微信。

概览

安装laravel5.5-laravel8之间的版本,然后安装快速微服务包。

composer require windawake/laravelremodel dev-master

首先执行命令php artisan laravelremodel:example-models,将OrderDetailRemote.php、OrderRemote.php和ProductRemote.php三个文件(位于./vendor/windawake/laravelremodel/examples/Models目录下)复制到app文件夹。

├── app
│   ├── Console
│   │   └── Kernel.php
│   ├── Exceptions
│   │   └── Handler.php
│   ├── Http
│   │   ├── Controllers
│   │   ├── Kernel.php
│   │   └── Middleware
│   ├── Models
│   │   ├── OrderDetailRemote.php
│   │   ├── OrderRemote.php
│   │   └── ProductRemote.php

然后执行命令创建SQLite数据库文件test.db

php ./vendor/windawake/laravelremodel/examples/sqlite/build.php

在phpunit.xml中添加sqlite配置和Remote测试套件。

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>

        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>

        <testsuite name="Remote">
            	<directory>./vendor/windawake/laravelremodel/tests</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="MAIL_DRIVER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="DB_CONNECTION" value="sqlite"/>
        <server name="DB_DATABASE" value="./test.db"/>
    </php>
</phpunit>

最后,运行测试命令./vendor/bin/phpunit --testsuit=Remote,运行结果如下,18个ORM示例测试通过。

root@DESKTOP-VQOELJ5:/web/linux/php/laravel/laravel58# ./vendor/bin/phpunit --testsuit=Remote
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.

..................                                                18 / 18 (100%)

Time: 208 ms, Memory: 20.00 MB

OK (18 tests, 21 assertions)

特性

  1. 应用程序后端代码无需重构,逐步实现API接口的渐进式服务。
  2. 支持懒加载,避免1+n查询API。
  3. 支持连接表、联合表、原生SQL查询、聚合查询、子查询等。几乎可以使用所有Laravel ORM功能。
  4. 使用Laravel服务容器编写方法,因此可以自定义查询编译器和分布式事务方法。(推荐使用分布式事务组件)。

原理

远程基本服务的API接口被封装成ORM。我的应用程序模型只是一个虚拟模型,它是远程基本服务模型的镜像。例如,紫色ProductModel是一个镜像,但OrderLogic几乎以同样的方式使用它。这样做有什么好处?可以重用Laravel模型的所有功能。因为现在许多包为模型做了很多新功能,不使用它们真是太遗憾了。

如何使用

创建一个新的ProductRemote类,继承自RemoteModel类。

<?php
namespace App\Models;

use Laravel\Remote2Model\RemoteModel;

class ProductRemote extends RemoteModel {
    const CREATED_AT = null;
    const UPDATED_AT = null;

    protected $primaryKey = 'pid';
    protected $table = 'product';
    public $timestamps = false;

    public function getHandle()
    {
        /**
         * @var RemoteTool
         */
        $remoteTool = app('laravelremodel.tool');
        $condition = $remoteTool->queryToCondition($this->queryBuilder);

        $client = new \GuzzleHttp\Client();
        $res = $client->request('GET', 'http://127.0.0.1:18001/api/product', [
            'query' => [
                'condition' => $condition
            ],
        ]);
        $json = $res->getBody()->getContents();
        
        $list = json_decode($json, true);
        return $list;
    }
    
}

上面的示例是查询getHandle。默认提供5个方法:getHandle、updateHandle、insertGetIdHandle、deleteHandle和existsHandle。继承RemoteModel类后,无需定义这些方法,如getHandle,将默认使用数据库驱动,就像正常的Model类一样。