and/whmcs-module-framework

此包最新版本(dev-master)没有提供许可证信息。

WHMCS 小型框架,用于模块/插件(附带一些实用工具集合)

dev-master 2020-01-17 20:33 UTC

This package is auto-updated.

Last update: 2024-09-12 23:21:00 UTC


README

如何使用

1. 在 WHMCS 模块目录内创建插件,例如 "plugin-name"

2. 在 "plugin-name" 目录内创建 "plugin-name.php" 文件

$bootstrap = require_once __DIR__ . '/vendor/and/whmcs-module-framework/bootstrapper.php';
$bootstrap();

return Addon::registerModuleByFile(__FILE__,
   Addon::configBuilder()
   ->setName('My Plugin Friendly Name')
   ->setDescription('description')
   ->setAuthor('And')
   ->addField(FieldConfigBuilder::dropdown('choice', 'Type')->addOption('One and only option', 'option1'))
   ->build());

3. 在 "plugin-name" 目录内创建 "hooks.php" 文件

ModuleHooks::registerHooks([
    YourHookClass::class,
    CallbackHook::createCallback('eventName', 0, function() {})
]);

或者

CallbackHook::attachCallback('InvoiceCreation', 0, function() {
    echo $this->getModule()->getId();
    echo $this->view('smarty.tpl', ['var' => 'value']);
});
CallbackHook::attachCallback(Invoice\InvoiceCreated::KEY, 0, function() {
    // Do your things here
});

API 请求者

$orders = APIFactory::orders()->getOrders($userId, 'Pending');
// No result, most cases is error
if (!$orders->isLoaded()) {
    return;
}
APIFactory::billing()
->addCredit($userId, $invoice['credit'],
    sprintf('Refund of customers credit for "%s" because of order "%s" cancellation (Blazing Dashboard Proxy)',
        $invoice['credit'], $order['id']))
->validate('result=success')

响应中可用的方法

  • #isLoaded() - 如果有错误或错误的响应,则为 false;每个响应都通过请求方法中定义的数据路径进行验证,例如对于 getInvoices,它是 invoices.invoice
  • #validate(key, messageForException, exceptionClass = ResponseException) - key 可以是 path.with.dots
  • #getApiMethod()
  • #getApiArgs()
  • #__toString() - json 编码的数据

响应是有效的数组,除了你可以使用来获取 values path.with.dots(嵌套数组路径)。更方便的是 - 路径不区分大小写。有一点 - 你可以在数组中更改(设置/取消设置)值。

默认情况下,响应相对于数据容器(对于 getInvoices 它是 invoices.invoice),但如果你想从根获取数据,你应该使用路径 data.rootResponseValue

页面钩子

将自定义内容注入任何页面的方法。用法

AnyPageHook::buildInstance()->setPosition(AnyPageHook::POSITION_HEAD_BOTTOM)->setCodeCallback(function($vars) {
    $page = $vars['templatefile'];

    return "<meta page-template=\"$page\" />";
})->apply()