reliqarts /
laravel-docweaver
| 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 null|Command |
||
| 25 | */ |
||
| 26 | protected ?Command $callingCommand; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var FilesystemContract |
||
| 30 | */ |
||
| 31 | protected FilesystemContract $filesystem; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var LoggerContract |
||
| 35 | */ |
||
| 36 | protected LoggerContract $logger; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var float |
||
| 40 | */ |
||
| 41 | private float $startTime; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Publisher constructor. |
||
| 45 | */ |
||
| 46 | public function __construct(FilesystemContract $filesystem, LoggerContract $logger) |
||
| 47 | { |
||
| 48 | $this->filesystem = $filesystem; |
||
| 49 | $this->logger = $logger; |
||
| 50 | $this->startTime = microtime(true); |
||
| 51 | $this->callingCommand = null; |
||
| 52 | } |
||
| 53 | |||
| 54 | protected function getExecutionTime(): string |
||
| 55 | { |
||
| 56 | return sprintf('%ss', $this->secondsSince($this->startTime)); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Print to console or screen. |
||
| 61 | * |
||
| 62 | * @param string $text |
||
| 63 | * @param string $direction in|out |
||
| 64 | */ |
||
| 65 | protected function tell($text, $direction = self::TELL_DIRECTION_OUT): string |
||
| 66 | { |
||
| 67 | $direction = strtolower($direction); |
||
| 68 | $nl = app()->runningInConsole() ? "\n" : '<br/>'; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 69 | $dirSymbol = ($direction === self::TELL_DIRECTION_IN |
||
| 70 | ? '>> ' |
||
| 71 | : ($direction === self::TELL_DIRECTION_FLAT ? '-- ' : '<< ')); |
||
| 72 | if ($direction === self::TELL_DIRECTION_NONE) { |
||
| 73 | $dirSymbol = ''; |
||
| 74 | } |
||
| 75 | |||
| 76 | if ($this->callingCommand && app()->runningInConsole()) { |
||
| 77 | $line = sprintf('%s%s', $dirSymbol, $text); |
||
| 78 | |||
| 79 | if ($direction === self::TELL_DIRECTION_OUT) { |
||
| 80 | $line = sprintf('<info>%s</info>', $line); |
||
| 81 | } |
||
| 82 | |||
| 83 | $this->callingCommand->line($line); |
||
| 84 | } else { |
||
| 85 | echo "{$nl}{$dirSymbol}{$text}"; |
||
| 86 | } |
||
| 87 | |||
| 88 | return $text; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Ensure documentation resource directory exists and is writable. |
||
| 93 | */ |
||
| 94 | protected function readyResourceDirectory(string $directory): bool |
||
| 95 | { |
||
| 96 | if (!$this->filesystem->isDirectory($directory)) { |
||
| 97 | $this->filesystem->makeDirectory($directory, self::DIRECTORY_READY_MODE, true); |
||
| 98 | } |
||
| 99 | |||
| 100 | return $this->filesystem->isWritable($directory); |
||
| 101 | } |
||
| 102 | |||
| 103 | protected function setExecutionStartTime(): void |
||
| 104 | { |
||
| 105 | $this->startTime = microtime(true); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Get seconds since a micro-time start-time. |
||
| 110 | * |
||
| 111 | * @param float $startTime start time in microseconds |
||
| 112 | * |
||
| 113 | * @return string seconds since, to 2 decimal places |
||
| 114 | */ |
||
| 115 | private function secondsSince(float $startTime): string |
||
| 116 | { |
||
| 117 | $duration = microtime(true) - $startTime; |
||
| 118 | $hours = (int)($duration / 60 / 60); |
||
| 119 | $minutes = (int)($duration / 60) - $hours * 60; |
||
| 120 | $seconds = $duration - $hours * 60 * 60 - $minutes * 60; |
||
| 121 | |||
| 122 | return number_format((float)$seconds, 2, '.', ''); |
||
| 123 | } |
||
| 124 | } |
||
| 125 |