ascio/ascio-framework

Ascio-API 的 PHP 框架。同步、自动队列、更新、REST-API

v0.1 2021-09-15 08:46 UTC

This package is auto-updated.

Last update: 2024-09-30 01:35:41 UTC


README

一个使用 docker 同步和连接到 ascio-api 的 php 框架。可扩展,基于 docker 和 kafka。

它使域名管理过程变得轻松

需求

唯一需求是 docker。它可以在以下位置下载: https://www.docker.com/

安装

  1. 克隆仓库: git clone https://github.com/rendermani/ascio-framework.git ascio-framework
  2. cd ascio-framework
  3. chmod u+x bin/init.sh
  4. 运行 bin/init.sh <<github token>>。github-token 可选用于扩展配额(无法验证对 github.com 的身份)。可以在以下位置获得: https://github.com/settings/tokens
  5. .env 中设置缺失的密码
  6. config/accounts/default.json 中输入 ascio 凭证
  7. 运行 bin/up.sh

初始数据库同步:ascio > 本地

最初数据库为空。当 Ascio 消息发送,当有变化时,最初必须进行同步。同步订单脚本是同步所有订单和数据,例如域名或 SSL 证书。

  • 确保安装完成,并且 default.json 包含有效的凭证和环境
  • 运行 bin/sync-orders.sh

测试安装

最初日志中会有 Kafka 异常。当 Kafka 完全加载后,这将会消失。没有依赖 Kafka,因为 Kafka 可以在别处运行。

  • 运行 bin/logs.sh。寻找 401 认证失败
  • 运行 bin/php sandbox/create-order.php
  • 观察日志以完成轮询。
  • 检查数据库:https://:8001/

位置

命令

  • 初始化框架,创建表,构建:bin/init.sh
  • 初始同步订单:bin/sync-orders.sh
  • 启动:bin/up.sh
  • 停止:bin/down.sh
  • 重启框架:bin/restart-framework.sh
  • 查看日志:bin/logs.sh
  • Docker shell:docker exec -it ascio-framework-php bash
  • 执行 php:docker exec ascio-framework-php php sandbox/my-example.php

示例代码

示例在 sandbox 目录中。这里有一些示例

当阻塞时自动队列下一个订单

首先创建一个注册订单并注册一个域名。通过设置 $submitOptions->setQueue(false) 订单将直接提交。之后发送一个过期订单。但是,由于异步注册订单仍在运行,过期订单被排入数据库,并在注册完成后开始。

<?php
namespace ascio\lib;
use ascio\v2\TestLib;
use ascio\lib\SubmitOptions;
use SoapFault;

require(__DIR__."/../vendor/autoload.php");
// an alternative can be set here
Ascio::setConfig();
// get Test-Domain
$domain = TestLib::getDomain("testme-".uniqid().".com");
try {
    $submitOptions = new SubmitOptions();
    // direct submit if possible
    $submitOptions->setQueue(false);
    // register the domain
    $order = $domain->register($submitOptions);
    echo "Register: ". $domain->getDomainName()." Order: ".$order->getOrderId()." ".$order->db()->_id."\n";
    // as register is running, the expire order will be auto-queued
    $order = $domain->expire($submitOptions);
    echo "Expire: ". $domain->getDomainName()." Order: ".$order->getOrderId()." ".$order->db()->_id."\n";
} catch (SoapFault $e) {
    echo "error!";
    echo $e->getMessage();
    // view SOAP-Request
    echo ascio::getClientV2()->__getLastRequest();
} catch (AscioException $e) {
    // view Ascio-Errors and the result
    echo $e->getMessage();
    var_dump($e->getResult()->getCreateOrderResult());
}

update() 方法

通常在更改数据时需要知道订单类型。通过域的更新方法,可以更改任何数据,订单类型将自动选择。如果需要多个订单类型,订单将像上面示例那样排队。

某些订单类型可以更改两个不同的对象。例如,所有者变更可以在一个请求中更改注册者和管理员联系人。在这种情况下,更新方法仅使用必要的对象。

<?php
namespace ascio\lib;
use ascio\v2\TestLib;
use SoapFault;

require(__DIR__."/../vendor/autoload.php");
// an alternative can be set here
Ascio::setConfig();
// get Test-Domain
$domain = TestLib::getDomain("testme-".uniqid().".com");
try {
    // register the domain
    $order = $domain->register();
    echo "Register: ". $domain->getDomainName()." Order: ".$order->getOrderId()." ".$order->db()->_id."\n";
    // change domain data
    $domain->getAdminContact()->setFirstName("Manuel");
    $domain->getRegistrant()->setAddress1("new adr. 123");
    $domain->getNameServers()->createNameServer3()->setHostName("ns3.ascio.net");
    // 3 orders are created: Registrant_Details_Update, Contact_Update and Nameserver_Update
    $domain->update();
} catch (SoapFault $e) {
    echo "error!";
    echo $e->getMessage();
    // view SOAP-Request
    echo ascio::getClientV2()->__getLastRequest();
} catch (AscioException $e) {
    // view Ascio-Errors and the result
    echo $e->getMessage();
    var_dump($e->getResult()->getCreateOrderResult());
}

