Skip to content

Commit 4b12bd5

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

6 files changed

+16
-19
lines changed

src/Bridges/HttpDI/HttpExtension.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace Nette\Bridges\HttpDI;
1111

1212
use Nette;
13-
use Nette\PhpGenerator\Helpers;
1413
use Nette\Schema\Expect;
1514

1615

@@ -76,13 +75,13 @@ public function loadConfiguration()
7675
}
7776

7877

79-
public function afterCompile(Nette\PhpGenerator\ClassType $class)
78+
public function beforeCompile()
8079
{
8180
if ($this->cliMode) {
8281
return;
8382
}
8483

85-
$initialize = $class->getMethod('initialize');
84+
$builder = $this->getContainerBuilder();
8685
$config = $this->config;
8786
$headers = array_map('strval', $config->headers);
8887

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

99-
$code = [];
10098
foreach (['csp', 'cspReportOnly'] as $key) {
10199
if (empty($config->$key)) {
102100
continue;
103101
}
104102
$value = self::buildPolicy($config->$key);
105103
if (strpos($value, "'nonce'")) {
106-
$code[0] = '$cspNonce = base64_encode(random_bytes(16));';
107104
$value = Nette\DI\ContainerBuilder::literal(
108-
'str_replace(?, ? . $cspNonce, ?)',
105+
'str_replace(?, ? . (isset($cspNonce) \? $cspNonce : $cspNonce = base64_encode(random_bytes(16))), ?)',
109106
["'nonce", "'nonce-", $value]
110107
);
111108
}
@@ -116,16 +113,16 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
116113
$headers['Feature-Policy'] = self::buildPolicy($config->featurePolicy);
117114
}
118115

119-
$code[] = Helpers::formatArgs('$response = $this->getService(?);', [$this->prefix('response')]);
116+
$response = $builder->getDefinition($this->prefix('response'));
117+
assert($response instanceof Nette\DI\Definitions\ServiceDefinition);
118+
120119
foreach ($headers as $key => $value) {
121120
if ($value !== '') {
122-
$code[] = Helpers::formatArgs('$response->setHeader(?, ?);', [$key, $value]);
121+
$response->addSetup('?->setHeader(?, ?);', ['@self', $key, $value]);
123122
}
124123
}
125124

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})();");
125+
$response->addSetup('?->setCookie(...?)', ['@self', ['nette-samesite', '1', 0, '/', null, null, true, 'Strict']]);
129126
}
130127

131128

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)