1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AlecRabbit\Spinner\Core\A; |
6
|
|
|
|
7
|
|
|
use AlecRabbit\Spinner\Contract\IDeltaTimer; |
8
|
|
|
use AlecRabbit\Spinner\Contract\IInterval; |
9
|
|
|
use AlecRabbit\Spinner\Contract\IObserver; |
10
|
|
|
use AlecRabbit\Spinner\Core\Contract\IDriver; |
11
|
|
|
use AlecRabbit\Spinner\Core\Contract\IDriverMessages; |
12
|
|
|
use AlecRabbit\Spinner\Core\Contract\IRenderer; |
13
|
|
|
use AlecRabbit\Spinner\Core\Contract\ISpinner; |
14
|
|
|
use Closure; |
15
|
|
|
|
16
|
|
|
abstract class ADriver extends ASubject implements IDriver |
17
|
|
|
{ |
18
|
|
|
protected IInterval $interval; |
19
|
|
|
|
20
|
|
|
protected function __construct( |
21
|
|
|
protected readonly IInterval $initialInterval, |
22
|
|
|
protected readonly IDriverMessages $driverMessages, |
23
|
|
|
protected readonly IRenderer $renderer, |
24
|
|
|
protected readonly IDeltaTimer $deltaTimer, |
25
|
|
|
?IObserver $observer = null, |
26
|
|
|
) { |
27
|
|
|
parent::__construct($observer); |
28
|
|
|
$this->interval = $this->initialInterval; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function interrupt(?string $interruptMessage = null): void |
32
|
|
|
{ |
33
|
|
|
$this->finalize($interruptMessage ?? $this->driverMessages->getInterruptionMessage()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function finalize(?string $finalMessage = null): void |
37
|
|
|
{ |
38
|
|
|
$this->erase(); |
39
|
|
|
$this->renderer->finalize($finalMessage ?? $this->driverMessages->getFinalMessage()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
abstract protected function erase(): void; |
43
|
|
|
|
44
|
|
|
public function getInterval(): IInterval |
45
|
|
|
{ |
46
|
|
|
return $this->interval; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function wrap(Closure $callback): Closure |
50
|
|
|
{ |
51
|
|
|
return function (mixed ...$args) use ($callback): void { |
52
|
|
|
$this->erase(); |
53
|
|
|
$callback(...$args); |
54
|
|
|
$this->render(); |
55
|
|
|
}; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
abstract public function render(?float $dt = null): void; |
59
|
|
|
|
60
|
|
|
public function initialize(): void |
61
|
|
|
{ |
62
|
|
|
$this->renderer->initialize(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
abstract public function has(ISpinner $spinner): bool; |
66
|
|
|
} |
67
|
|
|
|