10
10
namespace Nette \Bridges \HttpDI ;
11
11
12
12
use Nette ;
13
- use Nette \PhpGenerator \Helpers ;
14
13
15
14
16
15
/**
@@ -71,13 +70,13 @@ public function loadConfiguration()
71
70
}
72
71
73
72
74
- public function afterCompile ( Nette \ PhpGenerator \ ClassType $ class )
73
+ public function beforeCompile ( )
75
74
{
76
75
if ($ this ->cliMode ) {
77
76
return ;
78
77
}
79
78
80
- $ initialize = $ class -> getMethod ( ' initialize ' );
79
+ $ builder = $ this -> getContainerBuilder ( );
81
80
$ config = $ this ->getConfig ();
82
81
$ headers = array_map ('strval ' , $ config ['headers ' ]);
83
82
@@ -91,16 +90,14 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
91
90
$ headers ['X-Frame-Options ' ] = $ frames ;
92
91
}
93
92
94
- $ code = [];
95
93
foreach (['csp ' , 'cspReportOnly ' ] as $ key ) {
96
94
if (empty ($ config [$ key ])) {
97
95
continue ;
98
96
}
99
97
$ value = self ::buildPolicy ($ config [$ key ]);
100
98
if (strpos ($ value , "'nonce' " )) {
101
- $ code [0 ] = '$cspNonce = base64_encode(random_bytes(16)); ' ;
102
99
$ value = Nette \DI \ContainerBuilder::literal (
103
- 'str_replace(?, ? . $cspNonce, ?) ' ,
100
+ 'str_replace(?, ? . (isset( $cspNonce) \? $cspNonce : $cspNonce = base64_encode(random_bytes(16))) , ?) ' ,
104
101
["'nonce " , "'nonce- " , $ value ]
105
102
);
106
103
}
@@ -111,16 +108,15 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
111
108
$ headers ['Feature-Policy ' ] = self ::buildPolicy ($ config ['featurePolicy ' ]);
112
109
}
113
110
114
- $ code [] = Helpers::formatArgs ('$response = $this->getService(?); ' , [$ this ->prefix ('response ' )]);
115
111
foreach ($ headers as $ key => $ value ) {
116
112
if ($ value !== '' ) {
117
- $ code [] = Helpers::formatArgs ('$response->setHeader(?, ?); ' , [$ key , $ value ]);
113
+ $ builder ->getDefinition ($ this ->prefix ('response ' ))
114
+ ->addSetup ('?->setHeader(?, ?); ' , ['@self ' , $ key , $ value ]);
118
115
}
119
116
}
120
117
121
- $ code [] = Helpers::formatArgs ('$response->setCookie(...?); ' , [['nette-samesite ' , '1 ' , 0 , '/ ' , null , null , true , 'Strict ' ]]);
122
-
123
- $ initialize ->addBody ("(function () { \n\t" . implode ("\n\t" , $ code ) . "\n})(); " );
118
+ $ builder ->getDefinition ($ this ->prefix ('response ' ))
119
+ ->addSetup ('?->setCookie(...?) ' , ['@self ' , ['nette-samesite ' , '1 ' , 0 , '/ ' , null , null , true , 'Strict ' ]]);
124
120
}
125
121
126
122
0 commit comments