Skip to content

Commit 9b138d0

Browse files
committed
Move response configuration from initialize method to response service
1 parent 9901e0f commit 9b138d0

File tree

6 files changed

+16
-18
lines changed

6 files changed

+16
-18
lines changed

src/Bridges/HttpDI/HttpExtension.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ public function loadConfiguration()
7676
}
7777

7878

79-
public function afterCompile(Nette\PhpGenerator\ClassType $class)
79+
public function beforeCompile()
8080
{
8181
if ($this->cliMode) {
8282
return;
8383
}
8484

85-
$initialize = $class->getMethod('initialize');
85+
$builder = $this->getContainerBuilder();
8686
$config = $this->config;
8787
$headers = array_map('strval', $config->headers);
8888

@@ -96,16 +96,14 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
9696
$headers['X-Frame-Options'] = $frames;
9797
}
9898

99-
$code = [];
10099
foreach (['csp', 'cspReportOnly'] as $key) {
101100
if (empty($config->$key)) {
102101
continue;
103102
}
104103
$value = self::buildPolicy($config->$key);
105104
if (strpos($value, "'nonce'")) {
106-
$code[0] = '$cspNonce = base64_encode(random_bytes(16));';
107105
$value = Nette\DI\ContainerBuilder::literal(
108-
'str_replace(?, ? . $cspNonce, ?)',
106+
'str_replace(?, ? . (isset($cspNonce) \? $cspNonce : $cspNonce = base64_encode(random_bytes(16))), ?)',
109107
["'nonce", "'nonce-", $value]
110108
);
111109
}
@@ -116,16 +114,16 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
116114
$headers['Feature-Policy'] = self::buildPolicy($config->featurePolicy);
117115
}
118116

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+
120120
foreach ($headers as $key => $value) {
121121
if ($value !== '') {
122-
$code[] = Helpers::formatArgs('$response->setHeader(?, ?);', [$key, $value]);
122+
$response->addSetup('?->setHeader(?, ?);', ['@self', $key, $value]);
123123
}
124124
}
125125

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']]);
129127
}
130128

131129

tests/Http.DI/HttpExtension.csp.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ EOD
4545
eval($compiler->addConfig($config)->compile());
4646

4747
$container = new Container;
48-
$container->initialize();
48+
$container->getService('http.response');
4949

5050
$headers = headers_list();
5151

@@ -59,5 +59,5 @@ echo ' '; @ob_flush(); flush();
5959
Assert::true(headers_sent());
6060

6161
Assert::exception(function () use ($container) {
62-
$container->initialize();
62+
$container->createService('http.response');
6363
}, Nette\InvalidStateException::class, 'Cannot send header after %a%');

tests/Http.DI/HttpExtension.defaultHeaders.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ $compiler->addExtension('http', new HttpExtension);
2323
eval($compiler->compile());
2424

2525
$container = new Container;
26-
$container->initialize();
26+
$container->getService('http.response');
2727

2828
$headers = headers_list();
2929
Assert::contains('X-Frame-Options: SAMEORIGIN', $headers);

tests/Http.DI/HttpExtension.featurePolicy.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ EOD
3535
eval($compiler->addConfig($config)->compile());
3636

3737
$container = new Container;
38-
$container->initialize();
38+
$container->getService('http.response');
3939

4040
$headers = headers_list();
4141
var_dump($headers);
@@ -48,5 +48,5 @@ echo ' '; @ob_flush(); flush();
4848
Assert::true(headers_sent());
4949

5050
Assert::exception(function () use ($container) {
51-
$container->initialize();
51+
$container->createService('http.response');
5252
}, Nette\InvalidStateException::class, 'Cannot send header after %a%');

tests/Http.DI/HttpExtension.headers.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ EOD
3333
eval($compiler->addConfig($config)->compile());
3434

3535
$container = new Container;
36-
$container->initialize();
36+
$container->getService('http.response');
3737

3838
$headers = headers_list();
3939
Assert::contains('X-Frame-Options: SAMEORIGIN', $headers);
@@ -49,5 +49,5 @@ echo ' '; @ob_flush(); flush();
4949
Assert::true(headers_sent());
5050

5151
Assert::exception(function () use ($container) {
52-
$container->initialize();
52+
$container->createService('http.response');
5353
}, Nette\InvalidStateException::class, 'Cannot send header after %a%');

tests/Http.DI/HttpExtension.sameSiteProtection.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ $compiler->addExtension('http', new HttpExtension);
2121
eval($compiler->compile());
2222

2323
$container = new Container;
24-
$container->initialize();
24+
$container->getService('http.response');
2525

2626
$headers = headers_list();
2727
Assert::contains(

0 commit comments

Comments
 (0)