SmartyTemplate   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 7
dl 0
loc 85
rs 10
c 0
b 0
f 0

4 Methods

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