-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathFileOperationTask.php
More file actions
83 lines (71 loc) · 2.07 KB
/
Copy pathFileOperationTask.php
File metadata and controls
83 lines (71 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
* _ _ _ _
* | (_) | (_)
* | |_| |__ __ _ ___ _ _ _ __ ___ _ ___
* | | | '_ \ / _` / __| | | | '_ \ / __| |/ _ \
* | | | |_) | (_| \__ \ |_| | | | | (__| | (_) |
* |_|_|_.__/ \__,_|___/\__, |_| |_|\___|_|\___/
* __/ |
* |___/
*
* Copyright (C) 2016-2026 NetherGames Network
*
* This is private software, you cannot redistribute and/or modify it in any way
* unless given explicit permission to do so. If you have not been given explicit
* permission to view or modify this software you should take the appropriate actions
* to remove this software from your device immediately.
*
* @author driesboy
*
*/
declare(strict_types=1);
namespace libasyncio;
use InvalidArgumentException;
use pocketmine\scheduler\AsyncTask;
use function is_callable;
abstract class FileOperationTask extends AsyncTask
{
/** @var string */
protected string $source;
/** @var bool */
private bool $success = false;
/** @var float */
private float $taskTime = 0.0;
public function __construct(string $source, ?callable $onSuccess = null)
{
$this->source = $source;
$this->storeLocal('closure', $onSuccess);
}
/**
* @inheritDoc
*/
public function onRun(): void
{
$this->taskTime = microtime(true);
}
public function onCompletion(): void
{
$this->taskTime = microtime(true) - $this->taskTime;
$this->checkSuccess();
if ($this->getSuccess()) {
try {
$onSuccess = $this->fetchLocal('closure');
} catch (InvalidArgumentException) {
$onSuccess = null;
}
if (is_callable($onSuccess)) {
$onSuccess($this->taskTime);
}
}
}
abstract protected function checkSuccess(): void;
public function getSuccess(): bool
{
return $this->success;
}
public function setSuccess(bool $success): void
{
$this->success = $success;
}
}