/
DebugBundle.php
180 lines (145 loc) · 5.24 KB
/
DebugBundle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
declare(strict_types=1);
namespace Snicco\Bundle\Debug;
use FilesystemIterator;
use RecursiveDirectoryIterator;
use RuntimeException;
use Snicco\Bundle\Debug\Displayer\WhoopsHtmlDisplayer;
use Snicco\Bundle\Debug\Displayer\WhoopsJsonDisplayer;
use Snicco\Bundle\Debug\Option\DebugOption;
use Snicco\Bundle\HttpRouting\Option\HttpErrorHandlingOption;
use Snicco\Component\Kernel\Bundle;
use Snicco\Component\Kernel\Configuration\WritableConfig;
use Snicco\Component\Kernel\Kernel;
use Snicco\Component\Kernel\ValueObject\Directories;
use Snicco\Component\Kernel\ValueObject\Environment;
use SplFileInfo;
use Whoops\Handler\JsonResponseHandler;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Run;
use function array_merge;
use function copy;
use function dirname;
use function is_file;
use function sprintf;
final class DebugBundle implements Bundle
{
/**
* @var string
*/
public const ALIAS = 'snicco/debug-bundle';
public function shouldRun(Environment $env): bool
{
if (! $env->isDevelop()) {
return false;
}
return $env->isDebug();
}
public function configure(WritableConfig $config, Kernel $kernel): void
{
if ($kernel->usesBundle('snicco/http-routing-bundle')) {
$this->configureHttpRouting($config, $kernel);
}
}
public function register(Kernel $kernel): void
{
if ($kernel->usesBundle('snicco/http-routing-bundle')) {
$this->registerHttpDebugServices($kernel);
}
}
public function bootstrap(Kernel $kernel): void
{
}
public function alias(): string
{
return self::ALIAS;
}
private function configureHttpRouting(WritableConfig $config, Kernel $kernel): void
{
$config->prependToList('http_error_handling.' . HttpErrorHandlingOption::DISPLAYERS, [
WhoopsJsonDisplayer::class,
WhoopsHtmlDisplayer::class,
]);
$defaults = require dirname(__DIR__) . '/config/debug.php';
$defaults = array_merge($defaults, [
DebugOption::APPLICATION_PATHS => $this->allDirectoriesExpectVendor($kernel->directories()),
]);
$config->mergeDefaults('debug', $defaults);
$this->copyConfiguration($kernel);
}
private function registerHttpDebugServices(Kernel $kernel): void
{
// private
$kernel->container()
->factory(Run::class, function (): Run {
$whoops = new Run();
$whoops->allowQuit(false);
$whoops->writeToOutput(false);
return $whoops;
});
$kernel->container()
->shared(PrettyPageHandler::class, function () use ($kernel): FilterablePrettyPageHandler {
$handler = new FilterablePrettyPageHandler();
$handler->handleUnconditionally(true);
$handler->setEditor($kernel->config()->getString('debug.' . DebugOption::EDITOR));
$handler->setApplicationRootPath($kernel->directories()->baseDir());
$handler->setApplicationPaths(
$kernel->config()
->getListOfStrings('debug.' . DebugOption::APPLICATION_PATHS)
);
return $handler;
});
$kernel->container()
->shared(WhoopsHtmlDisplayer::class, function () use ($kernel): WhoopsHtmlDisplayer {
$whoops = $kernel->container()
->make(Run::class);
$whoops->pushHandler($kernel->container()->make(PrettyPageHandler::class));
return new WhoopsHtmlDisplayer($whoops);
});
$kernel->container()
->shared(WhoopsJsonDisplayer::class, function () use ($kernel): WhoopsJsonDisplayer {
$whoops = $kernel->container()
->make(Run::class);
$handler = new JsonResponseHandler();
$handler->addTraceToOutput(true);
$handler->setJsonApi(true);
$whoops->pushHandler($handler);
return new WhoopsJsonDisplayer($whoops);
});
}
/**
* @return list<string>
*/
private function allDirectoriesExpectVendor(Directories $dirs): array
{
$iterator = new RecursiveDirectoryIterator($dirs->baseDir(), FilesystemIterator::SKIP_DOTS);
$dirs = [];
/**
* @var string $name
* @var SplFileInfo $file_info
*/
foreach ($iterator as $name => $file_info) {
if ($file_info->isDir() && 'vendor' !== $file_info->getBasename()) {
$dirs[] = $name;
}
}
return $dirs;
}
private function copyConfiguration(Kernel $kernel): void
{
$destination = $kernel->directories()
->configDir() . '/debug.php';
if (is_file($destination)) {
return;
}
$copied = copy(dirname(__DIR__) . '/config/debug.php', $destination);
if (! $copied) {
// @codeCoverageIgnoreStart
throw new RuntimeException(sprintf(
'Could not copy the default templating config to destination [%s]',
$destination
));
// @codeCoverageIgnoreEnd
}
}
}