00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010 class StaticPagesController extends ControllerBase {
00011 private $cache_templates = null;
00012
00013
00014
00015
00016 public function get_routes() {
00017 $templates = $this->collect_templates();
00018 $ret = array();
00019 foreach ($templates as $template => $path) {
00020
00021 $ret[] = new StaticPageRoute(STATICPAGES_PREPEND, $path, STATICPAGES_APPEND, $template, $this, 'static');
00022 }
00023
00024 $ret[] = new StaticPageParamterizedRoute(STATICPAGES_PREPEND . '{page:s}' . STATICPAGES_APPEND, $this, 'static');
00025
00026 return $ret;
00027 }
00028
00029
00030
00031
00032
00033
00034
00035 public function action_static($page_data, $page) {
00036 $path = 'static/' . $page;
00037 $template_file = TemplatePathResolver::resolve($path);
00038 if (!file_exists($template_file)) {
00039 return CONTROLLER_NOT_FOUND;
00040 }
00041
00042 $cache = $page_data->get_cache_manager();
00043 if ($cache) {
00044 $cache->set_cache_duration(GyroDate::ONE_DAY);
00045 }
00046 $view = ViewFactory::create_view(IViewFactory::CONTENT, $template_file, $page_data);
00047 $view->render();
00048 }
00049
00050
00051
00052
00053
00054
00055 protected function collect_templates() {
00056 if (is_null($this->cache_templates)) {
00057 $this->cache_templates = array();
00058 $dirs = TemplatePathResolver::get_template_paths();
00059 foreach($dirs as $dir) {
00060 $statics_dir = $dir . 'static';
00061 if (is_dir($statics_dir)) {
00062 $this->collect_templates_in_dir($statics_dir, '');
00063 }
00064 }
00065 }
00066 return $this->cache_templates;
00067 }
00068
00069 protected function collect_templates_in_dir($dir_path, $template_prefix) {
00070 $it = new DirectoryIterator($dir_path);
00071 foreach($it as $fileinfo) {
00072 if (!$fileinfo->isDot()) {
00073 $file = $fileinfo->getFilename();
00074 if ($fileinfo->isDir()) {
00075 $this->collect_templates_in_dir($fileinfo->getPathname(), $template_prefix . $file . '/');
00076 }
00077 else if(substr($file, -8, 8) === '.tpl.php') {
00078 $file = basename(substr($file, 0, -8));
00079 $tpl = $template_prefix . $file;
00080 $path = $template_prefix;
00081 if ($file != 'index') {
00082 $path .= $file;
00083 }
00084 $this->cache_templates[$tpl] = $path;
00085 }
00086 }
00087 }
00088 }
00089
00090
00091
00092
00093
00094
00095
00096 public function on_event($name, $params, &$result) {
00097 if ($name == 'gsitemap_site' && $params == 'main') {
00098 $arr = $this->collect_templates();
00099 foreach($arr as $template => $path) {
00100 $result[] = array(
00101 'url' => ActionMapper::get_url('static', array('page' => $path)),
00102 'lastmod' => filemtime(TemplatePathResolver::resolve('static/' . $template))
00103 );
00104 }
00105 }
00106 }
00107 }