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

SmartyTemplate   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 57
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A _subTemplateRegister() 0 13 3
A dispatch() 0 19 2
A normalizeName() 0 8 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
    /**
33
     * {@inheritdoc}
34
     */
35
    public function _subTemplateRegister()
36
    {
37
        foreach ($this->compiled->includes as $name => $count) {
38
            // @codeCoverageIgnoreStart
39
            if (isset($this->smarty->_cache['subTplInfo'][$name])) {
40
                $this->smarty->_cache['subTplInfo'][$name] += $count;
41
            } else {
42
                $this->smarty->_cache['subTplInfo'][$name] = $count;
43
            }
44
            // @codeCoverageIgnoreEnd
45
            $this->dispatch(new self($name, $this->smarty));
46
        }
47
    }
48
49
    /**
50
     * @param \Smarty_Internal_Template $template
51
     */
52
    protected function dispatch(\Smarty_Internal_Template $template)
53
    {
54
        /** @var SmartyFactory $viewFactory */
55
        $viewFactory = $this->smarty->getViewFactory();
56
        $name = $this->normalizeName($template, $viewFactory);
57
        $view = new View(
58
            $viewFactory,
59
            $viewFactory->getEngineResolver()->resolve('smarty'),
60
            $name,
61
            $template->source->filepath,
62
            []
63
        );
64
        $viewFactory->callCreator($view);
65
        $viewFactory->callComposer($view);
66
        foreach ($view->getData() as $key => $data) {
67
            $this->assign($key, $data);
68
        }
69
        unset($template);
70
    }
71
72
    /**
73
     * @param \Smarty_Internal_Template $template
74
     * @param SmartyFactory             $viewFactory
75
     *
76
     * @return mixed
77
     */
78
    protected function normalizeName(
79
        \Smarty_Internal_Template $template,
80
        SmartyFactory $viewFactory
81
    ) {
82
        $name = str_replace('.' . $viewFactory->getSmartyFileExtension(), '', $template->source->name);
83
84
        return str_replace('/', '.', $name);
85
    }
86
}
87