reliqarts /
laravel-scavenger
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace ReliqArts\Scavenger\Service; |
||
| 6 | |||
| 7 | use Illuminate\Console\Command; |
||
| 8 | |||
| 9 | abstract class Communicator |
||
| 10 | { |
||
| 11 | protected const COMM_DIRECTION_IN = 'in'; |
||
| 12 | protected const COMM_DIRECTION_OUT = 'out'; |
||
| 13 | protected const COMM_DIRECTION_FLAT = 'flat'; |
||
| 14 | protected const COMM_DIRECTION_NONE = 'none'; |
||
| 15 | protected const HASH_ALGORITHM = 'sha512'; |
||
| 16 | protected const VERBOSITY_HIGH = 3; |
||
| 17 | protected const VERBOSITY_MEDIUM = 2; |
||
| 18 | protected const VERBOSITY_LOW = 1; |
||
| 19 | |||
| 20 | private const COMM_SPACE = ' '; |
||
| 21 | private const COMM_SYMBOL_IN = '>>'; |
||
| 22 | private const COMM_SYMBOL_OUT = '<<'; |
||
| 23 | private const COMM_SYMBOL_FLAT = '--'; |
||
| 24 | private const COMM_SYMBOL_NONE = ' '; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Calling command if running in console. |
||
| 28 | */ |
||
| 29 | protected ?Command $callingCommand; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Hashing algorithm in use. |
||
| 33 | */ |
||
| 34 | protected string $hashAlgorithm; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Level of detail. |
||
| 38 | */ |
||
| 39 | protected int $verbosity; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Communicator constructor. |
||
| 43 | */ |
||
| 44 | public function __construct(?Command $callingCommand = null) |
||
| 45 | { |
||
| 46 | $this->callingCommand = $callingCommand; |
||
| 47 | } |
||
| 48 | |||
| 49 | final protected function printBlankLine(): void |
||
| 50 | { |
||
| 51 | $this->tell("\n", 'none'); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Print to console or screen. |
||
| 56 | * |
||
| 57 | * @param string $direction in|out |
||
| 58 | */ |
||
| 59 | final protected function tell(string $text, string $direction = self::COMM_DIRECTION_OUT): void |
||
| 60 | { |
||
| 61 | $direction = strtolower($direction); |
||
| 62 | $nl = app()->runningInConsole() ? "\n" : '<br/>'; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 63 | |||
| 64 | switch ($direction) { |
||
| 65 | case self::COMM_DIRECTION_IN: |
||
| 66 | $dirSymbol = self::COMM_SYMBOL_IN; |
||
| 67 | |||
| 68 | break; |
||
| 69 | case self::COMM_DIRECTION_FLAT: |
||
| 70 | $dirSymbol = self::COMM_SYMBOL_FLAT; |
||
| 71 | |||
| 72 | break; |
||
| 73 | case self::COMM_DIRECTION_OUT: |
||
| 74 | $dirSymbol = self::COMM_SYMBOL_OUT; |
||
| 75 | |||
| 76 | break; |
||
| 77 | default: |
||
| 78 | $dirSymbol = self::COMM_SYMBOL_NONE; |
||
| 79 | } |
||
| 80 | $dirSymbol .= self::COMM_SPACE; |
||
| 81 | |||
| 82 | if ($this->callingCommand !== null && app()->runningInConsole()) { |
||
| 83 | if ($direction === self::COMM_DIRECTION_OUT) { |
||
| 84 | $this->callingCommand->line("<info>\\<\\< {$text}</info>"); |
||
| 85 | } else { |
||
| 86 | $this->callingCommand->line($dirSymbol . $text); |
||
| 87 | } |
||
| 88 | } else { |
||
| 89 | echo $nl . $dirSymbol . $text; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | } |
||
| 93 |