-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormMonitorExtension.php
137 lines (114 loc) · 3.25 KB
/
FormMonitorExtension.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
<?php declare(strict_types = 1);
namespace OriNette\Application\FormMonitor\DI;
use Nette\Application\Application;
use Nette\Application\IPresenter;
use Nette\Application\UI\Presenter;
use Nette\DI\CompilerExtension;
use Nette\DI\ContainerBuilder;
use Nette\DI\Definitions\ServiceDefinition;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use OriNette\Application\FormMonitor\FormStack;
use OriNette\Application\FormMonitor\SignalFormExtractor;
use OriNette\Application\FormMonitor\Tracy\FormMonitorPanel;
use stdClass;
use Tracy\Bar;
use function assert;
/**
* @property-read stdClass $config
*/
final class FormMonitorExtension extends CompilerExtension
{
private ServiceDefinition $formStackDefinition;
private ServiceDefinition $signalFormExtractorDefinition;
public function getConfigSchema(): Schema
{
return Expect::structure([
'enabled' => Expect::bool(false),
'debug' => Expect::structure([
'panel' => Expect::bool(false),
]),
]);
}
public function loadConfiguration(): void
{
parent::loadConfiguration();
$builder = $this->getContainerBuilder();
$config = $this->config;
if (!$config->enabled) {
return;
}
$this->registerExtractor($builder);
$this->registerFormStack($builder);
}
private function registerExtractor(ContainerBuilder $builder): void
{
$this->signalFormExtractorDefinition = $builder->addDefinition($this->prefix('signalFormExtractor'))
->setFactory(SignalFormExtractor::class)
->setAutowired(false);
}
private function registerFormStack(ContainerBuilder $builder): void
{
$this->formStackDefinition = $builder->addDefinition($this->prefix('formStack'))
->setFactory(FormStack::class);
}
public function beforeCompile(): void
{
parent::beforeCompile();
$builder = $this->getContainerBuilder();
$config = $this->config;
if (!$config->enabled) {
return;
}
$this->addExtractorToApplication($builder);
$this->addPanelToFormStack($builder, $config);
}
private function addExtractorToApplication(ContainerBuilder $builder): void
{
foreach ($builder->findByType(Application::class) as $applicationDefinition) {
assert($applicationDefinition instanceof ServiceDefinition);
$applicationDefinition->addSetup(
[self::class, 'setupSignalFormExtractor'],
[
$applicationDefinition,
$this->signalFormExtractorDefinition,
],
);
}
}
private function addPanelToFormStack(ContainerBuilder $builder, stdClass $config): void
{
if (!$config->debug->panel) {
return;
}
$this->formStackDefinition->addSetup(
[self::class, 'setupPanel'],
[
"$this->name.panel",
$builder->getDefinitionByType(Bar::class),
$this->formStackDefinition,
],
);
}
public static function setupSignalFormExtractor(Application $application, SignalFormExtractor $extractor): void
{
$application->onPresenter[] = static function (Application $application, IPresenter $presenter) use ($extractor): void {
if ($presenter instanceof Presenter) {
$presenter->onStartup[] = static function () use ($presenter, $extractor): void {
$extractor->extractForm($presenter);
};
}
};
}
public static function setupPanel(
string $name,
Bar $bar,
FormStack $formStack
): void
{
$bar->addPanel(
new FormMonitorPanel($formStack),
$name,
);
}
}