1<?php
2
3/**
4 * Smarty Method CompileAllTemplates
5 *
6 * Smarty::compileAllTemplates() method
7 *
8 * @package    Smarty
9 * @subpackage PluginsInternal
10 * @author     Uwe Tews
11 */
12class Smarty_Internal_Method_CompileAllTemplates
13{
14    /**
15     * Valid for Smarty object
16     *
17     * @var int
18     */
19    public $objMap = 1;
20
21    /**
22     * Compile all template files
23     *
24     * @api Smarty::compileAllTemplates()
25     *
26     * @param \Smarty $smarty        passed smarty object
27     * @param string  $extension     file extension
28     * @param bool    $force_compile force all to recompile
29     * @param int     $time_limit
30     * @param int     $max_errors
31     *
32     * @return integer number of template files recompiled
33     */
34    public function compileAllTemplates(
35        Smarty $smarty,
36        $extension = '.tpl',
37        $force_compile = false,
38        $time_limit = 0,
39        $max_errors = null
40    ) {
41        return $this->compileAll($smarty, $extension, $force_compile, $time_limit, $max_errors);
42    }
43
44    /**
45     * Compile all template or config files
46     *
47     * @param \Smarty $smarty
48     * @param string  $extension     template file name extension
49     * @param bool    $force_compile force all to recompile
50     * @param int     $time_limit    set maximum execution time
51     * @param int     $max_errors    set maximum allowed errors
52     * @param bool    $isConfig      flag true if called for config files
53     *
54     * @return int number of template files compiled
55     */
56    protected function compileAll(
57        Smarty $smarty,
58        $extension,
59        $force_compile,
60        $time_limit,
61        $max_errors,
62        $isConfig = false
63    ) {
64        // switch off time limit
65        if (function_exists('set_time_limit')) {
66            @set_time_limit($time_limit);
67        }
68        $_count = 0;
69        $_error_count = 0;
70        $sourceDir = $isConfig ? $smarty->getConfigDir() : $smarty->getTemplateDir();
71        // loop over array of source directories
72        foreach ($sourceDir as $_dir) {
73            $_dir_1 = new RecursiveDirectoryIterator(
74                $_dir,
75                defined('FilesystemIterator::FOLLOW_SYMLINKS') ?
76                    FilesystemIterator::FOLLOW_SYMLINKS : 0
77            );
78            $_dir_2 = new RecursiveIteratorIterator($_dir_1);
79            foreach ($_dir_2 as $_fileinfo) {
80                $_file = $_fileinfo->getFilename();
81                if (substr(basename($_fileinfo->getPathname()), 0, 1) === '.' || strpos($_file, '.svn') !== false) {
82                    continue;
83                }
84                if (substr_compare($_file, $extension, -strlen($extension)) !== 0) {
85                    continue;
86                }
87                if ($_fileinfo->getPath() !== substr($_dir, 0, -1)) {
88                    $_file = substr($_fileinfo->getPath(), strlen($_dir)) . DIRECTORY_SEPARATOR . $_file;
89                }
90                echo "\n<br>", $_dir, '---', $_file;
91                flush();
92                $_start_time = microtime(true);
93                $_smarty = clone $smarty;
94                //
95                $_smarty->_cache = array();
96                $_smarty->ext = new Smarty_Internal_Extension_Handler();
97                $_smarty->ext->objType = $_smarty->_objType;
98                $_smarty->force_compile = $force_compile;
99                try {
100                    /* @var Smarty_Internal_Template $_tpl */
101                    $_tpl = new $smarty->template_class($_file, $_smarty);
102                    $_tpl->caching = Smarty::CACHING_OFF;
103                    $_tpl->source =
104                        $isConfig ? Smarty_Template_Config::load($_tpl) : Smarty_Template_Source::load($_tpl);
105                    if ($_tpl->mustCompile()) {
106                        $_tpl->compileTemplateSource();
107                        $_count++;
108                        echo ' compiled in  ', microtime(true) - $_start_time, ' seconds';
109                        flush();
110                    } else {
111                        echo ' is up to date';
112                        flush();
113                    }
114                } catch (Exception $e) {
115                    echo "\n<br>        ------>Error: ", $e->getMessage(), "<br><br>\n";
116                    $_error_count++;
117                }
118                // free memory
119                unset($_tpl);
120                $_smarty->_clearTemplateCache();
121                if ($max_errors !== null && $_error_count === $max_errors) {
122                    echo "\n<br><br>too many errors\n";
123                    exit(1);
124                }
125            }
126        }
127        echo "\n<br>";
128        return $_count;
129    }
130}
131