Ext Direct 规范的 PHP 实现

0.4.6 2017-07-31 15:02 UTC

This package is not auto-updated.

Last update: 2024-09-22 08:58:04 UTC


README

Sencha Ext Direct 规范的 PHP 实现

该项目部分代码来自 J. Bruni (ExtDirect.php)

安装

composer require danielbragaalmeida/extdirect

如何使用

PHP

config.php

<?php
return [
    'discoverer' => [
        'paths' => [ // Directories of your classes
            __DIR__ . '/src', 
        ]
    ],
    'cache' => [
        'directory' => __DIR__ . '/cache',
        'lifetime' => 60,
    ],
    'api' => [
        'descriptor' => 'window.uERP_REMOTING_API',
        'declaration' => [
            'url' => 'http://api.myadomain.com/router.php', // Your router may be in another domain
            'type' => 'remoting',
            'id' => 'uERP', // it's required for the cache mechanism
            'namespace' => 'Ext.php',
            'timeout' => null,
        ]
    ]
];

示例类 src/Server.php

<?php
namespace Util;

/**
 * Class Server
 *
 * @ExtDirect
 * @ExtDirect\Alias UtilServer
 */
class Server
{
    /**
     * @param $format
     * @return bool|string
     * @ExtDirect
     */
    public function date($format)
    {
        return date($format);
    }

    /**
     *
     * @return string
     * @ExtDirect
     */
    public function hostname()
    {
        return gethostname();
    }
}

api.php

<?php
require_once __DIR__ . '/../vendor/autoload.php';

$config = new ExtDirect\Config(include 'config.php');

$discoverer = new ExtDirect\Discoverer($config);
$discoverer->start();

router.php

<?php
require_once __DIR__ . '/../vendor/autoload.php';

$config = new ExtDirect\Config(include 'config.php');

$discoverer = new ExtDirect\Router($config);
$discoverer->route();

HTML

<-- API in same domain -->
<script type="text/javascript" src="api.php"></script>

<-- API in another domain -->
<script type="text/javascript" src="http://api.mydomain.com/api.php"></script>

JavaScript

在此,您可以调用 API 中的操作/方法。如果您公开了 Util\Server(别名 UtilServer)类和 date 方法,您必须将 API 类别如下所示

Ext.Direct.addProvider(window.uERP_REMOTING_API); // window.uERP_REMOTING_API is your descriptor
Ext.php.UtilServer.date('Y-m-d', function(result) {
    alert('Server date is ' + result); 
});

特性

  1. 一次性配置,到处可用。

    您配置类所在路径,而不是要公开哪些类。

  2. 使用注解轻松确定要公开哪些类和方法。所有具有 @ExtDirect 的类都将检查可公开的方法(只有具有 @ExtDirect 的方法才会公开)。

  3. 缓存机制。您的 API 类和方法将被缓存,以避免过度加载发现过程。您可以配置缓存有效期。

配置

一个具有以下结构的数组

  • 发现
    • 路径 array
  • 缓存 array
    • 目录 string
    • 有效期 int
  • API
    • 描述符 string
    • 声明
      • URL string
      • 类型 string
      • 标识符 string
      • 命名空间 string
      • 超时 int

发现配置

discovery.paths: 一个包含类路径的数组。

<?php
...
'discoverer' => [
    'paths' => [
        __DIR__ . '/../src',
        __DIR__ . '/../lib',
    ]
],
...

cache.directory: 用于存储缓存数据的目录。

cache.lifetime: 缓存有效期,以秒为单位。

<?php
...
'cache' => [
    'directory' => __DIR__ . '/../cache',
    'lifetime' => 60,
],
...

API 配置

api.descriptor: 将接收 API 声明的 JavaScript 变量。

api.declaration.url: 此 API 的服务 URI。

api.declaration.type: 必须是 remoting(用于远程 API)或 polling(用于轮询 API)。

api.declaration.id: 远程 API 提供程序的标识符。当使用多个 API 时很有用。缓存机制需要它。

api.declaration.namespace: 给定远程 API 的命名空间。

api.declaration.timeout: 在此远程 API 中每个方法调用作为超时使用的毫秒数。(未实现)