00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 class TemplatePathResolver {
00017 public static $resolved_paths = array();
00018 private static $template_paths = array();
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 public static function resolve($resource, $required_file_extension = 'tpl.php') {
00031 $resource = Arr::force($resource, false);
00032 foreach($resource as $res) {
00033 $key = $res;
00034 if (!isset(self::$resolved_paths[$key])) {
00035 $ret = self::find_template($res, $required_file_extension);
00036 self::$resolved_paths[$key] = $ret;
00037 }
00038 else {
00039 $ret = self::$resolved_paths[$key];
00040 }
00041 if ($ret !== false) {
00042 break;
00043 }
00044 }
00045
00046 if ($ret === false) {
00047 throw new Exception('Template file ' . implode(', ', $resource) . ' not found');
00048 }
00049 return $ret;
00050 }
00051
00052 public static function exists($resource, $required_file_extension = 'tpl.php') {
00053 $path = self::find_template($resource, $required_file_extension);
00054 return ($path !== false);
00055 }
00056
00057 private static function find_template($resource, $required_file_extension = 'tpl.php') {
00058 if (substr($resource, 0, 1) == '/') {
00059
00060 return $resource;
00061 }
00062 if (strpos($resource, ':') !== false && strpos($resource, '::') === false) {
00063
00064 return $resource;
00065 }
00066 $resource_to_find = $resource;
00067 if ($required_file_extension) {
00068 $extension = '.' . $required_file_extension;
00069 if (!String::ends_with($resource_to_find, $extension)) {
00070 $resource_to_find .= $extension;
00071 }
00072 }
00073 $paths = self::get_template_paths();
00074 foreach($paths as $path) {
00075 $path .= $resource_to_find;
00076 if (file_exists($path)) {
00077 return $path;
00078 }
00079 }
00080 if (file_exists($resource_to_find)) {
00081 return $resource_to_find;
00082 }
00083
00084
00085 return false;
00086 }
00087
00088 public static function get_template_paths() {
00089 $lang = strtolower(GyroLocale::get_language());
00090 if (empty(self::$template_paths[$lang])) {
00091 $dirs = Load::get_base_directories(Load::ORDER_OVERLOAD);
00092 $ret = array();
00093 foreach($dirs as $dir) {
00094 $test = $dir . 'view/templates/' . $lang . '/';
00095 if (file_exists($test)) {
00096 $ret[] = $test;
00097 }
00098 $test = $dir . 'view/templates/default/';
00099 if (file_exists($test)) {
00100 $ret[] = $test;
00101 }
00102 }
00103 self::$template_paths[$lang] = $ret;
00104 }
00105 return self::$template_paths[$lang];
00106 }
00107 }