将数据存储到数据库中

所有数据都会自动存储在数据库中。如果需要手动存储数据,请使用$object->produce()将数据存储到所有数据库中。这个函数应谨慎使用,因为数据可能会不同步。

从数据库读取数据

可以从任何数据库中读取数据库。例如,域名对象具有序列化和反序列化数据的功能。请使用$object->deserialize()导入数据。使用$object->serialize($data)$object->toJson()进行序列化。

以下是从数据库检索数据的示例

// get Domain by databaseId
$domain->db()->getById("myId");
// get Domain by ascio domain-handle
$domain->db()->getByHandle("DomainHandle");
// get Domain by domain-name
$domain->db()->getByName("myname.com");
// get Domain order by OrderId
$order->db()->getByOrderId();
// get Order by databaseId
$order->db()->getById("myId");
// get related tables: Domain, Registrant
$order->getDomain()->getRegistrant();
// custom lavarel-query 
$domain->db()->where("Status","Active")->first();
// custom query
$domain->db()->raw("select * from domains where Status = 'Active'");

示例数据库连接器

数据库连接器监听Kafka服务并将数据写入数据库。可以包含自己的连接器。目前,已包含Redis和Couch作为示例。这是Redis连接器。

创建连接器的过程是

  • docker/docker-compose.yml中创建连接器。复制redis-连接器,并更改脚本名称为您自己的文件
  • 创建连接器文件
  • bin/down.sh
  • bin/up.sh

这是一个位于服务目录中的示例Redis连接器。

<?php 
namespace ascio\lib;
require(__DIR__."/../vendor/autoload.php");
use Illuminate\Support\Str;

\Predis\Autoloader::register();

$client = new \Predis\Client([
    'scheme' => 'tcp',
    'host'   => 'redis',
    'port'   => 6379,
]);

Consumer::object(function($payload) use($client) {
    Ascio::setConfig($payload->Config);
    $obj = $payload->object;
    $client->set($obj->db()->_id,$obj->toJson());
    echo $obj->getStatusSerializer()->console(LogLevel::Info,Str::ucfirst($payload->action));  
});

从API读取订单

从API读取订单。当从API读取时,数据会自动同步到所有数据库。

<?php
use ascio\v2\Order;

require(__DIR__."/../vendor/autoload.php");
Ascio::setConfig();
$order = new Order();
$order->api()->getByOrderId("TEST561580");

从API按句柄读取域名

从API读取域名。当从API读取时,数据会自动同步到所有数据库。

<?php
use ascio\v2\Domain;
require(__DIR__."/../vendor/autoload.php");
Ascio::setConfig();
$domain = new Domain();
$domain->api()->getByHandle("TEST1234");

从API按域名名称读取域名

从API读取域名。当从API读取时,数据会自动同步到所有数据库。

<?php
use ascio\v2\Domain;
require(__DIR__."/../vendor/autoload.php");
Ascio::setConfig();
$domain = new Domain();
$domain->api()->getByName("testmeee.com");

从API更新数据

$order = new Order();
$order->db()->getByOrderId("TEST561580");
$order->api()->get(); 

$domain = new Domain();
$domain->db()->getByName("test.de");
$domain->api()->get();

与AutoUnlock的工怍流

有时锁可能会阻止更新。当更新数千个域名时,这可能会成为一个问题,因为一些域名因为被锁定而失败。在所有操作完成后,它们需要被解锁并重新锁定。AutoUnlock使这个过程更容易。

在过期前自动解锁(Delete_Lock)

<?php
namespace ascio\lib;

use ascio\v2\Domain;
use ascio\v2\TestLib;


require(__DIR__."/../vendor/autoload.php");
Ascio::setConfig();

$domain = new Domain();
$domain->getByHandle("TESTME5D66885");
$wf = new Workflow($domain);
// if the orders of the workflow require and unlock it will be done before submitting the orders.
// After the orders are finished the relock will be restored if possible.
$wf->getSubmitOptions()->setAutoUnlock(true);
// add the expire order-object
$wf->addTask($domain->getOrderRequest()->expire());
// submit all orders
$wf->submit();

在更新前自动解锁

此示例与上面的示例相同,只是使用了声明性方法。不是添加一个$domain->getOrderRequest()->expire()任务,而是添加一个$domain->getUpdateOrders()任务。还可能有更多更改,例如$domain->getRegistrant()->setName("Manuel L"),并创建一个所有者变更。如果需要,将释放更新锁。

<?php
namespace ascio\lib;
use ascio\v2\TestLib;


require(__DIR__."/../vendor/autoload.php");
Ascio::setConfig();

$domain = TestLib::getDomain("testme-".uniqid().".com");
// register domain
$domain->register();
// set AutoRenew
$domain->getAutoRenew()->set(false);
$wf = new Workflow($domain);
// if the orders of the workflow require and unlock it will be done before submitting the orders.
// After the orders are finished the relock will be restored if possible.
$wf->getSubmitOptions()->setAutoUnlock(true);
$wf->addTasks($domain->getUpdateOrders());
// submit workflow
$wf->submit();