Completed
Push — master ( 76df40...642da6 )
by yuuki
01:59
created

SmartyTemplate   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A _subTemplateRegister() 0 11 3
A dispatch() 0 17 2
A normalizeTemplateName() 0 12 4
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 Illuminate\View\Factory;
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
            if (isset($this->smarty->_cache['subTplInfo'][$name])) {
39
                $this->smarty->_cache['subTplInfo'][$name] += $count;
40
            } else {
41
                $this->smarty->_cache['subTplInfo'][$name] = $count;
42
            }
43
            $this->dispatch($this->normalizeTemplateName($name));
44
        }
45
    }
46
47
    /**
48
     * @param string $name
49
     */
50
    protected function dispatch($name)
51
    {
52
        /** @var Factory $viewFactory */
53
        $viewFactory = $this->smarty->getViewFactory();
54
        $view = new View(
55
            $viewFactory,
56
            $viewFactory->getEngineResolver()->resolve('smarty'),
57
            $name,
58
            null,
59
            []
60
        );
61
        $viewFactory->callCreator($view);
62
        $viewFactory->callComposer($view);
63
        foreach ($view->getData() as $key => $data) {
64
            $this->assign($key, $data);
65
        }
66
    }
67
68
    /**
69
     * @param string $name
70
     *
71
     * @return string
72
     */
73
    protected function normalizeTemplateName($name)
74
    {
75
        $name = "\"$name\"";
76
        if (preg_match('/^([\'"])(([A-Za-z0-9_\-]{2,})[:])?(([^$()]+)|(.+))\1$/', $name, $match)) {
77
            $name = !empty($match[5]) ? $match[5] : $match[6];
78
        }
79
        $fileInfo = new \SplFileInfo($name);
80
        $path = ($fileInfo->getPath() === '') ? null : $fileInfo->getPath() . '/';
81
        $viewPathInfo = $path . $fileInfo->getBasename('.' . $fileInfo->getExtension());
82
83
        return trim(str_replace('/', '.', $viewPathInfo), '\'"');
84
    }
85
}
86