/** * populate Source Object with meta data from Resource * * @param Smurty_Template_Source $source source object * @param Smurty_Internal_Template $_template template object * * @throws SmurtyException */ public function populate(Smurty_Template_Source $source, Smurty_Internal_Template $_template = null) { $uid = ''; $sources = array(); $components = explode('|', $source->name); $exists = true; foreach ($components as $component) { $s = Smurty_Resource::source(null, $source->smurty, $component); if ($s->type == 'php') { throw new SmurtyException("Resource type {$s->type} cannot be used with the extends resource type"); } $sources[$s->uid] = $s; $uid .= realpath($s->filepath); if ($_template && $_template->smurty->compile_check) { $exists = $exists && $s->exists; } } $source->components = $sources; $source->filepath = $s->filepath; $source->uid = sha1($uid); if ($_template && $_template->smurty->compile_check) { $source->timestamp = $s->timestamp; $source->exists = $exists; } // need the template at getContent() $source->template = $_template; }
/** * <<magic>> Generic getter. * * @param string $property_name valid: timestamp, exists, content * * @return mixed * @throws SmurtyException if $property_name is not valid */ public function __get($property_name) { switch ($property_name) { case 'timestamp': case 'exists': $this->handler->populateTimestamp($this); return $this->{$property_name}; case 'content': return $this->content = $this->handler->getContent($this); default: throw new SmurtyException("source property '{$property_name}' does not exist."); } }
/** * get hiweb_tpl property in template context * * @param string $property_name property name * * @throws SmurtyException */ public function __get($property_name) { switch ($property_name) { case 'source': if (strlen($this->template_resource) == 0) { throw new SmurtyException('Missing template name'); } $this->source = Smurty_Resource::source($this); // cache template object under a unique ID // do not cache eval resources if ($this->source->type != 'eval') { if ($this->smurty->allow_ambiguous_resources) { $_templateId = $this->source->unique_resource . $this->cache_id . $this->compile_id; } else { $_templateId = $this->smurty->joined_template_dir . '#' . $this->template_resource . $this->cache_id . $this->compile_id; } if (isset($_templateId[150])) { $_templateId = sha1($_templateId); } $this->smurty->template_objects[$_templateId] = $this; } return $this->source; case 'compiled': $this->compiled = $this->source->getCompiled($this); return $this->compiled; case 'cached': if (!class_exists('Smurty_Template_Cached')) { include HIWEB_CORE_HTML_ASSETS . 'smurty_cacheresource.php'; } $this->cached = new Smurty_Template_Cached($this); return $this->cached; case 'compiler': $this->smurty->loadPlugin($this->source->compiler_class); $this->compiler = new $this->source->compiler_class($this->source->template_lexer_class, $this->source->template_parser_class, $this->smurty); return $this->compiler; // FIXME: routing of template -> smurty attributes // FIXME: routing of template -> smurty attributes default: if (property_exists($this->smurty, $property_name)) { return $this->smurty->{$property_name}; } } throw new SmurtyException("template property '{$property_name}' does not exist."); }
/** * Delete compiled template file * * @param string $resource_name template name * @param string $compile_id compile id * @param integer $exp_time expiration time * @param hiweb_tpl $smurty hiweb_tpl instance * * @return integer number of template files deleted */ public static function clearCompiledTemplate($resource_name, $compile_id, $exp_time, hiweb_tpl $smurty) { $_compile_dir = realpath($smurty->getCompileDir()) . '/'; $_compile_id = isset($compile_id) ? preg_replace('![^\\w\\|]+!', '_', $compile_id) : null; $_dir_sep = $smurty->use_sub_dirs ? '/' : '^'; if (isset($resource_name)) { $_save_stat = $smurty->caching; $smurty->caching = false; $tpl = new $smurty->template_class($resource_name, $smurty); $smurty->caching = $_save_stat; // remove from template cache $tpl->source; // have the template registered before unset() if ($smurty->allow_ambiguous_resources) { $_templateId = $tpl->source->unique_resource . $tpl->cache_id . $tpl->compile_id; } else { $_templateId = $smurty->joined_template_dir . '#' . $resource_name . $tpl->cache_id . $tpl->compile_id; } if (isset($_templateId[150])) { $_templateId = sha1($_templateId); } unset($smurty->template_objects[$_templateId]); if ($tpl->source->exists) { $_resource_part_1 = basename(str_replace('^', '/', $tpl->compiled->filepath)); $_resource_part_1_length = strlen($_resource_part_1); } else { return 0; } $_resource_part_2 = str_replace('.php', '.cache.php', $_resource_part_1); $_resource_part_2_length = strlen($_resource_part_2); } $_dir = $_compile_dir; if ($smurty->use_sub_dirs && isset($_compile_id)) { $_dir .= $_compile_id . $_dir_sep; } if (isset($_compile_id)) { $_compile_id_part = str_replace('\\', '/', $_compile_dir . $_compile_id . $_dir_sep); $_compile_id_part_length = strlen($_compile_id_part); } $_count = 0; try { $_compileDirs = new RecursiveDirectoryIterator($_dir); // NOTE: UnexpectedValueException thrown for PHP >= 5.3 } catch (Exception $e) { return 0; } $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST); foreach ($_compile as $_file) { if (substr(basename($_file->getPathname()), 0, 1) == '.' || strpos($_file, '.svn') !== false) { continue; } $_filepath = str_replace('\\', '/', (string) $_file); if ($_file->isDir()) { if (!$_compile->isDot()) { // delete folder if empty @rmdir($_file->getPathname()); } } else { $unlink = false; if ((!isset($_compile_id) || isset($_filepath[$_compile_id_part_length]) && ($a = !strncmp($_filepath, $_compile_id_part, $_compile_id_part_length))) && (!isset($resource_name) || isset($_filepath[$_resource_part_1_length]) && substr_compare($_filepath, $_resource_part_1, -$_resource_part_1_length, $_resource_part_1_length) == 0 || isset($_filepath[$_resource_part_2_length]) && substr_compare($_filepath, $_resource_part_2, -$_resource_part_2_length, $_resource_part_2_length) == 0)) { if (isset($exp_time)) { if (time() - @filemtime($_filepath) >= $exp_time) { $unlink = true; } } else { $unlink = true; } } if ($unlink && @unlink($_filepath)) { $_count++; } } } // clear compiled cache Smurty_Resource::$sources = array(); Smurty_Resource::$compileds = array(); return $_count; }
/** * creates a template object * * @param string $template the resource handle of the template file * @param mixed $cache_id cache id to be used with this template * @param mixed $compile_id compile id to be used with this template * @param object $parent next higher level of hiweb_tpl variables * @param boolean $do_clone flag is hiweb_tpl object shall be cloned * * @return object template object */ public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null, $do_clone = true) { if ($cache_id !== null && (is_object($cache_id) || is_array($cache_id))) { $parent = $cache_id; $cache_id = null; } if ($parent !== null && is_array($parent)) { $data = $parent; $parent = null; } else { $data = null; } // default to cache_id and compile_id of hiweb_tpl object $cache_id = $cache_id === null ? $this->cache_id : $cache_id; $compile_id = $compile_id === null ? $this->compile_id : $compile_id; // already in template cache? if ($this->allow_ambiguous_resources) { $_templateId = Smurty_Resource::getUniqueTemplateName($this, $template) . $cache_id . $compile_id; } else { $_templateId = $this->joined_template_dir . '#' . $template . $cache_id . $compile_id; } if (isset($_templateId[150])) { $_templateId = sha1($_templateId); } if ($do_clone) { if (isset($this->template_objects[$_templateId])) { // return cached template object $tpl = clone $this->template_objects[$_templateId]; $tpl->smurty = clone $tpl->smurty; $tpl->parent = $parent; $tpl->tpl_vars = array(); $tpl->config_vars = array(); } else { $tpl = new $this->template_class($template, clone $this, $parent, $cache_id, $compile_id); } } else { if (isset($this->template_objects[$_templateId])) { // return cached template object $tpl = $this->template_objects[$_templateId]; $tpl->parent = $parent; $tpl->tpl_vars = array(); $tpl->config_vars = array(); } else { $tpl = new $this->template_class($template, $this, $parent, $cache_id, $compile_id); } } // fill data if present if (!empty($data) && is_array($data)) { // set up variable values foreach ($data as $_key => $_val) { $tpl->tpl_vars[$_key] = new Smurty_variable($_val); } } return $tpl; }
/** * get hiweb_tpl property in template context * * @param string $property_name property name * * @return \Smurty_Config_Source|\Smurty_Template_Compiled * @throws SmurtyException if $property_name is not valid */ public function __get($property_name) { switch ($property_name) { case 'source': if (empty($this->config_resource)) { throw new SmurtyException("Unable to parse resource name \"{$this->config_resource}\""); } $this->source = Smurty_Resource::config($this); return $this->source; case 'compiled': $this->compiled = $this->source->getCompiled($this); return $this->compiled; } throw new SmurtyException("config attribute '{$property_name}' does not exist."); }