Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
20 / 20 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
BuildWarnings | |
100.00% |
20 / 20 |
|
100.00% |
9 / 9 |
15 | |
100.00% |
1 / 1 |
getInstance | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
report | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
getWarnings | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasWarnings | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
reportsWarnings | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
reportsWarningsAsExceptions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
writeWarningsToOutput | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
renderWarnings | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
renderWarningsAsExceptions | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Hyde\Support; |
6 | |
7 | use Hyde\Facades\Config; |
8 | use Hyde\Framework\Exceptions\BuildWarning; |
9 | use Illuminate\Container\Container; |
10 | use Illuminate\Contracts\Debug\ExceptionHandler; |
11 | use Symfony\Component\Console\Style\OutputStyle; |
12 | |
13 | use function app; |
14 | use function sprintf; |
15 | |
16 | /** |
17 | * @experimental |
18 | */ |
19 | class BuildWarnings |
20 | { |
21 | /** @var array<\Hyde\Framework\Exceptions\BuildWarning> */ |
22 | protected array $warnings = []; |
23 | |
24 | public static function getInstance(): self |
25 | { |
26 | $app = Container::getInstance(); |
27 | |
28 | if (! $app->bound(self::class)) { |
29 | $app->singleton(self::class); |
30 | } |
31 | |
32 | /** @var \Hyde\Support\BuildWarnings $instance */ |
33 | $instance = $app->make(self::class); |
34 | |
35 | return $instance; |
36 | } |
37 | |
38 | public static function report(BuildWarning|string $warning): void |
39 | { |
40 | static::getInstance()->warnings[] = $warning instanceof BuildWarning ? $warning : new BuildWarning($warning); |
41 | } |
42 | |
43 | /** @return array<int, \Hyde\Framework\Exceptions\BuildWarning> */ |
44 | public static function getWarnings(): array |
45 | { |
46 | return static::getInstance()->warnings; |
47 | } |
48 | |
49 | public static function hasWarnings(): bool |
50 | { |
51 | return count(static::getInstance()->warnings) > 0; |
52 | } |
53 | |
54 | public static function reportsWarnings(): bool |
55 | { |
56 | return Config::getBool('hyde.log_warnings', true); |
57 | } |
58 | |
59 | public static function reportsWarningsAsExceptions(): bool |
60 | { |
61 | return Config::getBool('hyde.convert_build_warnings_to_exceptions', false); |
62 | } |
63 | |
64 | public static function writeWarningsToOutput(OutputStyle $output, bool $verbose = false): void |
65 | { |
66 | if (static::reportsWarningsAsExceptions()) { |
67 | self::renderWarningsAsExceptions($output); |
68 | } else { |
69 | self::renderWarnings($output, $verbose); |
70 | } |
71 | } |
72 | |
73 | protected static function renderWarnings(OutputStyle $output, bool $verbose): void |
74 | { |
75 | foreach (static::getWarnings() as $number => $warning) { |
76 | $output->writeln(sprintf(' %s. <comment>%s</comment>', $number + 1, $warning->getMessage())); |
77 | if ($verbose) { |
78 | $output->writeln(sprintf(' <fg=gray>%s:%s</>', $warning->getFile(), $warning->getLine())); |
79 | } |
80 | } |
81 | } |
82 | |
83 | protected static function renderWarningsAsExceptions(OutputStyle $output): void |
84 | { |
85 | /** @var ExceptionHandler $handler */ |
86 | $handler = app(ExceptionHandler::class); |
87 | |
88 | foreach (static::getWarnings() as $warning) { |
89 | $handler->renderForConsole($output, $warning); |
90 | } |
91 | } |
92 | } |