12
12
use Composer \Autoload \ClassLoader ;
13
13
use Nette ;
14
14
use Nette \Application \UI ;
15
+ use Nette \DI \Config \Expect ;
16
+ use Nette \DI \Definitions ;
15
17
use Tracy ;
16
18
17
19
20
22
*/
21
23
final class ApplicationExtension extends Nette \DI \CompilerExtension
22
24
{
23
- private $ defaults = [
24
- 'debugger ' => null ,
25
- 'errorPresenter ' => 'Nette:Error ' ,
26
- 'catchExceptions ' => null ,
27
- 'mapping ' => null ,
28
- 'scanDirs ' => [],
29
- 'scanComposer ' => null ,
30
- 'scanFilter ' => 'Presenter ' ,
31
- 'silentLinks ' => false ,
32
- ];
33
-
34
25
/** @var bool */
35
26
private $ debugMode ;
36
27
28
+ /** @var array */
29
+ private $ scanDirs ;
30
+
37
31
/** @var int */
38
32
private $ invalidLinkMode ;
39
33
@@ -43,48 +37,60 @@ final class ApplicationExtension extends Nette\DI\CompilerExtension
43
37
44
38
public function __construct (bool $ debugMode = false , array $ scanDirs = null , string $ tempDir = null )
45
39
{
46
- $ this ->defaults ['debugger ' ] = interface_exists (Tracy \IBarPanel::class);
47
- $ this ->defaults ['scanDirs ' ] = (array ) $ scanDirs ;
48
- $ this ->defaults ['scanComposer ' ] = class_exists (ClassLoader::class);
49
- $ this ->defaults ['catchExceptions ' ] = !$ debugMode ;
50
40
$ this ->debugMode = $ debugMode ;
41
+ $ this ->scanDirs = (array ) $ scanDirs ;
51
42
$ this ->tempDir = $ tempDir ;
52
43
}
53
44
54
45
46
+ public function getConfigSchema (): Nette \DI \Config \Schema
47
+ {
48
+ return Expect::structure ([
49
+ 'debugger ' => Expect::bool (interface_exists (Tracy \IBarPanel::class)),
50
+ 'errorPresenter ' => Expect::string ('Nette:Error ' )->dynamic (),
51
+ 'catchExceptions ' => Expect::bool (!$ this ->debugMode )->dynamic (),
52
+ 'mapping ' => Expect::arrayOf ('string ' )->dynamic (),
53
+ 'scanDirs ' => Expect::enum (Expect::arrayOf ('string ' )->default ($ this ->scanDirs ), false ),
54
+ 'scanComposer ' => Expect::bool (class_exists (ClassLoader::class)),
55
+ 'scanFilter ' => Expect::string ('Presenter ' ),
56
+ 'silentLinks ' => Expect::bool (),
57
+ ]);
58
+ }
59
+
60
+
55
61
public function loadConfiguration ()
56
62
{
57
- $ config = $ this ->validateConfig ( $ this -> defaults ) ;
63
+ $ config = $ this ->config ;
58
64
$ builder = $ this ->getContainerBuilder ();
59
65
$ builder ->addExcludedClasses ([UI \Presenter::class]);
60
66
61
67
$ this ->invalidLinkMode = $ this ->debugMode
62
- ? UI \Presenter::INVALID_LINK_TEXTUAL | ($ config[ ' silentLinks ' ] ? 0 : UI \Presenter::INVALID_LINK_WARNING )
68
+ ? UI \Presenter::INVALID_LINK_TEXTUAL | ($ config-> silentLinks ? 0 : UI \Presenter::INVALID_LINK_WARNING )
63
69
: UI \Presenter::INVALID_LINK_WARNING ;
64
70
65
71
$ application = $ builder ->addDefinition ($ this ->prefix ('application ' ))
66
72
->setFactory (Nette \Application \Application::class)
67
- ->addSetup ('$catchExceptions ' , [$ config[ ' catchExceptions ' ] ])
68
- ->addSetup ('$errorPresenter ' , [$ config[ ' errorPresenter ' ] ]);
73
+ ->addSetup ('$catchExceptions ' , [$ config-> catchExceptions ])
74
+ ->addSetup ('$errorPresenter ' , [$ config-> errorPresenter ]);
69
75
70
- if ($ config[ ' debugger ' ] ) {
76
+ if ($ config-> debugger ) {
71
77
$ application ->addSetup ([Nette \Bridges \ApplicationTracy \RoutingPanel::class, 'initializePanel ' ]);
72
78
}
73
79
74
- $ touch = $ this ->debugMode && $ config[ ' scanDirs ' ] && $ this ->tempDir ? $ this ->tempDir . '/touch ' : null ;
80
+ $ touch = $ this ->debugMode && $ config-> scanDirs && $ this ->tempDir ? $ this ->tempDir . '/touch ' : null ;
75
81
$ presenterFactory = $ builder ->addDefinition ($ this ->prefix ('presenterFactory ' ))
76
82
->setType (Nette \Application \IPresenterFactory::class)
77
- ->setFactory (Nette \Application \PresenterFactory::class, [new Nette \ DI \ Definitions \Statement (
83
+ ->setFactory (Nette \Application \PresenterFactory::class, [new Definitions \Statement (
78
84
Nette \Bridges \ApplicationDI \PresenterFactoryCallback::class, [1 => $ this ->invalidLinkMode , $ touch ]
79
85
)]);
80
86
81
- if ($ config[ ' mapping ' ] ) {
82
- $ presenterFactory ->addSetup ('setMapping ' , [$ config[ ' mapping ' ] ]);
87
+ if ($ config-> mapping ) {
88
+ $ presenterFactory ->addSetup ('setMapping ' , [$ config-> mapping ]);
83
89
}
84
90
85
91
$ builder ->addDefinition ($ this ->prefix ('linkGenerator ' ))
86
92
->setFactory (Nette \Application \LinkGenerator::class, [
87
- 1 => new Nette \ DI \ Definitions \Statement ('@Nette\Http\IRequest::getUrl ' ),
93
+ 1 => new Definitions \Statement ('@Nette\Http\IRequest::getUrl ' ),
88
94
]);
89
95
90
96
if ($ this ->name === 'application ' ) {
@@ -115,7 +121,7 @@ public function beforeCompile()
115
121
$ def ->addTag (Nette \DI \Extensions \InjectExtension::TAG_INJECT )
116
122
->setAutowired (false );
117
123
118
- if (is_subclass_of ($ def ->getType (), UI \Presenter::class)) {
124
+ if (is_subclass_of ($ def ->getType (), UI \Presenter::class) && $ def instanceof Definitions \ServiceDefinition ) {
119
125
$ def ->addSetup ('$invalidLinkMode ' , [$ this ->invalidLinkMode ]);
120
126
}
121
127
}
@@ -127,13 +133,13 @@ private function findPresenters(): array
127
133
$ config = $ this ->getConfig ();
128
134
$ classes = [];
129
135
130
- if ($ config[ ' scanDirs ' ] ) {
136
+ if ($ config-> scanDirs ) {
131
137
if (!class_exists (Nette \Loaders \RobotLoader::class)) {
132
138
throw new Nette \NotSupportedException ("RobotLoader is required to find presenters, install package `nette/robot-loader` or disable option {$ this ->prefix ('scanDirs ' )}: false " );
133
139
}
134
140
$ robot = new Nette \Loaders \RobotLoader ;
135
- $ robot ->addDirectory (...$ config[ ' scanDirs ' ] );
136
- $ robot ->acceptFiles = ['* ' . $ config[ ' scanFilter ' ] . '*.php ' ];
141
+ $ robot ->addDirectory (...$ config-> scanDirs );
142
+ $ robot ->acceptFiles = ['* ' . $ config-> scanFilter . '*.php ' ];
137
143
if ($ this ->tempDir ) {
138
144
$ robot ->setTempDirectory ($ this ->tempDir );
139
145
$ robot ->refresh ();
@@ -144,7 +150,7 @@ private function findPresenters(): array
144
150
$ this ->getContainerBuilder ()->addDependency ($ this ->tempDir . '/touch ' );
145
151
}
146
152
147
- if ($ config[ ' scanComposer ' ] ) {
153
+ if ($ config-> scanComposer ) {
148
154
$ rc = new \ReflectionClass (ClassLoader::class);
149
155
$ classFile = dirname ($ rc ->getFileName ()) . '/autoload_classmap.php ' ;
150
156
if (is_file ($ classFile )) {
@@ -158,7 +164,7 @@ private function findPresenters(): array
158
164
$ presenters = [];
159
165
foreach (array_unique ($ classes ) as $ class ) {
160
166
if (
161
- strpos ($ class , $ config[ ' scanFilter ' ] ) !== false
167
+ strpos ($ class , $ config-> scanFilter ) !== false
162
168
&& class_exists ($ class )
163
169
&& ($ rc = new \ReflectionClass ($ class ))
164
170
&& $ rc ->implementsInterface (Nette \Application \IPresenter::class)
0 commit comments