Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
5.56% |
1 / 18 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
ViewActionResult | |
5.56% |
1 / 18 |
|
50.00% |
1 / 2 |
17.48 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createResponse | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\Core\Mvc\Result; |
6 | |
7 | use PeServer\Core\Http\ContentType; |
8 | use PeServer\Core\Http\HttpResponse; |
9 | use PeServer\Core\IO\Directory; |
10 | use PeServer\Core\IO\Path; |
11 | use PeServer\Core\Mime; |
12 | use PeServer\Core\Mvc\Result\IActionResult; |
13 | use PeServer\Core\Mvc\Template\ITemplateFactory; |
14 | use PeServer\Core\Mvc\Template\TemplateOptions; |
15 | use PeServer\Core\Mvc\Template\TemplateParameter; |
16 | use PeServer\Core\ProgramContext; |
17 | use PeServer\Core\Web\IUrlHelper; |
18 | use PeServer\Core\Web\WebSecurity; |
19 | |
20 | /** |
21 | * 結果操作: View。 |
22 | */ |
23 | readonly class ViewActionResult implements IActionResult |
24 | { |
25 | /** |
26 | * 生成。 |
27 | * |
28 | * @param non-empty-string $templateBaseName |
29 | * @param non-empty-string $actionName |
30 | * @param TemplateParameter $templateParameter |
31 | * @param array<non-empty-string,string[]> $headers |
32 | */ |
33 | public function __construct( |
34 | protected string $templateBaseName, |
35 | protected string $actionName, |
36 | protected TemplateParameter $templateParameter, |
37 | protected array $headers, |
38 | protected ProgramContext $programContext, |
39 | protected ITemplateFactory $templateFactory, |
40 | protected IUrlHelper $urlHelper, |
41 | protected WebSecurity $webSecurity |
42 | ) { |
43 | } |
44 | |
45 | #region IActionResult |
46 | |
47 | public function createResponse(): HttpResponse |
48 | { |
49 | $response = new HttpResponse(); |
50 | |
51 | $response->status = $this->templateParameter->httpStatus; |
52 | |
53 | foreach ($this->headers as $name => $headers) { |
54 | $response->header->setValues($name, $headers); |
55 | } |
56 | if (!$response->header->existsHeader(ContentType::NAME)) { |
57 | $response->header->addValue(ContentType::NAME, Mime::HTML); |
58 | } |
59 | |
60 | $options = new TemplateOptions( |
61 | __DIR__ . '/../../template', |
62 | '', |
63 | $this->programContext, |
64 | $this->urlHelper, |
65 | $this->webSecurity, |
66 | Path::combine(Directory::getTemporaryDirectory(), 'PeServer-Core', 'template') |
67 | ); |
68 | $template = $this->templateFactory->createTemplate($options); |
69 | |
70 | $response->content = $template->build($this->actionName . '.tpl', $this->templateParameter); |
71 | |
72 | return $response; |
73 | } |
74 | |
75 | #endregion |
76 | } |