MODX Revolution PHP Cross Reference Content Management Systems

Source: /core/model/smarty/sysplugins/smarty_internal_utility.php - 296 lines - 11821 bytes - Summary - Text - Print

   1  <?php
   2  
   3  /**
   4   * Project:     Smarty: the PHP compiling template engine
   5   * File:        smarty_internal_utility.php
   6   * SVN:         $Id: $
   7   * 
   8   * This library is free software; you can redistribute it and/or
   9   * modify it under the terms of the GNU Lesser General Public
  10   * License as published by the Free Software Foundation; either
  11   * version 2.1 of the License, or (at your option) any later version.
  12   * 
  13   * This library is distributed in the hope that it will be useful,
  14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16   * Lesser General Public License for more details.
  17   * 
  18   * You should have received a copy of the GNU Lesser General Public
  19   * License along with this library; if not, write to the Free Software
  20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21   * 
  22   * For questions, help, comments, discussion, etc., please join the
  23   * Smarty mailing list. Send a blank e-mail to
  24   * smarty-discussion-subscribe@googlegroups.com
  25   * 
  26   * @link http://www.smarty.net/
  27   * @copyright 2008 New Digital Group, Inc.
  28   * @author Monte Ohrt <monte at ohrt dot com> 
  29   * @author Uwe Tews 
  30   * @package Smarty
  31   * @subpackage PluginsInternal
  32   * @version 3-SVN$Rev: 3286 $
  33   */
  34  
  35  class Smarty_Internal_Utility {
  36      protected $smarty;
  37  
  38      function __construct($smarty)
  39      {
  40          $this->smarty = $smarty;
  41      } 
  42  
  43      /**
  44       * Compile all template files
  45       * 
  46       * @param string $extension file extension
  47       * @param bool $force_compile force all to recompile
  48       * @param int $time_limit 
  49       * @param int $max_errors 
  50       * @return integer number of template files recompiled
  51       */
  52      function compileAllTemplates($extention = '.tpl', $force_compile = false, $time_limit = 0, $max_errors = null)
  53      { 
  54          // switch off time limit
  55          if (function_exists('set_time_limit')) {
  56              @set_time_limit($time_limit);
  57          } 
  58          $this->smarty->force_compile = $force_compile;
  59          $_count = 0;
  60          $_error_count = 0; 
  61          // loop over array of template directories
  62          foreach((array)$this->smarty->template_dir as $_dir) {
  63              if (strpos('/\\', substr($_dir, -1)) === false) {
  64                  $_dir .= DS;
  65              } 
  66              $_compileDirs = new RecursiveDirectoryIterator($_dir);
  67              $_compile = new RecursiveIteratorIterator($_compileDirs);
  68              foreach ($_compile as $_fileinfo) {
  69                  if (strpos($_fileinfo, '.svn') !== false) continue;
  70                  $_file = $_fileinfo->getFilename();
  71                  if (!substr_compare($_file, $extention, - strlen($extention)) == 0) continue;
  72                  if ($_fileinfo->getPath() == substr($_dir, 0, -1)) {
  73                     $_template_file = $_file;
  74                  } else {
  75                     $_template_file = substr($_fileinfo->getPath(), strlen($_dir)) . DS . $_file;
  76                  }
  77                  echo '<br>', $_dir, '---', $_template_file;
  78                  flush();
  79                  $_start_time = microtime(true);
  80                  try {
  81                      $_tpl = $this->smarty->createTemplate($_template_file);
  82                      if ($_tpl->mustCompile()) {
  83                          $_tpl->compileTemplateSource();
  84                          echo ' compiled in  ', microtime(true) - $_start_time, ' seconds';
  85                          flush();
  86                      } else {
  87                          echo ' is up to date';
  88                          flush();
  89                      } 
  90                  } 
  91                  catch (Exception $e) {
  92                      echo 'Error: ', $e->getMessage(), "<br><br>";
  93                      $_error_count++;
  94                  } 
  95                  if ($max_errors !== null && $_error_count == $max_errors) {
  96                      echo '<br><br>too many errors';
  97                      exit();
  98                  } 
  99              } 
 100          } 
 101          return $_count;
 102      } 
 103  
 104      /**
 105       * Compile all config files
 106       * 
 107       * @param string $extension file extension
 108       * @param bool $force_compile force all to recompile
 109       * @param int $time_limit 
 110       * @param int $max_errors 
 111       * @return integer number of template files recompiled
 112       */
 113      function compileAllConfig($extention = '.conf', $force_compile = false, $time_limit = 0, $max_errors = null)
 114      { 
 115          // switch off time limit
 116          if (function_exists('set_time_limit')) {
 117              @set_time_limit($time_limit);
 118          } 
 119          $this->smarty->force_compile = $force_compile;
 120          $_count = 0;
 121          $_error_count = 0; 
 122          // loop over array of template directories
 123          foreach((array)$this->smarty->config_dir as $_dir) {
 124              if (strpos('/\\', substr($_dir, -1)) === false) {
 125                  $_dir .= DS;
 126              } 
 127              $_compileDirs = new RecursiveDirectoryIterator($_dir);
 128              $_compile = new RecursiveIteratorIterator($_compileDirs);
 129              foreach ($_compile as $_fileinfo) {
 130                  if (strpos($_fileinfo, '.svn') !== false) continue;
 131                  $_file = $_fileinfo->getFilename();
 132                  if (!substr_compare($_file, $extention, - strlen($extention)) == 0) continue;
 133                  if ($_fileinfo->getPath() == substr($_dir, 0, -1)) {
 134                      $_config_file = $_file;
 135                  } else {
 136                      $_config_file = substr($_fileinfo->getPath(), strlen($_dir)) . DS . $_file;
 137                  } 
 138                  echo '<br>', $_dir, '---', $_config_file;
 139                  flush();
 140                  $_start_time = microtime(true);
 141                  try {
 142                      $_config = new Smarty_Internal_Config($_config_file, $this->smarty);
 143                      if ($_config->mustCompile()) {
 144                          $_config->compileConfigSource();
 145                          echo ' compiled in  ', microtime(true) - $_start_time, ' seconds';
 146                          flush();
 147                      } else {
 148                          echo ' is up to date';
 149                          flush();
 150                      } 
 151                  } 
 152                  catch (Exception $e) {
 153                      echo 'Error: ', $e->getMessage(), "<br><br>";
 154                      $_error_count++;
 155                  } 
 156                  if ($max_errors !== null && $_error_count == $max_errors) {
 157                      echo '<br><br>too many errors';
 158                      exit();
 159                  } 
 160              } 
 161          } 
 162          return $_count;
 163      } 
 164  
 165      /**
 166       * Delete compiled template file
 167       * 
 168       * @param string $resource_name template name
 169       * @param string $compile_id compile id
 170       * @param integer $exp_time expiration time
 171       * @return integer number of template files deleted
 172       */
 173      function clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null)
 174      {
 175          $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;
 176          $_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
 177          if (isset($resource_name)) {
 178              $_resource_part_1 = $resource_name . '.php';
 179              $_resource_part_2 = $resource_name . '.cache' . '.php';
 180          } else {
 181              $_resource_part = '';
 182          } 
 183          $_dir = $this->smarty->compile_dir;
 184          if ($this->smarty->use_sub_dirs && isset($_compile_id)) {
 185              $_dir .= $_compile_id . $_dir_sep;
 186          } 
 187          if (isset($_compile_id)) {
 188              $_compile_id_part = $this->smarty->compile_dir . $_compile_id . $_dir_sep;
 189          } 
 190          $_count = 0;
 191          $_compileDirs = new RecursiveDirectoryIterator($_dir);
 192          $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST);
 193          foreach ($_compile as $_file) {
 194              if (strpos($_file, '.svn') !== false) continue;
 195              if ($_file->isDir()) {
 196                  if (!$_compile->isDot()) {
 197                      // delete folder if empty
 198                      @rmdir($_file->getPathname());
 199                  } 
 200              } else {
 201                  if ((!isset($_compile_id) || (strlen((string)$_file) > strlen($_compile_id_part) && substr_compare((string)$_file, $_compile_id_part, 0, strlen($_compile_id_part)) == 0)) &&
 202                          (!isset($resource_name) || (strlen((string)$_file) > strlen($_resource_part_1) && substr_compare((string)$_file, $_resource_part_1, - strlen($_resource_part_1), strlen($_resource_part_1)) == 0) ||
 203                              (strlen((string)$_file) > strlen($_resource_part_2) && substr_compare((string)$_file, $_resource_part_2, - strlen($_resource_part_2), strlen($_resource_part_2)) == 0))) {
 204                      if (isset($exp_time)) {
 205                          if (time() - @filemtime($_file) >= $exp_time) {
 206                              $_count += @unlink((string) $_file) ? 1 : 0;
 207                          } 
 208                      } else {
 209                          $_count += @unlink((string) $_file) ? 1 : 0;
 210                      } 
 211                  } 
 212              } 
 213          } 
 214          return $_count;
 215      } 
 216  
 217      /**
 218       * Return array of tag/attributes of all tags used by an template
 219       * 
 220       * @param object $templae template object
 221       * @return array of tag/attributes
 222       */
 223  	function getTags(Smarty_Internal_Template $template) 
 224      {
 225          $template->smarty->get_used_tags = true;
 226          $template->compileTemplateSource();
 227          return $template->compiler_object->used_tags;
 228      }    
 229      
 230      function testInstall()
 231      {
 232          echo "<PRE>\n";
 233  
 234          echo "Smarty Installation test...\n";
 235  
 236          echo "Testing template directory...\n";
 237  
 238          foreach((array)$this->smarty->template_dir as $template_dir) {
 239              if (!is_dir($template_dir))
 240                  echo "FAILED: $template_dir is not a directory.\n";
 241              elseif (!is_readable($template_dir))
 242                  echo "FAILED: $template_dir is not readable.\n";
 243              else
 244                  echo "$template_dir is OK.\n";
 245          } 
 246  
 247          echo "Testing compile directory...\n";
 248  
 249          if (!is_dir($this->smarty->compile_dir))
 250              echo "FAILED: {$this->smarty->compile_dir} is not a directory.\n";
 251          elseif (!is_readable($this->smarty->compile_dir))
 252              echo "FAILED: {$this->smarty->compile_dir} is not readable.\n";
 253          elseif (!is_writable($this->smarty->compile_dir))
 254              echo "FAILED: {$this->smarty->compile_dir} is not writable.\n";
 255          else
 256              echo "{$this->smarty->compile_dir} is OK.\n";
 257  
 258          echo "Testing plugins directory...\n";
 259  
 260          foreach((array)$this->smarty->plugins_dir as $plugin_dir) {
 261              if (!is_dir($plugin_dir))
 262                  echo "FAILED: $plugin_dir is not a directory.\n";
 263              elseif (!is_readable($plugin_dir))
 264                  echo "FAILED: $plugin_dir is not readable.\n";
 265              else
 266                  echo "$plugin_dir is OK.\n";
 267          } 
 268  
 269          echo "Testing cache directory...\n";
 270  
 271          if (!is_dir($this->smarty->cache_dir))
 272              echo "FAILED: {$this->smarty->cache_dir} is not a directory.\n";
 273          elseif (!is_readable($this->smarty->cache_dir))
 274              echo "FAILED: {$this->smarty->cache_dir} is not readable.\n";
 275          elseif (!is_writable($this->smarty->cache_dir))
 276              echo "FAILED: {$this->smarty->cache_dir} is not writable.\n";
 277          else
 278              echo "{$this->smarty->cache_dir} is OK.\n";
 279  
 280          echo "Testing configs directory...\n";
 281  
 282          if (!is_dir($this->smarty->config_dir))
 283              echo "FAILED: {$this->smarty->config_dir} is not a directory.\n";
 284          elseif (!is_readable($this->smarty->config_dir))
 285              echo "FAILED: {$this->smarty->config_dir} is not readable.\n";
 286          else
 287              echo "{$this->smarty->config_dir} is OK.\n";
 288  
 289          echo "Tests complete.\n";
 290  
 291          echo "</PRE>\n";
 292  
 293          return true;
 294      } 
 295  }
 296  ?>

title

Description

title

Description

title

Description

title

title

Body