Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
51 / 51
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
SmartyTemplate
100.00% covered (success)
100.00%
51 / 51
100.00% covered (success)
100.00%
4 / 4
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 applyParameter
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 build
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 registerPlugins
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\Core\Mvc\Template;
6
7use PeServer\Core\Environment;
8use PeServer\Core\IO\Path;
9use PeServer\Core\Mvc\Template\Plugin\AssetFunction;
10use PeServer\Core\Mvc\Template\Plugin\BotTextImageFunction;
11use PeServer\Core\Mvc\Template\Plugin\CodeFunction;
12use PeServer\Core\Mvc\Template\Plugin\CsrfFunction;
13use PeServer\Core\Mvc\Template\Plugin\DumpModifier;
14use PeServer\Core\Mvc\Template\Plugin\InputHelperFunction;
15use PeServer\Core\Mvc\Template\Plugin\ITemplateBlockFunction;
16use PeServer\Core\Mvc\Template\Plugin\ITemplateFunction;
17use PeServer\Core\Mvc\Template\Plugin\ITemplateModifier;
18use PeServer\Core\Mvc\Template\Plugin\MarkdownFunction;
19use PeServer\Core\Mvc\Template\Plugin\PagerFunction;
20use PeServer\Core\Mvc\Template\Plugin\ShowErrorMessagesFunction;
21use PeServer\Core\Mvc\Template\Plugin\TemplatePluginArgument;
22use PeServer\Core\Mvc\Template\Plugin\TimestampFunction;
23use PeServer\Core\Mvc\Template\TemplateBase;
24use PeServer\Core\Mvc\Template\TemplateOptions;
25use PeServer\Core\Mvc\Template\TemplateParameter;
26use PeServer\Core\Mvc\Template\TemplateStore;
27use PeServer\Core\Store\Stores;
28use PeServer\Core\Throws\NotImplementedException;
29use Smarty\Smarty;
30
31class 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}