• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tempestphp / tempest-framework / 11924655642

19 Nov 2024 12:37PM UTC coverage: 81.705% (-0.2%) from 81.946%
11924655642

push

github

web-flow
chore(core): framework installer improvements (#752)

1 of 17 new or added lines in 2 files covered. (5.88%)

56 existing lines in 5 files now uncovered.

7869 of 9631 relevant lines covered (81.7%)

51.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

29.03
/src/Tempest/Debug/src/Debug.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\Debug;
6

7
use Symfony\Component\VarDumper\Cloner\VarCloner;
8
use Symfony\Component\VarDumper\Dumper\CliDumper;
9
use Symfony\Component\VarDumper\VarDumper;
10
use Tempest\Container\GenericContainer;
11
use Tempest\Highlight\Themes\TerminalStyle;
12
use Tempest\Log\LogConfig;
13

14
final readonly class Debug
15
{
16
    private function __construct(private ?LogConfig $logConfig = null)
4✔
17
    {
18
    }
4✔
19

20
    public static function resolve(): self
4✔
21
    {
22
        if (! class_exists(GenericContainer::class)) {
4✔
23
            return new self();
×
24
        }
25

26
        if (! class_exists(LogConfig::class)) {
4✔
27
            return new self();
×
28
        }
29

30
        $container = GenericContainer::instance();
4✔
31

32
        $logConfig = $container?->get(LogConfig::class);
4✔
33

34
        return new self($logConfig);
4✔
35
    }
36

37
    public function log(array $items, bool $writeToLog = true, bool $writeToOut = true): void
4✔
38
    {
39
        $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
4✔
40
        $callPath = $trace[1]['file'] . ':' . $trace[1]['line'];
4✔
41

42
        if ($writeToLog) {
4✔
43
            $this->writeToLog($items, $callPath);
4✔
44
        }
45

46
        if ($writeToOut) {
4✔
47
            $this->writeToOut($items, $callPath);
×
48
        }
49
    }
50

51
    private function writeToLog(array $items, string $callPath): void
4✔
52
    {
53
        if ($this->logConfig === null) {
4✔
54
            return;
×
55
        }
56

57
        if (! $this->logConfig->debugLogPath) {
4✔
58
            return;
4✔
59
        }
60

61
        $directory = dirname($this->logConfig->debugLogPath);
×
62

63
        if (! is_dir($directory)) {
×
64
            mkdir(directory: $directory, recursive: true);
×
65
        }
66

67
        $handle = @fopen($this->logConfig->debugLogPath, 'a');
×
68

UNCOV
69
        if (! $handle) {
×
70
            return;
×
71
        }
72

73
        foreach ($items as $key => $item) {
×
UNCOV
74
            $output = $this->createDump($item) . $callPath;
×
75

76
            fwrite($handle, "{$key} " . $output . PHP_EOL);
×
77
        }
78

79
        fclose($handle);
×
80
    }
81

82
    private function writeToOut(array $items, string $callPath): void
×
83
    {
84
        foreach ($items as $key => $item) {
×
UNCOV
85
            if (defined('STDOUT')) {
×
86
                fwrite(STDOUT, TerminalStyle::BG_BLUE(" {$key} ") . ' ');
×
87

88
                $output = $this->createDump($item);
×
89

UNCOV
90
                fwrite(STDOUT, $output);
×
91

UNCOV
92
                fwrite(STDOUT, $callPath . PHP_EOL);
×
93
            } else {
UNCOV
94
                echo sprintf(
×
UNCOV
95
                    '<span style="
×
96
                    display:inline-block; 
97
                    color: #fff; 
98
                    font-family: %s;
99
                    padding: 2px 4px;
100
                    font-size: 0.8rem;
101
                    margin-bottom: -12px;
102
                    background: #0071BC;"
103
                >%s (%s)</span>',
×
UNCOV
104
                    'Source Code Pro, ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace',
×
UNCOV
105
                    $key,
×
UNCOV
106
                    $callPath,
×
UNCOV
107
                );
×
108

UNCOV
109
                VarDumper::dump($item);
×
110
            }
111
        }
112
    }
113

114
    private function createDump(mixed $input): string
×
115
    {
116
        $cloner = new VarCloner();
×
117

UNCOV
118
        $output = '';
×
119

120
        $dumper = new CliDumper(function ($line, $depth) use (&$output): void {
×
UNCOV
121
            if ($depth < 0) {
×
122
                return;
×
123
            }
124

UNCOV
125
            $output .= str_repeat(' ', $depth) . $line . "\n";
×
126
        });
×
127

128
        $dumper->setColors(true);
×
129

130
        $dumper->dump($cloner->cloneVar($input));
×
131

UNCOV
132
        return preg_replace(
×
UNCOV
133
            pattern: '/\e](.*)\e]8;;\e/',
×
UNCOV
134
            replacement: '',
×
UNCOV
135
            subject: $output,
×
UNCOV
136
        );
×
137
    }
138
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc