Completed
Branch master (e379bd)
by Pierre-Henry
33:06
created

Smarty_Internal_Runtime_UpdateCache::updateCache()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 32
nop 3
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Inline Runtime Methods render, setSourceByUid, setupSubTemplate
5
 *
6
 * @package    Smarty
7
 * @subpackage PluginsInternal
8
 * @author     Uwe Tews
9
 *
10
 **/
11
class Smarty_Internal_Runtime_UpdateCache
12
{
13
    /**
14
     * check client side cache
15
     *
16
     * @param \Smarty_Template_Cached  $cached
17
     * @param Smarty_Internal_Template $_template
18
     * @param  string                  $content
19
     */
20
    public function cacheModifiedCheck(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $content)
0 ignored issues
show
Unused Code introduced by
The parameter $cached is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $_template is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $content is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
21
    {
22
    }
23
24
    /**
25
     * Sanitize content and write it to cache resource
26
     *
27
     * @param \Smarty_Template_Cached  $cached
28
     * @param Smarty_Internal_Template $_template
29
     * @param bool                     $no_output_filter
30
     *
31
     * @throws \SmartyException
32
     */
33
    public function removeNoCacheHash(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template,
34
                                      $no_output_filter)
35
    {
36
        $content = ob_get_clean();
37
        unset($cached->hashes[ $_template->compiled->nocache_hash ]);
38
        if (!empty($cached->hashes)) {
39
            $hash_array = array();
40
            foreach ($cached->hashes as $hash => $foo) {
41
                $hash_array[] = "/{$hash}/";
42
            }
43
            $content = preg_replace($hash_array, $_template->compiled->nocache_hash, $content);
44
        }
45
        $_template->cached->has_nocache_code = false;
46
        // get text between non-cached items
47
        $cache_split =
48
            preg_split("!/\*%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*\/(.+?)/\*/%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*/!s",
49
                       $content);
50
        // get non-cached items
51
        preg_match_all("!/\*%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*\/(.+?)/\*/%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*/!s",
52
                       $content, $cache_parts);
53
        $content = '';
54
        // loop over items, stitch back together
55
        foreach ($cache_split as $curr_idx => $curr_split) {
56
            // escape PHP tags in template content
57
            $content .= preg_replace('/(<%|%>|<\?php|<\?|\?>|<script\s+language\s*=\s*[\"\']?\s*php\s*[\"\']?\s*>)/',
58
                                     "<?php echo '\$1'; ?>\n", $curr_split);
59
            if (isset($cache_parts[ 0 ][ $curr_idx ])) {
60
                $_template->cached->has_nocache_code = true;
61
                $content .= $cache_parts[ 1 ][ $curr_idx ];
62
            }
63
        }
64
        if (!$no_output_filter && !$_template->cached->has_nocache_code &&
65
            (isset($_template->smarty->autoload_filters[ 'output' ]) ||
66
             isset($_template->smarty->registered_filters[ 'output' ]))
67
        ) {
68
            $content = $_template->smarty->ext->_filterHandler->runFilter('output', $content, $_template);
69
        }
70
        // write cache file content
71
        $this->writeCachedContent($cached, $_template, $content);
72
    }
73
74
    /**
75
     * Cache was invalid , so render from compiled and write to cache
76
     *
77
     * @param \Smarty_Template_Cached   $cached
78
     * @param \Smarty_Internal_Template $_template
79
     * @param                           $no_output_filter
80
     *
81
     * @throws \Exception
82
     */
83
    public function updateCache(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $no_output_filter)
84
    {
85
        ob_start();
86
        if (!isset($_template->compiled)) {
87
            $_template->loadCompiled();
88
        }
89
        $_template->compiled->render($_template);
90
        if ($_template->smarty->debugging) {
91
            $_template->smarty->_debug->start_cache($_template);
92
        }
93
        $this->removeNoCacheHash($cached, $_template, $no_output_filter);
94
        $compile_check = $_template->smarty->compile_check;
95
        $_template->smarty->compile_check = false;
96
        if ($_template->_isSubTpl()) {
97
            $_template->compiled->unifunc = $_template->parent->compiled->unifunc;
0 ignored issues
show
Bug introduced by
The property compiled does not seem to exist in Smarty_Internal_Data.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
98
        }
99
        if (!$_template->cached->processed) {
100
            $_template->cached->process($_template, true);
101
        }
102
        $_template->smarty->compile_check = $compile_check;
103
        $cached->getRenderedTemplateCode($_template);
104
        if ($_template->smarty->debugging) {
105
            $_template->smarty->_debug->end_cache($_template);
106
        }
107
    }
108
109
    /**
110
     * Writes the content to cache resource
111
     *
112
     * @param \Smarty_Template_Cached  $cached
113
     * @param Smarty_Internal_Template $_template
114
     * @param string                   $content
115
     *
116
     * @return bool
117
     */
118
    public function writeCachedContent(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $content)
119
    {
120
        if ($_template->source->handler->recompiled || !($_template->caching == Smarty::CACHING_LIFETIME_CURRENT ||
121
                                                         $_template->caching == Smarty::CACHING_LIFETIME_SAVED)
122
        ) {
123
            // don't write cache file
124
            return false;
125
        }
126
        $content = $_template->smarty->ext->_codeFrame->create($_template, $content, '', true);
127
        return $this->write($cached, $_template, $content);
128
    }
129
130
    /**
131
     * Write this cache object to handler
132
     *
133
     * @param \Smarty_Template_Cached  $cached
134
     * @param Smarty_Internal_Template $_template template object
135
     * @param string                   $content   content to cache
136
     *
137
     * @return bool success
138
     */
139
    public function write(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $content)
140
    {
141
        if (!$_template->source->handler->recompiled) {
142
            if ($cached->handler->writeCachedContent($_template, $content)) {
143
                $cached->content = null;
144
                $cached->timestamp = time();
145
                $cached->exists = true;
146
                $cached->valid = true;
147
                $cached->cache_lifetime = $_template->cache_lifetime;
148
                $cached->processed = false;
149
                if ($_template->smarty->cache_locking) {
150
                    $cached->handler->releaseLock($_template->smarty, $cached);
151
                }
152
153
                return true;
154
            }
155
            $cached->content = null;
156
            $cached->timestamp = false;
157
            $cached->exists = false;
158
            $cached->valid = false;
159
            $cached->processed = false;
160
        }
161
162
        return false;
163
    }
164
165
}