17
17
use Kdyby \Monolog \Processor \TracyUrlProcessor ;
18
18
use Kdyby \Monolog \Tracy \BlueScreenRenderer ;
19
19
use Kdyby \Monolog \Tracy \MonologAdapter ;
20
+ use Nette ;
20
21
use Nette \Configurator ;
21
22
use Nette \DI \Compiler ;
22
- use Nette \DI \Config \Helpers ;
23
23
use Nette \DI \Helpers as DIHelpers ;
24
24
use Nette \DI \Statement ;
25
25
use Nette \PhpGenerator \ClassType as ClassTypeGenerator ;
26
26
use Nette \PhpGenerator \PhpLiteral ;
27
+ use Nette \Schema \Expect ;
28
+ use Nette \Schema \Schema ;
27
29
use Psr \Log \LoggerAwareInterface ;
28
30
use Tracy \Debugger ;
29
31
use Tracy \ILogger ;
@@ -40,28 +42,29 @@ class MonologExtension extends \Nette\DI\CompilerExtension
40
42
const TAG_PROCESSOR = 'monolog.processor ' ;
41
43
const TAG_PRIORITY = 'monolog.priority ' ;
42
44
43
- /**
44
- * @var mixed[]
45
- */
46
- private $ defaults = [
47
- 'handlers ' => [],
48
- 'processors ' => [],
49
- 'name ' => 'app ' ,
50
- 'hookToTracy ' => TRUE ,
51
- 'tracyBaseUrl ' => NULL ,
52
- 'usePriorityProcessor ' => TRUE ,
53
- // 'registerFallback' => TRUE,
54
- 'accessPriority ' => ILogger::INFO ,
55
- ];
45
+ /** @var array */
46
+ protected $ config = [];
47
+
48
+ public function getConfigSchema (): Schema
49
+ {
50
+ return Expect::structure ([
51
+ 'handlers ' => Expect::anyOf (Expect::arrayOf ('Nette\DI\Definitions\Statement ' ), 'false ' )->default ([]),
52
+ 'processors ' => Expect::anyOf (Expect::arrayOf ('Nette\DI\Definitions\Statement ' ), 'false ' )->default ([]),
53
+ 'name ' => Expect::string ('app ' ),
54
+ 'hookToTracy ' => Expect::bool (TRUE ),
55
+ 'tracyBaseUrl ' => Expect::string (),
56
+ 'usePriorityProcessor ' => Expect::bool (TRUE ),
57
+ 'accessPriority ' => Expect::string (ILogger::INFO ),
58
+ 'logDir ' => Expect::string (),
59
+ ])->castTo ('array ' );
60
+ }
56
61
57
62
public function loadConfiguration (): void
58
63
{
59
64
$ builder = $ this ->getContainerBuilder ();
60
-
61
- $ config = $ this ->validateConfig ($ this ->defaults );
62
- $ config ['logDir ' ] = self ::resolveLogDir ($ builder ->parameters );
65
+ $ this ->config ['logDir ' ] = self ::resolveLogDir ($ builder ->parameters );
66
+ $ config = $ this ->config ;
63
67
self ::createDirectory ($ config ['logDir ' ]);
64
- $ this ->setConfig ($ config );
65
68
66
69
if (!isset ($ builder ->parameters [$ this ->name ]) || (is_array ($ builder ->parameters [$ this ->name ]) && !isset ($ builder ->parameters [$ this ->name ]['name ' ]))) {
67
70
$ builder ->parameters [$ this ->name ]['name ' ] = $ config ['name ' ];
@@ -100,6 +103,7 @@ public function loadConfiguration(): void
100
103
101
104
$ this ->loadHandlers ($ config );
102
105
$ this ->loadProcessors ($ config );
106
+ $ this ->setConfig ($ config );
103
107
}
104
108
105
109
protected function loadHandlers (array $ config ): void
@@ -171,17 +175,15 @@ public function beforeCompile(): void
171
175
$ logger ->addSetup ('pushProcessor ' , ['@ ' . $ serviceName ]);
172
176
}
173
177
174
- // This part of code used deprecated methods, that were removed in Nette 3.0
175
- // Rewritten so that the output is same as before
176
- $ originalConfig = Helpers::merge ($ this ->getConfig (), DIHelpers::expand ($ this ->defaults , $ builder ->parameters ));
177
- /** @var array $config */
178
- $ config = Helpers::merge ($ originalConfig , DIHelpers::expand (['registerFallback ' => empty ($ handlers )], $ builder ->parameters ));
178
+ if (empty ($ handlers ) && !array_key_exists ('registerFallback ' , $ this ->config )) {
179
+ $ this ->config ['registerFallback ' ] = TRUE ;
180
+ }
179
181
180
- if (array_key_exists ('registerFallback ' , $ config ) && !empty ($ config ['registerFallback ' ])) {
182
+ if (array_key_exists ('registerFallback ' , $ this -> config ) && !empty ($ this -> config ['registerFallback ' ])) {
181
183
$ logger ->addSetup ('pushHandler ' , [
182
184
new Statement (FallbackNetteHandler::class, [
183
- 'appName ' => $ config ['name ' ],
184
- 'logDir ' => $ config ['logDir ' ],
185
+ 'appName ' => $ this -> config ['name ' ],
186
+ 'logDir ' => $ this -> config ['logDir ' ],
185
187
]),
186
188
]);
187
189
}
@@ -210,7 +212,7 @@ public function afterCompile(ClassTypeGenerator $class): void
210
212
{
211
213
$ initialize = $ class ->getMethod ('initialize ' );
212
214
213
- if (Debugger::$ logDirectory === NULL && isset ( $ this ->config [ ' logDir ' ] )) {
215
+ if (Debugger::$ logDirectory === NULL && array_key_exists ( ' logDir ' , $ this ->config )) {
214
216
$ initialize ->addBody ('?::$logDirectory = ?; ' , [new PhpLiteral (Debugger::class), $ this ->config ['logDir ' ]]);
215
217
}
216
218
}
0 commit comments