Completed
Pull Request — master (#44)
by Mark
01:40
created

SmartyTemplate::dispatch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 2
eloc 11
nc 2
nop 2
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 _subTemplateRender($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $scope,
36
                                       $forceTplCache, $uid = null, $content_func = null)
37
    {
38
        $tpl = clone $this;
39
        $tpl->parent = $this;
40
        $smarty = &$this->smarty;
41
        $_templateId = $smarty->_getTemplateId($template, $cache_id, $compile_id, $caching, $tpl);
42
        // recursive call ?
43
        if (isset($tpl->templateId) ? $tpl->templateId : $tpl->_getTemplateId() != $_templateId) {
44
            // already in template cache?
45
            if (isset($smarty->_cache[ 'tplObjects' ][ $_templateId ])) {
46
                // copy data from cached object
47
                $cachedTpl = &$smarty->_cache[ 'tplObjects' ][ $_templateId ];
48
                $tpl->templateId = $cachedTpl->templateId;
49
                $tpl->template_resource = $cachedTpl->template_resource;
50
                $tpl->cache_id = $cachedTpl->cache_id;
51
                $tpl->compile_id = $cachedTpl->compile_id;
52
                $tpl->source = $cachedTpl->source;
53
                if (isset($cachedTpl->compiled)) {
54
                    $tpl->compiled = $cachedTpl->compiled;
55
                } else {
56
                    unset($tpl->compiled);
57
                }
58
                if ($caching != 9999 && isset($cachedTpl->cached)) {
59
                    $tpl->cached = $cachedTpl->cached;
60
                } else {
61
                    unset($tpl->cached);
62
                }
63
            } else {
64
                $tpl->templateId = $_templateId;
65
                $tpl->template_resource = $template;
66
                $tpl->cache_id = $cache_id;
67
                $tpl->compile_id = $compile_id;
68
                if (isset($uid)) {
69
                    // for inline templates we can get all resource information from file dependency
70
                    list($filepath, $timestamp, $type) = $tpl->compiled->file_dependency[ $uid ];
71
                    $tpl->source = new \Smarty_Template_Source($smarty, $filepath, $type, $filepath);
72
                    $tpl->source->filepath = $filepath;
73
                    $tpl->source->timestamp = $timestamp;
74
                    $tpl->source->exists = true;
75
                    $tpl->source->uid = $uid;
76
77
                    $pathinfo = pathinfo($tpl->source->filepath);
78
                    $this->dispatch($pathinfo['filename'], $tpl->source->filepath);
79
                } else {
80
                    $tpl->source = \Smarty_Template_Source::load($tpl);
81
82
                    $pathinfo = pathinfo($tpl->source->filepath);
83
                    $this->dispatch($pathinfo['filename'], $tpl->source->filepath);
84
                    unset($tpl->compiled);
85
                }
86
                if ($caching != 9999) {
87
                    unset($tpl->cached);
88
                }
89
            }
90
        } else {
91
            // on recursive calls force caching
92
            $forceTplCache = true;
93
        }
94
        $tpl->caching = $caching;
95
        $tpl->cache_lifetime = $cache_lifetime;
96
        // set template scope
97
        $tpl->scope = $scope;
98
        if (!isset($smarty->_cache[ 'tplObjects' ][ $tpl->templateId ]) && !$tpl->source->handler->recompiled) {
99
            // check if template object should be cached
100
            if ($forceTplCache || (isset($smarty->_cache[ 'subTplInfo' ][ $tpl->template_resource ]) &&
101
                                   $smarty->_cache[ 'subTplInfo' ][ $tpl->template_resource ] > 1) ||
102
                ($tpl->_isParentTemplate() && isset($smarty->_cache[ 'tplObjects' ][ $tpl->parent->templateId ]))
103
            ) {
104
                $smarty->_cache[ 'tplObjects' ][ $tpl->templateId ] = $tpl;
105
            }
106
        }
107
108
        if (!empty($data)) {
109
            // set up variable values
110
            foreach ($data as $_key => $_val) {
111
                $tpl->tpl_vars[ $_key ] = new \Smarty_Variable($_val, $this->isRenderingCache);
112
            }
113
        }
114
        if ($tpl->caching == 9999) {
115
            if (!isset($tpl->compiled)) {
116
                $this->loadCompiled(true);
117
            }
118
            if ($tpl->compiled->has_nocache_code) {
119
                $this->cached->hashes[ $tpl->compiled->nocache_hash ] = true;
120
            }
121
        }
122
        $tpl->_cache = array();
123
        if (isset($uid)) {
124
            if ($smarty->debugging) {
125
                if (!isset($smarty->_debug)) {
126
                    $smarty->_debug = new \Smarty_Internal_Debug();
127
                }
128
                $smarty->_debug->start_template($tpl);
129
                $smarty->_debug->start_render($tpl);
130
            }
131
            $tpl->compiled->getRenderedTemplateCode($tpl, $content_func);
132
            if ($smarty->debugging) {
133
                $smarty->_debug->end_template($tpl);
134
                $smarty->_debug->end_render($tpl);
135
            }
136
        } else {
137
            if (isset($tpl->compiled)) {
138
                $tpl->compiled->render($tpl);
139
            } else {
140
                $tpl->render();
141
            }
142
        }
143
    }
144
145
    /**
146
     * @param string $name
147
     */
148
    protected function dispatch($name, $path)
149
    {
150
        /** @var Factory $viewFactory */
151
        $viewFactory = $this->smarty->getViewFactory();
152
        $view = new View(
153
            $viewFactory,
154
            $viewFactory->getEngineResolver()->resolve('smarty'),
155
            $name,
156
            $path,
157
            []
158
        );
159
160
        $viewFactory->callCreator($view);
161
        $viewFactory->callComposer($view);
162
        foreach ($view->getData() as $key => $data) {
163
            $this->assign($key, $data);
164
        }
165
    }
166
}