Completed
Pull Request — master (#45)
by yuuki
01:45
created

SmartyTemplate::normalizeTemplateName()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 6
nop 1
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-2016 Yuuki Takezawa
16
 *
17
 */
18
19
namespace Ytake\LaravelSmarty\Engines;
20
21
use Illuminate\View\View;
22
use Ytake\LaravelSmarty\SmartyFactory;
23
24
/**
25
 * Class SmartyTemplate
26
 *
27
 * @author  yuuki.takezawa <[email protected]>
28
 * @license http://opensource.org/licenses/MIT MIT
29
 */
30
class SmartyTemplate extends \Smarty_Internal_Template
31
{
32
    /** @var string */
33
    private $templateResourceName;
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function _subTemplateRender(
39
        $template,
40
        $cache_id,
41
        $compile_id,
42
        $caching,
43
        $cache_lifetime,
44
        $data,
45
        $scope,
46
        $forceTplCache,
47
        $uid = null,
48
        $content_func = null
49
    ) {
50
        $this->templateResourceName = $template;
51
        parent::_subTemplateRender($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $scope,
52
            $forceTplCache, $uid, $content_func);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function _subTemplateRegister()
59
    {
60
        foreach ($this->compiled->includes as $name => $count) {
61
            // @codeCoverageIgnoreStart
62
            if (isset($this->smarty->_cache['subTplInfo'][$name])) {
63
                $this->smarty->_cache['subTplInfo'][$name] += $count;
64
            } else {
65
                $this->smarty->_cache['subTplInfo'][$name] = $count;
66
            }
67
            // @codeCoverageIgnoreEnd
68
        }
69
        if ($this->templateResourceName) {
70
            $parseResourceName = \Smarty_Resource::parseResourceName(
71
                $this->templateResourceName,
72
                $this->smarty->default_resource_type
73
            );
74
            $this->dispatch($this, $parseResourceName[0]);
75
        }
76
    }
77
78
    /**
79
     * @param \Smarty_Internal_Template $template
80
     * @param string                    $name
81
     */
82
    protected function dispatch(\Smarty_Internal_Template $template, $name)
83
    {
84
        /** @var SmartyFactory $viewFactory */
85
        $viewFactory = $this->smarty->getViewFactory();
86
        $name = $this->normalizeName($name, $viewFactory);
87
        $view = new View(
88
            $viewFactory,
89
            $viewFactory->getEngineResolver()->resolve('smarty'),
90
            $name,
91
            $template->source->filepath,
92
            []
93
        );
94
        $viewFactory->callCreator($view);
95
        $viewFactory->callComposer($view);
96
        foreach ($view->getData() as $key => $data) {
97
            $this->assign($key, $data);
98
        }
99
        unset($template);
100
    }
101
102
    /**
103
     * @param string        $name
104
     * @param SmartyFactory $viewFactory
105
     *
106
     * @return mixed
107
     */
108
    protected function normalizeName($name, SmartyFactory $viewFactory)
109
    {
110
        $name = str_replace('.' . $viewFactory->getSmartyFileExtension(), '', $name);
111
112
        return str_replace('/', '.', $name);
113
    }
114
}
115