Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/**
4
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10
 * THE SOFTWARE.
11
 *
12
 * This software consists of voluntary contributions made by many individuals
13
 * and is licensed under the MIT license.
14
 *
15
 * Copyright (c) 2014-2022 Yuuki Takezawa
16
 *
17
 */
18
 
19
declare(strict_types=1);
20
 
21
namespace Ytake\LaravelSmarty\Engines;
22
 
23
use Illuminate\View\View;
24
use Smarty_Internal_Template;
25
use Smarty_Resource;
26
use Ytake\LaravelSmarty\Smarty;
27
use Ytake\LaravelSmarty\SmartyFactory;
28
 
29
use function str_replace;
30
 
31
/**
32
 * Class SmartyTemplate
33
 *
34
 * @author  yuuki.takezawa <yuuki.takezawa@comnect.jp.net>
35
 * @license http://opensource.org/licenses/MIT MIT
36
 */
37
class SmartyTemplate extends Smarty_Internal_Template
38
{
39
    /** @var string|null  */
40
    private ?string $templateResourceName = null;
41
 
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function _subTemplateRender(
46
        $template,
47
        $cache_id,
48
        $compile_id,
49
        $caching,
50
        $cache_lifetime,
51
        $data,
52
        $scope,
53
        $forceTplCache,
54
        $uid = null,
55
        $content_func = null
56
    ): void {
57
        $this->templateResourceName = $template;
58
        parent::_subTemplateRender(
59
            $template,
60
            $cache_id,
61
            $compile_id,
62
            $caching,
63
            $cache_lifetime,
64
            $data,
65
            $scope,
66
            $forceTplCache,
67
            $uid,
68
            $content_func
69
        );
70
    }
71
 
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function _subTemplateRegister(): void
76
    {
77
        foreach ($this->compiled->includes as $name => $count) {
78
            // @codeCoverageIgnoreStart
79
            if (isset($this->smarty->_cache['subTplInfo'][$name])) {
80
                $this->smarty->_cache['subTplInfo'][$name] += $count;
81
            } else {
82
                $this->smarty->_cache['subTplInfo'][$name] = $count;
83
            }
84
            // @codeCoverageIgnoreEnd
85
        }
86
        if ($this->templateResourceName) {
87
            $parseResourceName = Smarty_Resource::parseResourceName(
88
                $this->templateResourceName,
89
                $this->smarty->default_resource_type
90
            );
91
            $this->dispatch($this, $parseResourceName[0]);
92
        }
93
    }
94
 
95
    /**
96
     * @param Smarty_Internal_Template $template
97
     * @param string $name
98
     */
99
    protected function dispatch(
100
        Smarty_Internal_Template $template,
101
        string $name
102
    ): void {
103
        /** @var SmartyFactory $viewFactory */
104
        $viewFactory = $this->smarty->getViewFactory();
105
        $name = $this->normalizeName($name, $viewFactory);
106
        $view = new View(
107
            $viewFactory,
108
            $viewFactory->getEngineResolver()->resolve('smarty'),
109
            $name,
110
            $template->source->filepath,
111
            []
112
        );
113
        $viewFactory->callCreator($view);
114
        $viewFactory->callComposer($view);
115
        foreach ($view->getData() as $key => $data) {
116
            $this->assign($key, $data);
117
        }
118
        unset($template);
119
    }
120
 
121
    /**
122
     * @param string $name
123
     * @param SmartyFactory $viewFactory
124
     *
125
     * @return string|string[]
126
     */
127
    protected function normalizeName(
128
        string $name,
129
        SmartyFactory $viewFactory
130
    ): array|string {
131
        $name = str_replace('.' . $viewFactory->getSmartyFileExtension(), '', $name);
132
 
133
        return str_replace('/', '.', $name);
134
    }
135
}