|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Cerbero\Enum; |
| 6 | + |
| 7 | +use Closure; |
| 8 | +use Throwable; |
| 9 | + |
| 10 | +/** |
| 11 | + * Print out the given success message. |
| 12 | + */ |
| 13 | +function succeed(string $message): bool |
| 14 | +{ |
| 15 | + fwrite(STDOUT, "\e[38;2;38;220;38m{$message}\e[0m" . PHP_EOL); |
| 16 | + |
| 17 | + return true; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Print out the given error message. |
| 22 | + */ |
| 23 | +function fail(string $message): bool |
| 24 | +{ |
| 25 | + fwrite(STDERR, "\e[38;2;220;38;38m{$message}\e[0m" . PHP_EOL); |
| 26 | + |
| 27 | + return false; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Split the given argv into arguments and options. |
| 32 | + * |
| 33 | + * @param string[] $argv |
| 34 | + * @return list<string[]> |
| 35 | + */ |
| 36 | +function splitArgv(array $argv): array |
| 37 | +{ |
| 38 | + $arguments = $options = []; |
| 39 | + |
| 40 | + foreach (array_slice($argv, 2) as $item) { |
| 41 | + if (str_starts_with($item, '-')) { |
| 42 | + $options[] = $item; |
| 43 | + } else { |
| 44 | + $arguments[] = $item; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return [$arguments, $options]; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Set enum paths from the given options. |
| 53 | + * |
| 54 | + * @param string[] $options |
| 55 | + */ |
| 56 | +function setPathsByOptions(array $options): void |
| 57 | +{ |
| 58 | + if ($basePath = option('base-path', $options)) { |
| 59 | + Enums::setBasePath($basePath); |
| 60 | + } |
| 61 | + |
| 62 | + if ($paths = option('paths', $options)) { |
| 63 | + Enums::setPaths(...explode(',', $paths)); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Retrieve the value of the given option. |
| 69 | + * |
| 70 | + * @param string[] $options |
| 71 | + */ |
| 72 | +function option(string $name, array $options): ?string |
| 73 | +{ |
| 74 | + $prefix = "--{$name}="; |
| 75 | + |
| 76 | + foreach ($options as $option) { |
| 77 | + if (str_starts_with($option, $prefix)) { |
| 78 | + $segments = explode('=', $option, limit: 2); |
| 79 | + |
| 80 | + return $segments[1] === '' ? null : $segments[1]; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return null; |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Retrieve the normalized namespaces of the given enums. |
| 89 | + * |
| 90 | + * @param list<string> $enums |
| 91 | + * @return list<class-string<\UnitEnum>> |
| 92 | + */ |
| 93 | +function normalizeEnums(array $enums): array |
| 94 | +{ |
| 95 | + $namespaces = array_map(fn(string $enum) => strtr($enum, '/', '\\'), $enums); |
| 96 | + |
| 97 | + return array_unique(array_filter($namespaces, 'enum_exists')); |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * Print out the outcome of the given enum operation. |
| 102 | + * |
| 103 | + * @param class-string<\UnitEnum> $namespace |
| 104 | + * @param Closure(): bool $callback |
| 105 | + */ |
| 106 | +function enumOutcome(string $enum, Closure $callback): bool |
| 107 | +{ |
| 108 | + $error = null; |
| 109 | + |
| 110 | + try { |
| 111 | + $succeeded = $callback(); |
| 112 | + } catch (Throwable $e) { |
| 113 | + $succeeded = false; |
| 114 | + $error = "\e[38;2;220;38;38m{$e?->getMessage()}\e[0m"; |
| 115 | + } |
| 116 | + |
| 117 | + if ($succeeded) { |
| 118 | + fwrite(STDOUT, "\e[48;2;163;230;53m\e[38;2;63;98;18m\e[1m DONE \e[0m {$enum}" . PHP_EOL . PHP_EOL); |
| 119 | + } else { |
| 120 | + fwrite(STDERR, "\e[48;2;248;113;113m\e[38;2;153;27;27m\e[1m FAIL \e[0m {$enum} {$error}" . PHP_EOL . PHP_EOL); |
| 121 | + } |
| 122 | + |
| 123 | + return $succeeded; |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * Annotate the given enum within a new process. |
| 128 | + * |
| 129 | + * @param class-string<\UnitEnum> $enum |
| 130 | + */ |
| 131 | +function runAnnotate(string $enum, bool $force = false): bool |
| 132 | +{ |
| 133 | + // Once an enum is loaded, PHP accesses it from the memory and not from the disk. |
| 134 | + // Since we are writing on the disk, the enum in memory might get out of sync. |
| 135 | + // To ensure that the annotations reflect the current content of such enum, |
| 136 | + // we spin a new process to load in memory the latest state of the enum. |
| 137 | + ob_start(); |
| 138 | + |
| 139 | + $succeeded = cli("annotate \"{$enum}\"" . ($force ? ' --force' : '')); |
| 140 | + |
| 141 | + ob_end_clean(); |
| 142 | + |
| 143 | + return $succeeded; |
| 144 | +} |
| 145 | + |
| 146 | +/** |
| 147 | + * Run the enum CLI in a new process. |
| 148 | + */ |
| 149 | +function cli(string $command, ?int &$status = null): bool |
| 150 | +{ |
| 151 | + $cmd = vsprintf('"%s" "%s" %s 2>&1', [ |
| 152 | + PHP_BINARY, |
| 153 | + path(__DIR__ . '/../bin/enum'), |
| 154 | + $command, |
| 155 | + ]); |
| 156 | + |
| 157 | + return passthru($cmd, $status) === null; |
| 158 | +} |
| 159 | + |
| 160 | +/** |
| 161 | + * Synchronize the given enum in TypeScript within a new process. |
| 162 | + * |
| 163 | + * @param class-string<\UnitEnum> $enum |
| 164 | + */ |
| 165 | +function runTs(string $enum, bool $force = false): bool |
| 166 | +{ |
| 167 | + // Once an enum is loaded, PHP accesses it from the memory and not from the disk. |
| 168 | + // Since we are writing on the disk, the enum in memory might get out of sync. |
| 169 | + // To make sure that we are synchronizing the current content of such enum, |
| 170 | + // we spin a new process to load in memory the latest state of the enum. |
| 171 | + ob_start(); |
| 172 | + |
| 173 | + $succeeded = cli("ts \"{$enum}\"" . ($force ? ' --force' : '')); |
| 174 | + |
| 175 | + ob_end_clean(); |
| 176 | + |
| 177 | + return $succeeded; |
| 178 | +} |
0 commit comments