Пример #1
0
<?php

require_once __DIR__ . '/../vendor/autoload.php';
use JLaso\ToolsLib\Json;
use JLaso\ToolsLib\Starter;
$id = isset($_REQUEST["id"]) ? intval($_REQUEST["id"]) : null;
$file = $_REQUEST["file"];
$task = $_REQUEST["_task"];
if (null === $id || !is_array($file) || !$task) {
    Json::error('The "id","file" and "_task" are mandatory in order to process your request!');
} else {
    $url = $_SERVER["SERVER_PORT"] == "443" ? "https://" : "http://";
    $url .= $_SERVER["SERVER_NAME"] . "/server/copy-file.php";
    $starter = new Starter($url, $task);
    if (Starter::SUCCESS == ($result = $starter->invoke(array("id" => $id, "name" => $file["name"], "size" => $file["size"])))) {
        Json::ok(array('id' => $id));
    } else {
        Json::error('something wrong happened trying to start the task on the server! ' . $starter->getLastError());
    }
}
<?php

require_once __DIR__ . '/../vendor/autoload.php';
use JLaso\ToolsLib\Json;
use JLaso\ToolsLib\Status;
$id = $_REQUEST["id"];
$task = $_REQUEST["_task"];
if (!$id) {
    Json::error('The id is mandatory in order to process your request!');
} else {
    $status = new Status($task);
    Json::ok(array('id' => $id, 'status' => $status->getInfo($id)));
}
set_time_limit(0);
ignore_user_abort(true);
// this file wants to simulate a real large process that have to be executed in background
require_once __DIR__ . '/../../vendor/autoload.php';
use JLaso\ToolsLib\Json;
use JLaso\ToolsLib\Status;
$id = isset($_POST['id']) ? $_POST['id'] : null;
$fileName = isset($_POST['name']) ? $_POST['name'] : "";
$fileSize = isset($_POST['size']) ? $_POST['size'] : "";
if (null === $id || !$fileName || !$fileSize) {
    Json::error("you have to send 'id','name' and 'size' to start processing");
    exit;
}
// the next lines terminates the output buffer and let believe the requester that the program had finished
ob_start();
Json::ok();
header("Content-Length: " . ob_get_length());
header('Connection: close');
ob_end_flush();
flush();
session_write_close();
$status = new Status($_POST["_task"]);
$status->touchStatusFile();
// wait a little before the huge work
sleep(1);
$status->updateStatus($id, Status::PROCESSING);
process($status, $id, $fileName, $fileSize);
$status->updateStatus($id, Status::DONE);
sleep(2);
// give time to frontend to recover updated status
$status->freeStatusFile();
<?php

require_once __DIR__ . '/../vendor/autoload.php';
use JLaso\ToolsLib\Json;
use JLaso\ToolsLib\Status;
$id = isset($_REQUEST["id"]) ? intval($_REQUEST["id"]) : null;
$task = $_REQUEST["_task"];
if (null === $id) {
    Json::error('The "id" is mandatory in order to process your request!');
} else {
    $statusService = new Status($task);
    $taskStatus = explode(":", $statusService->getInfo($id));
    $status = isset($taskStatus[0]) ? $taskStatus[0] : 'unknown';
    $percent = isset($taskStatus[1]) ? intval($taskStatus[1]) : ($status == "done" ? 100 : 0);
    Json::ok(array('id' => $id, 'status' => $status, 'percent' => $percent, 'raw' => $statusService->getInfo($id)));
}
<?php

require_once __DIR__ . '/../vendor/autoload.php';
use JLaso\ToolsLib\Json;
use JLaso\ToolsLib\Status;
$task = $_REQUEST["_task"];
$ids = rtrim($_REQUEST["ids"], ",");
if (!$task || !$ids) {
    Json::error('The "_task" and "ids" are mandatory in order to process your request!');
} else {
    $result = array();
    foreach (explode(",", $ids) as $id) {
        $id = intval($id);
        $statusService = new Status($task . "-" . $id);
        $status = $statusService->getInfo($id);
        if (!isset($result[$id])) {
            $result[$id] = array("id" => $id);
            $temp = explode(":", $status);
            $result[$id]["percent"] = isset($temp[1]) ? intval($temp[1]) : 0;
            $result[$id]["status"] = isset($temp[0]) ? $temp[0] : $status;
        }
    }
    Json::ok(array('info' => array_values($result)));
}