本文整理汇总了PHP中Smarty_Resource类的典型用法代码示例。如果您正苦于以下问题:PHP Smarty_Resource类的具体用法?PHP Smarty_Resource怎么用?PHP Smarty_Resource使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Smarty_Resource类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setUp
public function setUp()
{
$this->smarty = SmartyTests::$smarty;
// reset cache for unit test
Smarty_Resource::$resources = array();
SmartyTests::init();
}
示例2: populate
/**
* populate Source Object with meta data from Resource
*
* @param Smarty_Template_Source $source source object
* @param Smarty_Internal_Template $_template template object
*
* @return void
*/
public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
{
$uid = '';
$sources = array();
$timestamp = 0;
foreach ($_template->smarty->getTemplateDir() as $key => $directory) {
try {
$s = Smarty_Resource::source(null, $source->smarty, 'file:' . '[' . $key . ']' . $source->name);
if (!$s->exists) {
continue;
}
$sources[$s->uid] = $s;
$uid .= $s->filepath;
$timestamp = $s->timestamp > $timestamp ? $s->timestamp : $timestamp;
} catch (SmartyException $e) {
}
}
if (!$sources) {
$source->exists = false;
return;
}
$sources = array_reverse($sources, true);
reset($sources);
$s = current($sources);
$source->components = $sources;
$source->filepath = $s->filepath;
$source->uid = sha1($uid . $_template->smarty->_joined_template_dir);
$source->exists = true;
$source->timestamp = $timestamp;
}
示例3: populate
/**
* populate Source Object with meta data from Resource
*
* @param Smarty_Template_Source $source source object
* @param Smarty_Internal_Template $_template template object
*/
public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
{
$uid = '';
$sources = array();
$components = explode('|', $source->name);
$exists = true;
foreach ($components as $component) {
$s = Smarty_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 = $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;
}
示例4: _getDefaultTemplate
/**
* get default content from template or config resource handler
*
* @param Smarty_Template_Source $source
*
* @throws \SmartyException
*/
public static function _getDefaultTemplate(Smarty_Template_Source $source)
{
if ($source->isConfig) {
$default_handler = $source->smarty->default_config_handler_func;
} else {
$default_handler = $source->smarty->default_template_handler_func;
}
$_content = $_timestamp = null;
$_return = call_user_func_array($default_handler, array($source->type, $source->name, &$_content, &$_timestamp, $source->smarty));
if (is_string($_return)) {
$source->exists = is_file($_return);
if ($source->exists) {
$source->timestamp = filemtime($_return);
} else {
throw new SmartyException("Default handler: Unable to load " . ($source->isConfig ? 'config' : 'template') . " default file '{$_return}' for '{$source->type}:{$source->name}'");
}
$source->name = $source->filepath = $_return;
$source->uid = sha1($source->filepath);
} elseif ($_return === true) {
$source->content = $_content;
$source->exists = true;
$source->uid = $source->name = sha1($_content);
$source->handler = Smarty_Resource::load($source->smarty, 'eval');
} else {
$source->exists = false;
throw new SmartyException('Default handler: No ' . ($source->isConfig ? 'config' : 'template') . " default content for '{$source->type}:{$source->name}'");
}
}
示例5: populate
/**
* populate Source Object with meta data from Resource
*
* @param Smarty_Template_Source $source source object
* @param Smarty_Internal_Template $_template template object
*
* @return void
*/
public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
{
$uid = '';
$sources = array();
$exists = true;
foreach ($_template->smarty->getTemplateDir() as $key => $directory) {
try {
$s = Smarty_Resource::source(null, $source->smarty, '[' . $key . ']' . $source->name);
if (!$s->exists) {
continue;
}
$sources[$s->uid] = $s;
$uid .= $s->filepath;
} catch (SmartyException $e) {
}
}
if (!$sources) {
$source->exists = false;
$source->template = $_template;
return;
}
$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;
}
示例6: decodeProperties
/**
* This function is executed automatically when a compiled or cached template file is included
* - Decode saved properties from compiled template and cache files
* - Check if compiled or cache file is valid
*
* @param array $properties special template properties
* @param bool $cache flag if called from cache file
*
* @return bool flag if compiled or cache file is valid
*/
public function decodeProperties(Smarty_Internal_Template $tpl, $properties, $cache = false)
{
$is_valid = true;
if (Smarty::SMARTY_VERSION != $properties['version']) {
// new version must rebuild
$is_valid = false;
} elseif ($is_valid && !empty($properties['file_dependency']) && (!$cache && $tpl->smarty->compile_check || $tpl->smarty->compile_check == 1)) {
// check file dependencies at compiled code
foreach ($properties['file_dependency'] as $_file_to_check) {
if ($_file_to_check[2] == 'file' || $_file_to_check[2] == 'extends' || $_file_to_check[2] == 'php') {
if ($tpl->source->filepath == $_file_to_check[0]) {
// do not recheck current template
continue;
//$mtime = $tpl->source->getTimeStamp();
} else {
// file and php types can be checked without loading the respective resource handlers
$mtime = is_file($_file_to_check[0]) ? filemtime($_file_to_check[0]) : false;
}
} elseif ($_file_to_check[2] == 'string') {
continue;
} else {
$handler = Smarty_Resource::load($tpl->smarty, $_file_to_check[2]);
if ($handler->checkTimestamps()) {
$source = Smarty_Template_Source::load($tpl, $tpl->smarty, $_file_to_check[0]);
$mtime = $source->getTimeStamp();
} else {
continue;
}
}
if (!$mtime || $mtime > $_file_to_check[1]) {
$is_valid = false;
break;
}
}
}
if ($cache) {
// CACHING_LIFETIME_SAVED cache expiry has to be validated here since otherwise we'd define the unifunc
if ($tpl->caching === Smarty::CACHING_LIFETIME_SAVED && $properties['cache_lifetime'] >= 0 && time() > $tpl->cached->timestamp + $properties['cache_lifetime']) {
$is_valid = false;
}
$tpl->cached->cache_lifetime = $properties['cache_lifetime'];
$tpl->cached->valid = $is_valid;
$resource = $tpl->cached;
} else {
$tpl->mustCompile = !$is_valid;
$resource = $tpl->compiled;
$resource->includes = isset($properties['includes']) ? $properties['includes'] : array();
}
if ($is_valid) {
$resource->unifunc = $properties['unifunc'];
$resource->has_nocache_code = $properties['has_nocache_code'];
// $tpl->compiled->nocache_hash = $properties['nocache_hash'];
$resource->file_dependency = $properties['file_dependency'];
if (isset($properties['tpl_function'])) {
$tpl->tpl_function = $properties['tpl_function'];
}
}
return $is_valid && !function_exists($properties['unifunc']);
}
示例7: init
public static function init()
{
error_reporting(E_ALL | E_STRICT);
self::_init(SmartyTests::$smarty);
self::_init(SmartyTests::$smartyBC);
SmartyTests::$smartyBC->registerPlugin('block', 'php', 'smarty_php_tag');
Smarty_Resource::$sources = array();
Smarty_Resource::$compileds = array();
}
示例8: setUp
public function setUp()
{
$this->smarty = SmartyTests::$smarty;
SmartyTests::init();
// empty the template dir
$this->smarty->setTemplateDir(array());
// kill cache for unit test
Smarty_Resource::$resources = array();
$this->smarty->_resource_handlers = array();
}
示例9: populate
/**
* populate Source Object with meta data from Resource
*
* @param Smarty_Template_Source $source source object
* @param Smarty_Internal_Template $_template template object
*/
public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
{
$filePath = $this->buildFilepath($source, $_template);
$s = Smarty_Resource::source(null, $source->smarty, $filePath);
$source->components = $s;
$source->filepath = $s->filepath;
$source->uid = $s->uid;
if ($_template && $_template->smarty->compile_check) {
$source->timestamp = $s->timestamp;
$source->exists = $s->exists;
}
$source->template = $_template;
}
示例10: makeFiles
/**
* 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, null, $compile_id);
$tpl->fetch();
$this->_files[$template . '#' . $compile_id] = substr($tpl->compiled->filepath, $directory_length - 1);
}
}
Smarty_Resource::$sources = array();
$this->smarty->template_objects = array();
return $this->_files;
}
示例11: load
/**
* initialize Source Object for given resource
* Either [$_template] or [$smarty, $template_resource] must be specified
*
* @param Smarty_Internal_Template $_template template object
* @param Smarty $smarty smarty object
* @param string $template_resource resource identifier
*
* @return Smarty_Template_Config Source Object
* @throws SmartyException
*/
public static function load(Smarty_Internal_Template $_template = null, Smarty $smarty = null, $template_resource = null)
{
static $_incompatible_resources = array('extends' => true, 'php' => true);
$template_resource = $_template->template_resource;
if (empty($template_resource)) {
throw new SmartyException('Missing config name');
}
// parse resource_name, load resource handler
list($name, $type) = Smarty_Resource::parseResourceName($template_resource, $_template->smarty->default_config_type);
// make sure configs are not loaded via anything smarty can't handle
if (isset($_incompatible_resources[$type])) {
throw new SmartyException("Unable to use resource '{$type}' for config");
}
$source = new Smarty_Template_Config($_template->smarty, $template_resource, $type, $name);
$source->handler->populate($source, $_template);
if (!$source->exists && isset($_template->smarty->default_config_handler_func)) {
Smarty_Internal_Method_RegisterDefaultTemplateHandler::_getDefaultTemplate($source);
}
return $source;
}
示例12: setSource
/**
* Set source object of inline template by $uid
*
* @param \Smarty_Internal_Template $tpl
* @param string $uid
*
* @throws \SmartyException
*/
public function setSource(Smarty_Internal_Template $tpl, $uid = null)
{
// $uid is set if template is inline
if (isset($uid)) {
// inline templates have same compiled resource
$tpl->compiled = $tpl->parent->compiled;
if (isset($tpl->compiled->file_dependency[$uid])) {
list($filepath, $timestamp, $resource) = $tpl->compiled->file_dependency[$uid];
$tpl->source = new Smarty_Template_Source(isset($tpl->smarty->_cache['resource_handlers'][$resource]) ? $tpl->smarty->_cache['resource_handlers'][$resource] : Smarty_Resource::load($tpl->smarty, $resource), $tpl->smarty, $filepath, $resource, $filepath);
$tpl->source->filepath = $filepath;
$tpl->source->timestamp = $timestamp;
$tpl->source->exists = true;
$tpl->source->uid = $uid;
} else {
$tpl->source = null;
}
} else {
$tpl->source = null;
unset($tpl->compiled);
}
if (!isset($tpl->source)) {
$tpl->source = Smarty_Template_Source::load($tpl);
}
}
示例13: clearCompiledTemplate
/**
* Delete compiled template file
*
* @param string $resource_name template name
* @param string $compile_id compile id
* @param integer $exp_time expiration time
* @param Smarty $smarty Smarty instance
*
* @return integer number of template files deleted
*/
public static function clearCompiledTemplate($resource_name, $compile_id, $exp_time, Smarty $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)) {
$_save_stat = $smarty->caching;
$smarty->caching = false;
$tpl = new $smarty->template_class($resource_name, $smarty);
$smarty->caching = $_save_stat;
// remove from template cache
$tpl->source;
// have the template registered before unset()
if ($smarty->allow_ambiguous_resources) {
$_templateId = $tpl->source->unique_resource . $tpl->cache_id . $tpl->compile_id;
} else {
$_templateId = $smarty->joined_template_dir . '#' . $resource_name . $tpl->cache_id . $tpl->compile_id;
}
if (isset($_templateId[150])) {
$_templateId = sha1($_templateId);
}
unset($smarty->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 ($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);
// 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
Smarty_Resource::$sources = array();
Smarty_Resource::$compileds = array();
return $_count;
}
示例14: __get
/**
* get Smarty property in template context
*
* @param string $property_name property name
*
* @return \Smarty_Config_Source|\Smarty_Template_Compiled
* @throws SmartyException if $property_name is not valid
*/
public function __get($property_name)
{
switch ($property_name) {
case 'source':
if (empty($this->config_resource)) {
throw new SmartyException("Unable to parse resource name \"{$this->config_resource}\"");
}
$this->source = Smarty_Resource::config($this);
return $this->source;
case 'compiled':
$this->compiled = $this->source->getCompiled($this);
return $this->compiled;
}
throw new SmartyException("config attribute '{$property_name}' does not exist.");
}
示例15: decodeProperties
/**
* This function is executed automatically when a compiled or cached template file is included
* - Decode saved properties from compiled template and cache files
* - Check if compiled or cache file is valid
*
* @param array $properties special template properties
* @param bool $cache flag if called from cache file
*
* @return bool flag if compiled or cache file is valid
*/
public function decodeProperties($properties, $cache = false)
{
$properties['version'] = isset($properties['version']) ? $properties['version'] : '';
$is_valid = true;
if (Smarty::SMARTY_VERSION != $properties['version']) {
// new version must rebuild
$is_valid = false;
} elseif ((!$cache && $this->smarty->compile_check || $cache && ($this->smarty->compile_check === true || $this->smarty->compile_check === Smarty::COMPILECHECK_ON)) && !empty($properties['file_dependency'])) {
// check file dependencies at compiled code
foreach ($properties['file_dependency'] as $_file_to_check) {
if ($_file_to_check[2] == 'file' || $_file_to_check[2] == 'php') {
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 = is_file($_file_to_check[0]) ? @filemtime($_file_to_check[0]) : false;
}
} elseif ($_file_to_check[2] == 'string') {
continue;
} else {
$source = Smarty_Resource::source(null, $this->smarty, $_file_to_check[0]);
$mtime = $source->timestamp;
}
if (!$mtime || $mtime > $_file_to_check[1]) {
$is_valid = false;
break;
}
}
}
if ($cache) {
// CACHING_LIFETIME_SAVED cache expiry has to be validated here since otherwise we'd define the unifunc
if ($this->caching === Smarty::CACHING_LIFETIME_SAVED && $properties['cache_lifetime'] >= 0 && time() > $this->cached->timestamp + $properties['cache_lifetime']) {
$is_valid = false;
}
$this->cached->valid = $is_valid;
} else {
$this->mustCompile = !$is_valid;
}
if ($is_valid) {
$this->has_nocache_code = $properties['has_nocache_code'];
// $this->properties['nocache_hash'] = $properties['nocache_hash'];
if (isset($properties['cache_lifetime'])) {
$this->properties['cache_lifetime'] = $properties['cache_lifetime'];
}
if (isset($properties['file_dependency'])) {
$this->properties['file_dependency'] = array_merge($this->properties['file_dependency'], $properties['file_dependency']);
}
if (isset($properties['tpl_function'])) {
$this->properties['tpl_function'] = array_merge($this->properties['tpl_function'], $properties['tpl_function']);
}
$this->properties['version'] = $properties['version'];
$this->properties['unifunc'] = $properties['unifunc'];
}
return $is_valid;
}