Example #1
0
 /**
  * Compile XE-compatible XML lang files into PHP.
  * 
  * @param string $filename
  * @param string $language
  * @return string|false
  */
 public static function compileXMLtoPHP($filename, $language, $output_filename = null)
 {
     // Check if the cache file already exists.
     if ($output_filename === null) {
         $output_filename = \RX_BASEDIR . 'files/cache/lang/' . md5($filename) . '.' . $language . '.php';
         if (file_exists($output_filename) && filemtime($output_filename) > filemtime($filename)) {
             return $output_filename;
         }
     }
     // Load the XML lang file.
     $xml = simplexml_load_string(Storage::read($filename));
     if ($xml === false) {
         Storage::write($output_filename, '');
         return false;
     }
     // Convert XML to a PHP array.
     $lang = array();
     self::_toArray($xml, $lang, $language);
     unset($xml);
     // Save the array as a cache file.
     $buff = "<?php\n// {$filename}\n";
     foreach ($lang as $key => $value) {
         if (is_array($value)) {
             foreach ($value as $subkey => $subvalue) {
                 if (is_array($subvalue)) {
                     foreach ($subvalue as $subsubkey => $subsubvalue) {
                         $buff .= '$lang->' . $key . "['{$subkey}']['{$subsubkey}']" . ' = ' . var_export($subsubvalue, true) . ";\n";
                     }
                 } else {
                     $buff .= '$lang->' . $key . "['{$subkey}']" . ' = ' . var_export($subvalue, true) . ";\n";
                 }
             }
         } else {
             $buff .= '$lang->' . $key . ' = ' . var_export($value, true) . ";\n";
         }
     }
     Storage::write($output_filename, $buff);
     return $output_filename;
 }