ubnt / ucrm-plugin-sdk

v0.10.0 2023-09-25 09:37 UTC

This package is auto-updated.

Last update: 2024-08-25 11:38:39 UTC


README

Coverage Latest Release License

此仓库包含用于 UCRM 插件 的开源 PHP SDK。

安装

可以使用 Composer 安装 UCRM 插件 SDK。运行以下命令

composer require ubnt/ucrm-plugin-sdk

可用类

用法

使用可用 SDK 类的简单示例

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

// Get UCRM log manager.
$log = \Ubnt\UcrmPluginSdk\Service\PluginLogManager::create();

// Check if there is a user logged into UCRM and has permission to view invoices.
// https://github.com/Ubiquiti-App/UCRM-plugins/blob/master/docs/security.md
$security = \Ubnt\UcrmPluginSdk\Service\UcrmSecurity::create();
$user = $security->getUser();
if (
    ! $user
    || ! $user->hasViewPermission(\Ubnt\UcrmPluginSdk\Security\PermissionNames::BILLING_INVOICES)
) {
    if (! headers_sent()) {
        header("HTTP/1.1 403 Forbidden");
    }

    $log->appendLog('Someone tried to access page only for admins with permission to view invoices.');

    die('You\'re not allowed to access this page.');
}

$log->appendLog('Starting invoice export.');

// Get export format from plugin's configuration.
// https://github.com/Ubiquiti-App/UCRM-plugins/blob/master/docs/manifest.md#configuration
$configManager = \Ubnt\UcrmPluginSdk\Service\PluginConfigManager::create();
$config = $configManager->loadConfig();
// the "exportFormat" key must be defined in plugin's manifest file, see the link above
$exportFormat = $config['exportFormat'];

// Get UCRM API manager.
$api = \Ubnt\UcrmPluginSdk\Service\UcrmApi::create();

// Load invoices from UCRM API ordered by created date in descending direction.
// https://ucrm.docs.apiary.io/#reference/invoices/invoicesclientidcreateddatefromcreateddateto/get
$invoices = $api->get(
    'invoices',
    [
        'order' => 'createdDate',
        'direction' => 'DESC',
    ]
);

foreach ($invoices as $invoice) {
    // some export implementation, using the $exportFormat
}

// Get plugin's public URL from automatically generated UCRM options.
// https://github.com/Ubiquiti-App/UCRM-plugins/blob/master/docs/file-structure.md#ucrmjson
$optionsManager = \Ubnt\UcrmPluginSdk\Service\UcrmOptionsManager::create();
$pluginPublicUrl = $optionsManager->loadOptions()->pluginPublicUrl;

$log->appendLog(sprintf('Finished invoice export. Take a look at %s.', $pluginPublicUrl));

打包脚本

要打包插件以便在 UCRM 中使用,可以使用提供的打包脚本。从根目录运行以下命令

./vendor/bin/pack-plugin

该脚本将创建插件的 ZIP 存档,可以上传到 UCRM。如果您正在使用官方 UCRM 插件仓库 的目录结构,存档将在您的 README.md 文件和 src/ 目录的上一个级别创建。否则,它将在您的根目录中创建。

如果无法正确检测到插件的根目录,您可以将其作为参数提供给脚本。例如
./vendor/bin/pack-plugin /home/username/my-new-plugin

测试

可以从根目录运行以下命令来执行单元测试

./vendor/bin/phpunit

可以从根目录运行以下命令来执行静态分析

./vendor/bin/phpstan analyse src tests --level max

可以从根目录运行以下命令来执行编码标准检查

./vendor/bin/ecs check src tests

免责声明

软件按“原样”提供,不提供任何类型的保证。更多信息请参阅 许可协议