Code

Updated smarty to 1.0.9
[gosa.git] / gosa-core / include / smarty / sysplugins / smarty_internal_config.php
index 31b410857cd09b79ff59a06da8945755a24e0ec9..5355512338c3f4a259927125f22bbf0a4585291b 100644 (file)
@@ -1,20 +1,20 @@
 <?php
 /**
-* Smarty Internal Plugin Config
-* 
-* Main class for config variables
-* 
-* @ignore 
-* @package Smarty
-* @subpackage Config
-* @author Uwe Tews 
-*/
+ * Smarty Internal Plugin Config
+ 
+ * Main class for config variables
+ 
+ * @ignore 
+ * @package Smarty
+ * @subpackage Config
+ * @author Uwe Tews 
+ */
 class Smarty_Internal_Config {
     static $config_objects = array();
 
-    public function __construct($config_resource, $smarty, $template = null)
+    public function __construct($config_resource, $smarty, $data = null)
     {
-        $this->template = $template;
+        $this->data = $data;
         $this->smarty = $smarty;
         $this->config_resource = $config_resource;
         $this->config_resource_type = null;
@@ -29,7 +29,7 @@ class Smarty_Internal_Config {
         $this->compiler_object = null; 
         // parse config resource name
         if (!$this->parseConfigResourceName ($config_resource)) {
-            throw new Exception ("Unable to parse config resource '{$config_resource}'");
+            throw new SmartyException ("Unable to parse config resource '{$config_resource}'");
         } 
     } 
 
@@ -85,28 +85,28 @@ class Smarty_Internal_Config {
         } 
         // check for absolute path
         if (file_exists($this->config_resource_name))
-            return $this->config_resource_name;
+            return $this->config_resource_name; 
         // no tpl file found
-        throw new Exception("Unable to load config file \"{$this->config_resource_name}\"");
+        throw new SmartyException("Unable to load config file \"{$this->config_resource_name}\"");
         return false;
     } 
     /**
-    * Read config file source
-    * 
-    * @return string content of source file
-    */
+     * Read config file source
+     
+     * @return string content of source file
+     */
     /**
-    * Returns the template source code
-    * 
-    * The template source is being read by the actual resource handler
-    * 
-    * @return string the template source
-    */
+     * Returns the template source code
+     
+     * The template source is being read by the actual resource handler
+     
+     * @return string the template source
+     */
     public function getConfigSource ()
     {
         if ($this->config_source === null) {
             if ($this->readConfigSource($this) === false) {
-                throw new Exception("Unable to load config file \"{$this->config_resource_name}\"");
+                throw new SmartyException("Unable to load config file \"{$this->config_resource_name}\"");
             } 
         } 
         return $this->config_source;
@@ -123,10 +123,10 @@ class Smarty_Internal_Config {
     } 
 
     /**
-    * Returns the compiled  filepath
-    * 
-    * @return string the compiled filepath
-    */
+     * Returns the compiled  filepath
+     
+     * @return string the compiled filepath
+     */
     public function getCompiledFilepath ()
     {
         return $this->compiled_filepath === null ?
@@ -135,6 +135,7 @@ class Smarty_Internal_Config {
     } 
     public function buildCompiledFilepath()
     {
+        $_compile_id = isset($this->smarty->compile_id) ? preg_replace('![^\w\|]+!', '_', $this->smarty->compile_id) : null;
         $_flag = (int)$this->smarty->config_read_hidden + (int)$this->smarty->config_booleanize * 2 +
         (int)$this->smarty->config_overwrite * 4;
         $_filepath = sha1($this->config_resource_name . $_flag); 
@@ -145,6 +146,10 @@ class Smarty_Internal_Config {
              . substr($_filepath, 4, 2) . DS
              . $_filepath;
         } 
+        $_compile_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
+        if (isset($_compile_id)) {
+            $_filepath = $_compile_id . $_compile_dir_sep . $_filepath;
+        } 
         $_compile_dir = $this->smarty->compile_dir;
         if (substr($_compile_dir, -1) != DS) {
             $_compile_dir .= DS;
@@ -152,10 +157,10 @@ class Smarty_Internal_Config {
         return $_compile_dir . $_filepath . '.' . basename($this->config_resource_name) . '.config' . '.php';
     } 
     /**
-    * Returns the timpestamp of the compiled file
-    * 
-    * @return integer the file timestamp
-    */
+     * Returns the timpestamp of the compiled file
+     
+     * @return integer the file timestamp
+     */
     public function getCompiledTimestamp ()
     {
         return $this->compiled_timestamp === null ?
@@ -163,25 +168,25 @@ class Smarty_Internal_Config {
         $this->compiled_timestamp;
     } 
     /**
-    * Returns if the current config file must be compiled 
-    * 
-    * It does compare the timestamps of config source and the compiled config and checks the force compile configuration
-    * 
-    * @return boolean true if the file must be compiled
-    */
+     * Returns if the current config file must be compiled 
+     
+     * It does compare the timestamps of config source and the compiled config and checks the force compile configuration
+     
+     * @return boolean true if the file must be compiled
+     */
     public function mustCompile ()
     {
         return $this->mustCompile === null ?
-        $this->mustCompile = ($this->smarty->force_compile || $this->getCompiledTimestamp () !== $this->getTimestamp ()):
+        $this->mustCompile = ($this->smarty->force_compile || $this->getCompiledTimestamp () === false || $this->smarty->compile_check && $this->getCompiledTimestamp () < $this->getTimestamp ()):
         $this->mustCompile;
     } 
     /**
-    * Returns the compiled config file 
-    * 
-    * It checks if the config file must be compiled or just read the compiled version
-    * 
-    * @return string the compiled config file
-    */
+     * Returns the compiled config file 
+     
+     * It checks if the config file must be compiled or just read the compiled version
+     
+     * @return string the compiled config file
+     */
     public function getCompiledConfig ()
     {
         if ($this->compiled_config === null) {
@@ -196,8 +201,8 @@ class Smarty_Internal_Config {
     } 
 
     /**
-    * Compiles the config files
-    */
+     * Compiles the config files
+     */
     public function compileConfigSource ()
     { 
         // compile template
@@ -205,18 +210,26 @@ class Smarty_Internal_Config {
             // load compiler
             $this->compiler_object = new Smarty_Internal_Config_File_Compiler($this->smarty);
         } 
+        // compile locking
+        if ($this->smarty->compile_locking) {
+            if ($saved_timestamp = $this->getCompiledTimestamp()) {
+                touch($this->getCompiledFilepath());
+            } 
+        } 
         // call compiler
-        if ($this->compiler_object->compileSource($this)) {
-            // compiling succeded
-            // write compiled template
-            Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath(), $this->getCompiledConfig(), $this->smarty); 
-            // make template and compiled file timestamp match
-            touch($this->getCompiledFilepath(), $this->getTimestamp());
-        } else {
-            // error compiling template
-            throw new Exception("Error compiling template {$this->getConfigFilepath ()}");
-            return false;
+        try {
+            $this->compiler_object->compileSource($this);
         } 
+        catch (Exception $e) {
+            // restore old timestamp in case of error
+            if ($this->smarty->compile_locking && $saved_timestamp) {
+                touch($this->getCompiledFilepath(), $saved_timestamp);
+            } 
+            throw $e;
+        } 
+        // compiling succeded
+        // write compiled template
+        Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath(), $this->getCompiledConfig(), $this->smarty);
     } 
 
     /*
@@ -225,36 +238,53 @@ class Smarty_Internal_Config {
     * @param mixed $sections array of section names, single section or null
     * @param object $scope global,parent or local
     */
-    public function loadConfigVars ($sections = null, $scope)
+    public function loadConfigVars ($sections = null, $scope = 'local')
     {
-        if (isset($this->template)) {
-            $this->template->properties['file_dependency'][sha1($this->getConfigFilepath())] = array($this->getConfigFilepath(), $this->getTimestamp());
-        } else {
-            $this->smarty->properties['file_dependency'][sha1($this->getConfigFilepath())] = array($this->getConfigFilepath(), $this->getTimestamp());
+        if ($this->data instanceof Smarty_Internal_Template) {
+            $this->data->properties['file_dependency'][sha1($this->getConfigFilepath())] = array($this->getConfigFilepath(), $this->getTimestamp(),'file');
         } 
-        $config_data = unserialize($this->getCompiledConfig()); 
-        // var_dump($config_data);
+        if ($this->mustCompile()) {
+            $this->compileConfigSource();
+        }
+        // pointer to scope
+        if ($scope == 'local') {
+               $scope_ptr = $this->data;
+        } elseif ($scope == 'parent') {
+               if (isset($this->data->parent)) {
+                       $scope_ptr = $this->data->parent;
+               } else {
+                       $scope_ptr = $this->data;
+               }                       
+        } elseif ($scope == 'root' || $scope == 'global') {
+               $scope_ptr = $this->data;
+               while (isset($scope_ptr->parent)) {
+                       $scope_ptr = $scope_ptr->parent;
+               } 
+        }
+        $_config_vars = array();
+        include($this->getCompiledFilepath ());
         // copy global config vars
-        foreach ($config_data['vars'] as $variable => $value) {
-            if ($this->smarty->config_overwrite || !isset($scope->config_vars[$variable])) {
-                $scope->config_vars[$variable] = $value;
+        foreach ($_config_vars['vars'] as $variable => $value) {
+            if ($this->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
+                $scope_ptr->config_vars[$variable] = $value;
             } else {
-                $scope->config_vars[$variable] = array_merge((array)$scope->config_vars[$variable], (array)$value);
+                $scope_ptr->config_vars[$variable] = array_merge((array)$scope_ptr->config_vars[$variable], (array)$value);
             } 
         } 
         // scan sections
-        foreach ($config_data['sections'] as $this_section => $dummy) {
-            if ($sections == null || in_array($this_section, (array)$sections)) {
-                foreach ($config_data['sections'][$this_section]['vars'] as $variable => $value) {
-                    if ($this->smarty->config_overwrite || !isset($scope->config_vars[$variable])) {
-                        $scope->config_vars[$variable] = $value;
-                    } else {
-                        $scope->config_vars[$variable] = array_merge((array)$scope->config_vars[$variable], (array)$value);
-                    } 
-                } 
-            } 
-        } 
+        if(!empty($sections)) {
+          foreach ($_config_vars['sections'] as $this_section => $dummy) {
+              if (in_array($this_section, (array)$sections)) {
+                  foreach ($_config_vars['sections'][$this_section]['vars'] as $variable => $value) {
+                      if ($this->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
+                          $scope_ptr->config_vars[$variable] = $value;
+                      } else {
+                          $scope_ptr->config_vars[$variable] = array_merge((array)$scope_ptr->config_vars[$variable], (array)$value);
+                      } 
+                  } 
+              } 
+          }
+        }
     } 
 } 
-
-?>
+?>
\ No newline at end of file