/** * populate Source Object with meta data from Resource * * @param TemplateSource $source source object * @param Template $_template template object */ public function populate(TemplateSource $source, Template $_template = null) { $uid = ''; $sources = array(); $components = explode('|', $source->name); $exists = true; foreach ($components as $component) { $s = Resource::source(null, $source->smarty, $component); if ($s->type == 'php') { throw new SmartyException("Resource type {$s->type} cannot be used with the extends resource type"); } $sources[$s->uid] = $s; $uid .= $s->filepath; if ($_template && $_template->smarty->compile_check) { $exists = $exists && $s->exists; } } $source->components = $sources; $source->filepath = realpath($s->filepath); $source->uid = sha1($uid); if ($_template && $_template->smarty->compile_check) { $source->timestamp = $s->timestamp; $source->exists = $exists; } // need the template at getContent() $source->template = $_template; }
/** * populate Source Object with meta data from Resource * * @param TemplateSource $source source object * @param Template $_template template object * @return void */ public function populate(TemplateSource $source, Template $_template = null) { $uid = ''; $sources = array(); $exists = true; foreach ($_template->smarty->getTemplateDir() as $key => $directory) { try { $s = \Box\Brainy\Resources\Resource::source(null, $source->smarty, '[' . $key . ']' . $source->name); if (!$s->exists) { continue; } $sources[$s->uid] = $s; $uid .= $s->filepath; } catch (SmartyException $e) { } } $sources = array_reverse($sources, true); reset($sources); $s = current($sources); $source->components = $sources; $source->filepath = $s->filepath; $source->uid = sha1($uid); $source->exists = $exists; if ($_template && $_template->smarty->compile_check) { $source->timestamp = $s->timestamp; } // need the template at getContent() $source->template = $_template; }
/** * @param string $property_name valid: timestamp, exists, content * @return mixed * @throws SmartyException if $property_name is not valid */ public function __get($property_name) { switch ($property_name) { case 'timestamp': case 'exists': $this->handler->populateTimestamp($this); return $this->exists; default: throw new SmartyException("source property '{$property_name}' does not exist."); } }
public function setUp() { $this->smarty = new \Box\Brainy\Brainy(); $this->setUpInstance($this->smarty); $this->smartyBC = new \Box\Brainy\SmartyBC(); $this->setUpInstance($this->smartyBC); \Box\Brainy\Brainy::$default_assign_scope = \Box\Brainy\Brainy::SCOPE_LOCAL; \Box\Brainy\Brainy::$enforce_expression_modifiers = array(); \Box\Brainy\Brainy::$global_tpl_vars = array(); \Box\Brainy\Resources\Resource::reset(); $this->clearFiles(); parent::setUp(); }
/** * generate compiled files * @uses $_files to store references * @return array list of files array( id => path ) */ protected function makeFiles() { $this->_files = array(); $directory_length = strlen($this->smarty->getCompileDir()); $templates = array('helloworld.tpl' => array(null, 'compile1', 'compile2'), 'helloworld2.tpl' => array(null, 'compile1', 'compile2'), 'ambiguous/case1/foobar.tpl' => array(null, 'compile1', 'compile2'), '[1]ambiguous/case1/foobar.tpl' => array(null, 'compile1', 'compile2')); foreach ($templates as $template => $compile_ids) { foreach ($compile_ids as $compile_id) { $tpl = $this->smarty->createTemplate($template, $compile_id); $tpl->fetch(); // Force a compile and render $this->_files[$template . '#' . $compile_id] = substr($tpl->compiled->filepath, $directory_length - 1); } } \Box\Brainy\Resources\Resource::reset(); return $this->_files; }
/** * Delete compiled template file * * @param string $resource_name template name * @param string $compile_id compile id * @param integer $exp_time expiration time * @param \Box\Brainy\Brainy $smarty \Box\Brainy\Brainy instance * @return integer number of template files deleted */ public static function clearCompiledTemplate($resource_name, $compile_id, $exp_time, \Box\Brainy\Brainy $smarty) { $_compile_dir = realpath($smarty->getCompileDir()) . '/'; $_compile_id = isset($compile_id) ? preg_replace('![^\\w\\|]+!', '_', $compile_id) : null; $_dir_sep = $smarty->use_sub_dirs ? '/' : '^'; if (isset($resource_name)) { $tpl = new \Box\Brainy\Templates\Template($resource_name, $smarty); // remove from template cache if (!$tpl->source->exists) { return 0; } $_resource_part_1 = basename(str_replace('^', '/', $tpl->compiled->filepath)); $_resource_part_1_length = strlen($_resource_part_1); } $_dir = $_compile_dir; if ($smarty->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); } catch (\UnexpectedValueException $e) { // NOTE: UnexpectedValueException thrown for PHP >= 5.3 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)) { if (isset($exp_time)) { if (time() - @filemtime($_filepath) >= $exp_time) { $unlink = true; } } else { $unlink = true; } } if ($unlink && @unlink($_filepath)) { $_count++; } } } // clear compiled cache \Box\Brainy\Resources\Resource::reset(); return $_count; }
/** * This function is executed automatically when a compiled or cached template file is included * * @param array $properties special template properties * @return bool flag if compiled or cache file is valid */ public function decodeProperties($properties) { if (isset($properties['file_dependency'])) { foreach ($properties['file_dependency'] as $key => $val) { $this->properties['file_dependency'][$key] = $val; } } $this->properties['version'] = isset($properties['version']) ? $properties['version'] : ''; $this->properties['unifunc'] = $properties['unifunc']; // check file dependencies at compiled code if ($this->properties['version'] != Brainy::SMARTY_VERSION) { return false; } if ($this->smarty->compile_check && empty($this->compiled->properties) && !$this->compiled->isCompiled && !empty($this->properties['file_dependency'])) { foreach ($this->properties['file_dependency'] as $_file_to_check) { if ($_file_to_check[2] == 'file') { if ($this->source->filepath == $_file_to_check[0] && isset($this->source->timestamp)) { // do not recheck current template $mtime = $this->source->timestamp; } else { // file and php types can be checked without loading the respective resource handlers $mtime = @filemtime($_file_to_check[0]); } } elseif ($_file_to_check[2] == 'string') { continue; } else { $source = \Box\Brainy\Resources\Resource::source(null, $this->smarty, $_file_to_check[0]); $mtime = $source->timestamp; } if (!$mtime || $mtime > $_file_to_check[1]) { return false; } } } // store data in reusable Smarty_Template_Compiled $this->compiled->properties = $properties; return true; }
/** * get Smarty property in template context * * @param string $property_name property name */ public function __get($property_name) { switch ($property_name) { case 'source': if (strlen($this->template_resource) == 0) { throw new SmartyException('Missing template name'); } $this->source = \Box\Brainy\Resources\Resource::source($this); // cache template object under a unique ID // do not cache eval resources if ($this->source->type != 'eval') { $_templateId = $this->smarty->joined_template_dir . '#' . $this->template_resource . $this->compile_id; if (isset($_templateId[150])) { $_templateId = sha1($_templateId); } $this->smarty->template_objects[$_templateId] = $this; } return $this->source; case 'compiled': $this->compiled = $this->source->getCompiled($this); return $this->compiled; case 'compiler': $this->compiler = new \Box\Brainy\Compiler\TemplateCompiler($this->smarty); return $this->compiler; // FIXME: routing of template -> smarty attributes // FIXME: routing of template -> smarty attributes default: if (property_exists($this->smarty, $property_name)) { return $this->smarty->{$property_name}; } } throw new SmartyException("template property '{$property_name}' does not exist."); }