Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
51 / 51 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
SmartyTemplate | |
100.00% |
51 / 51 |
|
100.00% |
4 / 4 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
applyParameter | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
build | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
registerPlugins | |
100.00% |
30 / 30 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\Core\Mvc\Template; |
6 | |
7 | use PeServer\Core\Environment; |
8 | use PeServer\Core\IO\Path; |
9 | use PeServer\Core\Mvc\Template\Plugin\AssetFunction; |
10 | use PeServer\Core\Mvc\Template\Plugin\BotTextImageFunction; |
11 | use PeServer\Core\Mvc\Template\Plugin\CodeFunction; |
12 | use PeServer\Core\Mvc\Template\Plugin\CsrfFunction; |
13 | use PeServer\Core\Mvc\Template\Plugin\DumpModifier; |
14 | use PeServer\Core\Mvc\Template\Plugin\InputHelperFunction; |
15 | use PeServer\Core\Mvc\Template\Plugin\ITemplateBlockFunction; |
16 | use PeServer\Core\Mvc\Template\Plugin\ITemplateFunction; |
17 | use PeServer\Core\Mvc\Template\Plugin\ITemplateModifier; |
18 | use PeServer\Core\Mvc\Template\Plugin\MarkdownFunction; |
19 | use PeServer\Core\Mvc\Template\Plugin\PagerFunction; |
20 | use PeServer\Core\Mvc\Template\Plugin\ShowErrorMessagesFunction; |
21 | use PeServer\Core\Mvc\Template\Plugin\TemplatePluginArgument; |
22 | use PeServer\Core\Mvc\Template\Plugin\TimestampFunction; |
23 | use PeServer\Core\Mvc\Template\TemplateBase; |
24 | use PeServer\Core\Mvc\Template\TemplateOptions; |
25 | use PeServer\Core\Mvc\Template\TemplateParameter; |
26 | use PeServer\Core\Mvc\Template\TemplateStore; |
27 | use PeServer\Core\Store\Stores; |
28 | use PeServer\Core\Throws\NotImplementedException; |
29 | use Smarty\Smarty; |
30 | |
31 | class SmartyTemplate extends TemplateBase |
32 | { |
33 | #region variable |
34 | |
35 | /** |
36 | * テンプレートエンジン。 |
37 | */ |
38 | private Smarty $engine; |
39 | |
40 | #endregion |
41 | |
42 | public function __construct( |
43 | TemplateOptions $options, |
44 | protected Stores $stores, |
45 | protected Environment $environment |
46 | ) { |
47 | parent::__construct($options); |
48 | |
49 | $this->engine = new Smarty(); |
50 | |
51 | $this->engine->addTemplateDir(Path::combine($this->options->rootDirectoryPath, $this->options->baseDirectoryName)); |
52 | $this->engine->addTemplateDir($this->options->rootDirectoryPath); |
53 | $this->engine->setCompileDir(Path::combine($this->options->temporaryDirectoryPath, 'compile', $this->options->baseDirectoryName)); |
54 | $this->engine->setCacheDir(Path::combine($this->options->temporaryDirectoryPath, 'cache', $this->options->baseDirectoryName)); |
55 | |
56 | $this->engine->escape_html = true; |
57 | |
58 | $this->registerPlugins(); |
59 | } |
60 | |
61 | #region function |
62 | |
63 | private function applyParameter(TemplateParameter $parameter): void |
64 | { |
65 | $this->engine->assign([ |
66 | 'status' => $parameter->httpStatus, |
67 | 'values' => $parameter->values, |
68 | 'errors' => $parameter->errors, |
69 | 'stores' => [ |
70 | 'cookie' => TemplateStore::createCookie($this->stores->cookie), |
71 | 'session' => TemplateStore::createSession($this->stores->session), |
72 | 'temporary' => TemplateStore::createTemporary($this->stores->temporary), |
73 | ], |
74 | 'environment' => $this->environment |
75 | ]); |
76 | } |
77 | |
78 | public function build(string $templateName, TemplateParameter $parameter): string |
79 | { |
80 | $this->applyParameter($parameter); |
81 | return $this->engine->fetch($templateName); |
82 | } |
83 | |
84 | private function registerPlugins(): void |
85 | { |
86 | $argument = new TemplatePluginArgument( |
87 | $this->engine, |
88 | $this->options->rootDirectoryPath, |
89 | Path::combine($this->options->rootDirectoryPath, $this->options->baseDirectoryName), |
90 | $this->options->programContext, |
91 | $this->options->urlHelper, |
92 | $this->options->webSecurity, |
93 | $this->stores, |
94 | $this->environment |
95 | ); |
96 | $showErrorMessagesFunction = new ShowErrorMessagesFunction($argument); |
97 | /** @var array<ITemplateFunction|ITemplateModifier> */ |
98 | $plugins = [ |
99 | new CsrfFunction($argument), |
100 | new AssetFunction($argument), |
101 | $showErrorMessagesFunction, |
102 | new InputHelperFunction($argument, $showErrorMessagesFunction), |
103 | new BotTextImageFunction($argument), |
104 | new MarkdownFunction($argument), |
105 | new CodeFunction($argument), |
106 | new PagerFunction($argument), |
107 | new DumpModifier($argument), |
108 | new TimestampFunction($argument), |
109 | ]; |
110 | foreach ($plugins as $plugin) { |
111 | // 関数は重複できない |
112 | if ($plugin instanceof ITemplateBlockFunction) { |
113 | $this->engine->registerPlugin(Smarty::PLUGIN_BLOCK, $plugin->getFunctionName(), [$plugin, 'functionBlockBody']); |
114 | } elseif ($plugin instanceof ITemplateFunction) { |
115 | $this->engine->registerPlugin(Smarty::PLUGIN_FUNCTION, $plugin->getFunctionName(), [$plugin, 'functionBody']); |
116 | } |
117 | |
118 | if ($plugin instanceof ITemplateModifier) { |
119 | $this->engine->registerPlugin(Smarty::PLUGIN_MODIFIER, $plugin->getModifierName(), [$plugin, 'modifierBody']); |
120 | } |
121 | } |
122 | } |
123 | |
124 | #endregion |
125 | } |