|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ReliqArts\Docweaver\Service; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Console\Command; |
|
8
|
|
|
use ReliqArts\Docweaver\Contract\Filesystem as FilesystemContract; |
|
9
|
|
|
use ReliqArts\Docweaver\Contract\Logger as LoggerContract; |
|
10
|
|
|
use ReliqArts\Docweaver\Contract\Publisher as PublisherContract; |
|
11
|
|
|
|
|
12
|
|
|
abstract class Publisher implements PublisherContract |
|
13
|
|
|
{ |
|
14
|
|
|
protected const TELL_DIRECTION_OUT = 'out'; |
|
15
|
|
|
protected const TELL_DIRECTION_IN = 'in'; |
|
16
|
|
|
protected const TELL_DIRECTION_FLAT = 'flat'; |
|
17
|
|
|
protected const TELL_DIRECTION_NONE = 'none'; |
|
18
|
|
|
|
|
19
|
|
|
private const DIRECTORY_READY_MODE = 0777; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Calling command if running in console. |
|
23
|
|
|
* |
|
24
|
|
|
* @var Command |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $callingCommand; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var FilesystemContract |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $filesystem; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var LoggerContract |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $logger; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var float |
|
40
|
|
|
*/ |
|
41
|
|
|
private $startTime; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Publisher constructor. |
|
45
|
|
|
* |
|
46
|
|
|
* @param FilesystemContract $filesystem |
|
47
|
|
|
* @param LoggerContract $logger |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct(FilesystemContract $filesystem, LoggerContract $logger) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->filesystem = $filesystem; |
|
52
|
|
|
$this->logger = $logger; |
|
53
|
|
|
$this->startTime = microtime(true); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function getExecutionTime(): string |
|
60
|
|
|
{ |
|
61
|
|
|
return sprintf('%ss', $this->secondsSince($this->startTime)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Print to console or screen. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $text |
|
68
|
|
|
* @param string $direction in|out |
|
69
|
|
|
* |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function tell($text, $direction = self::TELL_DIRECTION_OUT) |
|
73
|
|
|
{ |
|
74
|
|
|
$direction = strtolower($direction); |
|
75
|
|
|
$nl = app()->runningInConsole() ? "\n" : '<br/>'; |
|
76
|
|
|
$dirSymbol = ($direction === self::TELL_DIRECTION_IN |
|
77
|
|
|
? '>> ' |
|
78
|
|
|
: ($direction === self::TELL_DIRECTION_FLAT ? '-- ' : '<< ')); |
|
79
|
|
|
if ($direction === self::TELL_DIRECTION_NONE) { |
|
80
|
|
|
$dirSymbol = ''; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (app()->runningInConsole() && $this->callingCommand) { |
|
84
|
|
|
$line = sprintf('%s%s', $dirSymbol, $text); |
|
85
|
|
|
|
|
86
|
|
|
if ($direction === self::TELL_DIRECTION_OUT) { |
|
87
|
|
|
$line = sprintf('<info>%s</info>', $line); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$this->callingCommand->line($line); |
|
91
|
|
|
} else { |
|
92
|
|
|
echo "{$nl}{$dirSymbol}{$text}"; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $text; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Ensure documentation resource directory exists and is writable. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $directory |
|
102
|
|
|
* |
|
103
|
|
|
* @return bool |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function readyResourceDirectory(string $directory): bool |
|
106
|
|
|
{ |
|
107
|
|
|
if (!$this->filesystem->isDirectory($directory)) { |
|
108
|
|
|
$this->filesystem->makeDirectory($directory, self::DIRECTORY_READY_MODE, true); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $this->filesystem->isWritable($directory); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
protected function setExecutionStartTime(): void |
|
115
|
|
|
{ |
|
116
|
|
|
$this->startTime = microtime(true); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Get seconds since a micro-time start-time. |
|
121
|
|
|
* |
|
122
|
|
|
* @param float $startTime start time in microseconds |
|
123
|
|
|
* |
|
124
|
|
|
* @return string seconds since, to 2 decimal places |
|
125
|
|
|
*/ |
|
126
|
|
|
private function secondsSince(float $startTime): string |
|
127
|
|
|
{ |
|
128
|
|
|
$duration = microtime(true) - $startTime; |
|
129
|
|
|
$hours = (int)($duration / 60 / 60); |
|
130
|
|
|
$minutes = (int)($duration / 60) - $hours * 60; |
|
131
|
|
|
$seconds = $duration - $hours * 60 * 60 - $minutes * 60; |
|
132
|
|
|
|
|
133
|
|
|
return number_format((float)$seconds, 2, '.', ''); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|