-
-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathApplicationExtension.php
More file actions
222 lines (182 loc) · 6.41 KB
/
ApplicationExtension.php
File metadata and controls
222 lines (182 loc) · 6.41 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\Bridges\ApplicationDI;
use Composer\Autoload\ClassLoader;
use Nette;
use Nette\Application\UI;
use Nette\DI\Definitions;
use Nette\Schema\Expect;
use Tracy;
/**
* Application extension for Nette DI.
*/
final class ApplicationExtension extends Nette\DI\CompilerExtension
{
/** @var bool */
private $debugMode;
/** @var array */
private $scanDirs;
/** @var Nette\Loaders\RobotLoader|null */
private $robotLoader;
/** @var int */
private $invalidLinkMode;
/** @var string|null */
private $tempDir;
public function __construct(
bool $debugMode = false,
array $scanDirs = null,
string $tempDir = null,
Nette\Loaders\RobotLoader $robotLoader = null
) {
$this->debugMode = $debugMode;
$this->scanDirs = (array) $scanDirs;
$this->tempDir = $tempDir;
$this->robotLoader = $robotLoader;
}
public function getConfigSchema(): Nette\Schema\Schema
{
return Expect::structure([
'debugger' => Expect::bool(),
'errorPresenter' => Expect::string('Nette:Error')->dynamic(),
'catchExceptions' => Expect::bool()->dynamic(),
'mapping' => Expect::arrayOf('string|array'),
'scanDirs' => Expect::anyOf(
Expect::arrayOf('string')->default($this->scanDirs)->mergeDefaults(),
false
)->default($this->scanDirs),
'scanComposer' => Expect::bool(class_exists(ClassLoader::class)),
'scanFilter' => Expect::string('*Presenter'),
'silentLinks' => Expect::bool(),
]);
}
public function loadConfiguration()
{
$config = $this->config;
$builder = $this->getContainerBuilder();
$builder->addExcludedClasses([UI\Presenter::class]);
$this->invalidLinkMode = $this->debugMode
? UI\Presenter::INVALID_LINK_TEXTUAL | ($config->silentLinks ? 0 : UI\Presenter::INVALID_LINK_WARNING)
: UI\Presenter::INVALID_LINK_WARNING;
$builder->addDefinition($this->prefix('application'))
->setFactory(Nette\Application\Application::class)
->addSetup('$catchExceptions', [$this->debugMode ? $config->catchExceptions : true])
->addSetup('$errorPresenter', [$config->errorPresenter]);
$this->compiler->addExportedType(Nette\Application\Application::class);
if ($this->debugMode && ($config->scanDirs || $this->robotLoader) && $this->tempDir) {
$touch = $this->tempDir . '/touch';
Nette\Utils\FileSystem::createDir($this->tempDir);
$this->getContainerBuilder()->addDependency($touch);
}
$presenterFactory = $builder->addDefinition($this->prefix('presenterFactory'))
->setType(Nette\Application\IPresenterFactory::class)
->setFactory(Nette\Application\PresenterFactory::class, [new Definitions\Statement(
Nette\Bridges\ApplicationDI\PresenterFactoryCallback::class,
[1 => $this->invalidLinkMode, $touch ?? null]
)]);
if ($config->mapping) {
$presenterFactory->addSetup('setMapping', [$config->mapping]);
}
$builder->addDefinition($this->prefix('linkGenerator'))
->setType(Nette\Application\ILinkGenerator::class)
->setFactory(Nette\Application\DefaultLinkGenerator::class, [
1 => new Definitions\Statement([new Definitions\Statement('@Nette\Http\IRequest::getUrl'), 'withoutUserInfo']),
]);
if ($this->name === 'application') {
$builder->addAlias('application', $this->prefix('application'));
$builder->addAlias('nette.presenterFactory', $this->prefix('presenterFactory'));
}
}
public function beforeCompile()
{
$builder = $this->getContainerBuilder();
if ($this->config->debugger ?? $builder->getByType(Tracy\BlueScreen::class)) {
$builder->getDefinition($this->prefix('application'))
->addSetup([self::class, 'initializeBlueScreenPanel']);
}
$all = [];
foreach ($builder->findByType(Nette\Application\IPresenter::class) as $def) {
$all[$def->getType()] = $def;
}
$counter = 0;
foreach ($this->findPresenters() as $class) {
if (empty($all[$class])) {
$all[$class] = $builder->addDefinition($this->prefix((string) ++$counter))
->setType($class);
}
}
foreach ($all as $def) {
$def->addTag(Nette\DI\Extensions\InjectExtension::TAG_INJECT)
->setAutowired(false);
if (is_subclass_of($def->getType(), UI\Presenter::class) && $def instanceof Definitions\ServiceDefinition) {
$def->addSetup('$invalidLinkMode', [$this->invalidLinkMode]);
}
$this->compiler->addExportedType($def->getType());
}
}
private function findPresenters(): array
{
$config = $this->getConfig();
if ($config->scanDirs) {
if (!class_exists(Nette\Loaders\RobotLoader::class)) {
throw new Nette\NotSupportedException("RobotLoader is required to find presenters, install package `nette/robot-loader` or disable option {$this->prefix('scanDirs')}: false");
}
$robot = new Nette\Loaders\RobotLoader;
$robot->addDirectory(...$config->scanDirs);
$robot->acceptFiles = [$config->scanFilter . '.php'];
if ($this->tempDir) {
$robot->setTempDirectory($this->tempDir);
$robot->refresh();
} else {
$robot->rebuild();
}
} elseif ($this->robotLoader && $config->scanDirs !== false) {
$robot = $this->robotLoader;
$robot->refresh();
}
$classes = [];
if (isset($robot)) {
$classes = array_keys($robot->getIndexedClasses());
}
if ($config->scanComposer) {
$rc = new \ReflectionClass(ClassLoader::class);
$classFile = dirname($rc->getFileName()) . '/autoload_classmap.php';
if (is_file($classFile)) {
$this->getContainerBuilder()->addDependency($classFile);
$classes = array_merge($classes, array_keys((function ($path) {
return require $path;
})($classFile)));
}
}
$presenters = [];
foreach (array_unique($classes) as $class) {
if (
fnmatch($config->scanFilter, $class)
&& class_exists($class)
&& ($rc = new \ReflectionClass($class))
&& $rc->implementsInterface(Nette\Application\IPresenter::class)
&& !$rc->isAbstract()
) {
$presenters[] = $rc->getName();
}
}
return $presenters;
}
/** @internal */
public static function initializeBlueScreenPanel(
Tracy\BlueScreen $blueScreen,
Nette\Application\Application $application
): void {
$blueScreen->addPanel(function (?\Throwable $e) use ($application, $blueScreen): ?array {
$dumper = $blueScreen->getDumper();
return $e ? null : [
'tab' => 'Nette Application',
'panel' => '<h3>Requests</h3>' . $dumper($application->getRequests())
. '<h3>Presenter</h3>' . $dumper($application->getPresenter()),
];
});
}
}