@@ -76,13 +76,13 @@ public function loadConfiguration()
76
76
}
77
77
78
78
79
- public function afterCompile ( Nette \ PhpGenerator \ ClassType $ class )
79
+ public function beforeCompile ( )
80
80
{
81
81
if ($ this ->cliMode ) {
82
82
return ;
83
83
}
84
84
85
- $ initialize = $ class -> getMethod ( ' initialize ' );
85
+ $ builder = $ this -> getContainerBuilder ( );
86
86
$ config = $ this ->config ;
87
87
$ headers = array_map ('strval ' , $ config ->headers );
88
88
@@ -96,16 +96,14 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
96
96
$ headers ['X-Frame-Options ' ] = $ frames ;
97
97
}
98
98
99
- $ code = [];
100
99
foreach (['csp ' , 'cspReportOnly ' ] as $ key ) {
101
100
if (empty ($ config ->$ key )) {
102
101
continue ;
103
102
}
104
103
$ value = self ::buildPolicy ($ config ->$ key );
105
104
if (strpos ($ value , "'nonce' " )) {
106
- $ code [0 ] = '$cspNonce = base64_encode(random_bytes(16)); ' ;
107
105
$ value = Nette \DI \ContainerBuilder::literal (
108
- 'str_replace(?, ? . $cspNonce, ?) ' ,
106
+ 'str_replace(?, ? . (isset( $cspNonce) \? $cspNonce : $cspNonce = base64_encode(random_bytes(16))) , ?) ' ,
109
107
["'nonce " , "'nonce- " , $ value ]
110
108
);
111
109
}
@@ -116,16 +114,16 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
116
114
$ headers ['Feature-Policy ' ] = self ::buildPolicy ($ config ->featurePolicy );
117
115
}
118
116
119
- $ code [] = Helpers::formatArgs ('$response = $this->getService(?); ' , [$ this ->prefix ('response ' )]);
117
+ $ response = $ builder ->getDefinition ($ this ->prefix ('response ' ));
118
+ assert ($ response instanceof Nette \DI \Definitions \ServiceDefinition);
119
+
120
120
foreach ($ headers as $ key => $ value ) {
121
121
if ($ value !== '' ) {
122
- $ code [] = Helpers:: formatArgs ( ' $response ->setHeader(?, ?); ' , [$ key , $ value ]);
122
+ $ response -> addSetup ( ' ? ->setHeader(?, ?); ' , [' @self ' , $ key , $ value ]);
123
123
}
124
124
}
125
125
126
- $ code [] = Helpers::formatArgs ('$response->setCookie(...?); ' , [['nette-samesite ' , '1 ' , 0 , '/ ' , null , null , true , 'Strict ' ]]);
127
-
128
- $ initialize ->addBody ("(function () { \n\t" . implode ("\n\t" , $ code ) . "\n})(); " );
126
+ $ response ->addSetup ('?->setCookie(...?) ' , ['@self ' , ['nette-samesite ' , '1 ' , 0 , '/ ' , null , null , true , 'Strict ' ]]);
129
127
}
130
128
131
129
0 commit comments