diff options
author | anaqreon <tamanning@zoho.com> | 2014-12-30 16:06:35 -0600 |
---|---|---|
committer | anaqreon <tamanning@zoho.com> | 2014-12-30 16:06:35 -0600 |
commit | b78a545a1056e6db9f4b6b4f262182b0a5c56e67 (patch) | |
tree | 515c2f742a5a39e18b00debf1849ee9703fa4a42 /library/Smarty/demo/plugins | |
parent | d67c5a6ffd134602084a6dcb37b316bf768bf715 (diff) | |
parent | 43671a0a323afa758df56c06822ce3c46da026df (diff) | |
download | volse-hubzilla-b78a545a1056e6db9f4b6b4f262182b0a5c56e67.tar.gz volse-hubzilla-b78a545a1056e6db9f4b6b4f262182b0a5c56e67.tar.bz2 volse-hubzilla-b78a545a1056e6db9f4b6b4f262182b0a5c56e67.zip |
Merge pull request #1 from friendica/master
Pull from upstream
Diffstat (limited to 'library/Smarty/demo/plugins')
-rw-r--r-- | library/Smarty/demo/plugins/cacheresource.apc.php | 28 | ||||
-rw-r--r-- | library/Smarty/demo/plugins/cacheresource.memcache.php | 36 | ||||
-rw-r--r-- | library/Smarty/demo/plugins/cacheresource.mysql.php | 96 | ||||
-rw-r--r-- | library/Smarty/demo/plugins/resource.extendsall.php | 32 | ||||
-rw-r--r-- | library/Smarty/demo/plugins/resource.mysql.php | 35 | ||||
-rw-r--r-- | library/Smarty/demo/plugins/resource.mysqls.php | 28 |
6 files changed, 141 insertions, 114 deletions
diff --git a/library/Smarty/demo/plugins/cacheresource.apc.php b/library/Smarty/demo/plugins/cacheresource.apc.php index 00ba59817..d7336f2bf 100644 --- a/library/Smarty/demo/plugins/cacheresource.apc.php +++ b/library/Smarty/demo/plugins/cacheresource.apc.php @@ -2,19 +2,19 @@ /** * APC CacheResource - * * CacheResource Implementation based on the KeyValueStore API to use * memcache as the storage resource for Smarty's output caching. * * + * * @package CacheResource-examples - * @author Uwe Tews + * @author Uwe Tews */ -class Smarty_CacheResource_Apc extends Smarty_CacheResource_KeyValueStore { - +class Smarty_CacheResource_Apc extends Smarty_CacheResource_KeyValueStore +{ public function __construct() { // test if APC is present - if(!function_exists('apc_cache_info')) { + if (!function_exists('apc_cache_info')) { throw new Exception('APC Template Caching Error: APC is not installed'); } } @@ -22,8 +22,9 @@ class Smarty_CacheResource_Apc extends Smarty_CacheResource_KeyValueStore { /** * Read values for a set of keys from cache * - * @param array $keys list of keys to fetch - * @return array list of values with the given keys used as indexes + * @param array $keys list of keys to fetch + * + * @return array list of values with the given keys used as indexes * @return boolean true on success, false on failure */ protected function read(array $keys) @@ -33,28 +34,32 @@ class Smarty_CacheResource_Apc extends Smarty_CacheResource_KeyValueStore { foreach ($res as $k => $v) { $_res[$k] = $v; } + return $_res; } /** * Save values for a set of keys to cache * - * @param array $keys list of values to save - * @param int $expire expiration time + * @param array $keys list of values to save + * @param int $expire expiration time + * * @return boolean true on success, false on failure */ - protected function write(array $keys, $expire=null) + protected function write(array $keys, $expire = null) { foreach ($keys as $k => $v) { apc_store($k, $v, $expire); } + return true; } /** * Remove values from cache * - * @param array $keys list of keys to delete + * @param array $keys list of keys to delete + * * @return boolean true on success, false on failure */ protected function delete(array $keys) @@ -62,6 +67,7 @@ class Smarty_CacheResource_Apc extends Smarty_CacheResource_KeyValueStore { foreach ($keys as $k) { apc_delete($k); } + return true; } diff --git a/library/Smarty/demo/plugins/cacheresource.memcache.php b/library/Smarty/demo/plugins/cacheresource.memcache.php index 230607d69..e265365fb 100644 --- a/library/Smarty/demo/plugins/cacheresource.memcache.php +++ b/library/Smarty/demo/plugins/cacheresource.memcache.php @@ -2,34 +2,35 @@ /** * Memcache CacheResource - * * CacheResource Implementation based on the KeyValueStore API to use * memcache as the storage resource for Smarty's output caching. - * * Note that memcache has a limitation of 256 characters per cache-key. * To avoid complications all cache-keys are translated to a sha1 hash. * * @package CacheResource-examples - * @author Rodney Rehm + * @author Rodney Rehm */ -class Smarty_CacheResource_Memcache extends Smarty_CacheResource_KeyValueStore { +class Smarty_CacheResource_Memcache extends Smarty_CacheResource_KeyValueStore +{ /** * memcache instance + * * @var Memcache */ protected $memcache = null; - + public function __construct() { $this->memcache = new Memcache(); - $this->memcache->addServer( '127.0.0.1', 11211 ); + $this->memcache->addServer('127.0.0.1', 11211); } - + /** * Read values for a set of keys from cache * - * @param array $keys list of keys to fetch - * @return array list of values with the given keys used as indexes + * @param array $keys list of keys to fetch + * + * @return array list of values with the given keys used as indexes * @return boolean true on success, false on failure */ protected function read(array $keys) @@ -45,29 +46,33 @@ class Smarty_CacheResource_Memcache extends Smarty_CacheResource_KeyValueStore { foreach ($res as $k => $v) { $_res[$lookup[$k]] = $v; } + return $_res; } - + /** * Save values for a set of keys to cache * - * @param array $keys list of values to save - * @param int $expire expiration time + * @param array $keys list of values to save + * @param int $expire expiration time + * * @return boolean true on success, false on failure */ - protected function write(array $keys, $expire=null) + protected function write(array $keys, $expire = null) { foreach ($keys as $k => $v) { $k = sha1($k); $this->memcache->set($k, $v, 0, $expire); } + return true; } /** * Remove values from cache * - * @param array $keys list of keys to delete + * @param array $keys list of keys to delete + * * @return boolean true on success, false on failure */ protected function delete(array $keys) @@ -76,6 +81,7 @@ class Smarty_CacheResource_Memcache extends Smarty_CacheResource_KeyValueStore { $k = sha1($k); $this->memcache->delete($k); } + return true; } @@ -86,6 +92,6 @@ class Smarty_CacheResource_Memcache extends Smarty_CacheResource_KeyValueStore { */ protected function purge() { - return $this->memcache->flush(); + $this->memcache->flush(); } } diff --git a/library/Smarty/demo/plugins/cacheresource.mysql.php b/library/Smarty/demo/plugins/cacheresource.mysql.php index ab8c47516..d8d00ab26 100644 --- a/library/Smarty/demo/plugins/cacheresource.mysql.php +++ b/library/Smarty/demo/plugins/cacheresource.mysql.php @@ -2,10 +2,8 @@ /** * MySQL CacheResource - * * CacheResource Implementation based on the Custom API to use * MySQL as the storage resource for Smarty's output caching. - * * Table definition: * <pre>CREATE TABLE IF NOT EXISTS `output_cache` ( * `id` CHAR(40) NOT NULL COMMENT 'sha1 hash', @@ -22,19 +20,22 @@ * ) ENGINE = InnoDB;</pre> * * @package CacheResource-examples - * @author Rodney Rehm + * @author Rodney Rehm */ -class Smarty_CacheResource_Mysql extends Smarty_CacheResource_Custom { +class Smarty_CacheResource_Mysql extends Smarty_CacheResource_Custom +{ // PDO instance protected $db; protected $fetch; protected $fetchTimestamp; protected $save; - - public function __construct() { + + public function __construct() + { try { - $this->db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty", "smarty"); - } catch (PDOException $e) { + $this->db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty"); + } + catch (PDOException $e) { throw new SmartyException('Mysql Resource failed: ' . $e->getMessage()); } $this->fetch = $this->db->prepare('SELECT modified, content FROM output_cache WHERE id = :id'); @@ -46,19 +47,20 @@ class Smarty_CacheResource_Mysql extends Smarty_CacheResource_Custom { /** * fetch cached content and its modification time from data source * - * @param string $id unique cache content identifier - * @param string $name template name - * @param string $cache_id cache id - * @param string $compile_id compile id - * @param string $content cached content - * @param integer $mtime cache modification timestamp (epoch) + * @param string $id unique cache content identifier + * @param string $name template name + * @param string $cache_id cache id + * @param string $compile_id compile id + * @param string $content cached content + * @param integer $mtime cache modification timestamp (epoch) + * * @return void */ protected function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime) { $this->fetch->execute(array('id' => $id)); $row = $this->fetch->fetch(); - $this->fetch->closeCursor(); + $this->fetch->closeCursor(); if ($row) { $content = $row['content']; $mtime = strtotime($row['modified']); @@ -67,15 +69,17 @@ class Smarty_CacheResource_Mysql extends Smarty_CacheResource_Custom { $mtime = null; } } - + /** * Fetch cached content's modification timestamp from data source * * @note implementing this method is optional. Only implement it if modification times can be accessed faster than loading the complete cached content. - * @param string $id unique cache content identifier - * @param string $name template name - * @param string $cache_id cache id - * @param string $compile_id compile id + * + * @param string $id unique cache content identifier + * @param string $name template name + * @param string $cache_id cache id + * @param string $compile_id compile id + * * @return integer|boolean timestamp (epoch) the template was modified, or false if not found */ protected function fetchTimestamp($id, $name, $cache_id, $compile_id) @@ -83,40 +87,44 @@ class Smarty_CacheResource_Mysql extends Smarty_CacheResource_Custom { $this->fetchTimestamp->execute(array('id' => $id)); $mtime = strtotime($this->fetchTimestamp->fetchColumn()); $this->fetchTimestamp->closeCursor(); + return $mtime; } - + /** * Save content to cache * - * @param string $id unique cache content identifier - * @param string $name template name - * @param string $cache_id cache id - * @param string $compile_id compile id - * @param integer|null $exp_time seconds till expiration time in seconds or null - * @param string $content content to cache - * @return boolean success + * @param string $id unique cache content identifier + * @param string $name template name + * @param string $cache_id cache id + * @param string $compile_id compile id + * @param integer|null $exp_time seconds till expiration time in seconds or null + * @param string $content content to cache + * + * @return boolean success */ protected function save($id, $name, $cache_id, $compile_id, $exp_time, $content) { $this->save->execute(array( - 'id' => $id, - 'name' => $name, - 'cache_id' => $cache_id, - 'compile_id' => $compile_id, - 'content' => $content, - )); + 'id' => $id, + 'name' => $name, + 'cache_id' => $cache_id, + 'compile_id' => $compile_id, + 'content' => $content, + )); + return !!$this->save->rowCount(); } - + /** * Delete content from cache * - * @param string $name template name - * @param string $cache_id cache id - * @param string $compile_id compile id - * @param integer|null $exp_time seconds till expiration or null - * @return integer number of deleted caches + * @param string $name template name + * @param string $cache_id cache id + * @param string $compile_id compile id + * @param integer|null $exp_time seconds till expiration or null + * + * @return integer number of deleted caches */ protected function delete($name, $cache_id, $compile_id, $exp_time) { @@ -124,7 +132,8 @@ class Smarty_CacheResource_Mysql extends Smarty_CacheResource_Custom { if ($name === null && $cache_id === null && $compile_id === null && $exp_time === null) { // returning the number of deleted caches would require a second query to count them $query = $this->db->query('TRUNCATE TABLE output_cache'); - return -1; + + return - 1; } // build the filter $where = array(); @@ -142,11 +151,12 @@ class Smarty_CacheResource_Mysql extends Smarty_CacheResource_Custom { } // equal test cache_id and match sub-groups if ($cache_id !== null) { - $where[] = '(cache_id = '. $this->db->quote($cache_id) - . ' OR cache_id LIKE '. $this->db->quote($cache_id .'|%') .')'; + $where[] = '(cache_id = ' . $this->db->quote($cache_id) + . ' OR cache_id LIKE ' . $this->db->quote($cache_id . '|%') . ')'; } // run delete query $query = $this->db->query('DELETE FROM output_cache WHERE ' . join(' AND ', $where)); + return $query->rowCount(); } } diff --git a/library/Smarty/demo/plugins/resource.extendsall.php b/library/Smarty/demo/plugins/resource.extendsall.php index d8c40b5ba..500b3c862 100644 --- a/library/Smarty/demo/plugins/resource.extendsall.php +++ b/library/Smarty/demo/plugins/resource.extendsall.php @@ -2,49 +2,51 @@ /** * Extends All Resource - * * Resource Implementation modifying the extends-Resource to walk * through the template_dirs and inherit all templates of the same name - * + * * @package Resource-examples - * @author Rodney Rehm + * @author Rodney Rehm */ -class Smarty_Resource_Extendsall extends Smarty_Internal_Resource_Extends { - +class Smarty_Resource_Extendsall extends Smarty_Internal_Resource_Extends +{ /** * populate Source Object with meta data from Resource * - * @param Smarty_Template_Source $source source object - * @param Smarty_Internal_Template $_template template object + * @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) + 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 ); + $s = Smarty_Resource::source(null, $source->smarty, '[' . $key . ']' . $source->name); if (!$s->exists) { continue; } $sources[$s->uid] = $s; $uid .= $s->filepath; } - catch (SmartyException $e) {} + 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); @@ -55,6 +57,4 @@ class Smarty_Resource_Extendsall extends Smarty_Internal_Resource_Extends { // need the template at getContent() $source->template = $_template; } -} - -?>
\ No newline at end of file +} diff --git a/library/Smarty/demo/plugins/resource.mysql.php b/library/Smarty/demo/plugins/resource.mysql.php index 312f3fc73..dfc9606b4 100644 --- a/library/Smarty/demo/plugins/resource.mysql.php +++ b/library/Smarty/demo/plugins/resource.mysql.php @@ -2,10 +2,8 @@ /** * MySQL Resource - * * Resource Implementation based on the Custom API to use * MySQL as the storage resource for Smarty's templates and configs. - * * Table definition: * <pre>CREATE TABLE IF NOT EXISTS `templates` ( * `name` varchar(100) NOT NULL, @@ -13,14 +11,14 @@ * `source` text, * PRIMARY KEY (`name`) * ) ENGINE=InnoDB DEFAULT CHARSET=utf8;</pre> - * * Demo data: * <pre>INSERT INTO `templates` (`name`, `modified`, `source`) VALUES ('test.tpl', "2010-12-25 22:00:00", '{$x="hello world"}{$x}');</pre> * * @package Resource-examples - * @author Rodney Rehm + * @author Rodney Rehm */ -class Smarty_Resource_Mysql extends Smarty_Resource_Custom { +class Smarty_Resource_Mysql extends Smarty_Resource_Custom +{ // PDO instance protected $db; // prepared fetch() statement @@ -28,22 +26,25 @@ class Smarty_Resource_Mysql extends Smarty_Resource_Custom { // prepared fetchTimestamp() statement protected $mtime; - public function __construct() { + public function __construct() + { try { - $this->db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty", "smarty"); - } catch (PDOException $e) { + $this->db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty"); + } + catch (PDOException $e) { throw new SmartyException('Mysql Resource failed: ' . $e->getMessage()); } $this->fetch = $this->db->prepare('SELECT modified, source FROM templates WHERE name = :name'); $this->mtime = $this->db->prepare('SELECT modified FROM templates WHERE name = :name'); } - + /** * Fetch a template and its modification time from database * - * @param string $name template name - * @param string $source template source - * @param integer $mtime template modification timestamp (epoch) + * @param string $name template name + * @param string $source template source + * @param integer $mtime template modification timestamp (epoch) + * * @return void */ protected function fetch($name, &$source, &$mtime) @@ -59,18 +60,22 @@ class Smarty_Resource_Mysql extends Smarty_Resource_Custom { $mtime = null; } } - + /** * Fetch a template's modification time from database * * @note implementing this method is optional. Only implement it if modification times can be accessed faster than loading the comple template source. - * @param string $name template name + * + * @param string $name template name + * * @return integer timestamp (epoch) the template was modified */ - protected function fetchTimestamp($name) { + protected function fetchTimestamp($name) + { $this->mtime->execute(array('name' => $name)); $mtime = $this->mtime->fetchColumn(); $this->mtime->closeCursor(); + return strtotime($mtime); } } diff --git a/library/Smarty/demo/plugins/resource.mysqls.php b/library/Smarty/demo/plugins/resource.mysqls.php index f9fe1c2f2..f694ddf11 100644 --- a/library/Smarty/demo/plugins/resource.mysqls.php +++ b/library/Smarty/demo/plugins/resource.mysqls.php @@ -2,13 +2,10 @@ /** * MySQL Resource - * * Resource Implementation based on the Custom API to use * MySQL as the storage resource for Smarty's templates and configs. - * * Note that this MySQL implementation fetches the source and timestamps in - * a single database query, instead of two seperate like resource.mysql.php does. - * + * a single database query, instead of two separate like resource.mysql.php does. * Table definition: * <pre>CREATE TABLE IF NOT EXISTS `templates` ( * `name` varchar(100) NOT NULL, @@ -16,34 +13,37 @@ * `source` text, * PRIMARY KEY (`name`) * ) ENGINE=InnoDB DEFAULT CHARSET=utf8;</pre> - * * Demo data: * <pre>INSERT INTO `templates` (`name`, `modified`, `source`) VALUES ('test.tpl', "2010-12-25 22:00:00", '{$x="hello world"}{$x}');</pre> * * @package Resource-examples - * @author Rodney Rehm + * @author Rodney Rehm */ -class Smarty_Resource_Mysqls extends Smarty_Resource_Custom { +class Smarty_Resource_Mysqls extends Smarty_Resource_Custom +{ // PDO instance protected $db; // prepared fetch() statement protected $fetch; - public function __construct() { + public function __construct() + { try { - $this->db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty", "smarty"); - } catch (PDOException $e) { + $this->db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty"); + } + catch (PDOException $e) { throw new SmartyException('Mysql Resource failed: ' . $e->getMessage()); } $this->fetch = $this->db->prepare('SELECT modified, source FROM templates WHERE name = :name'); } - + /** * Fetch a template and its modification time from database * - * @param string $name template name - * @param string $source template source - * @param integer $mtime template modification timestamp (epoch) + * @param string $name template name + * @param string $source template source + * @param integer $mtime template modification timestamp (epoch) + * * @return void */ protected function fetch($name, &$source, &$mtime) |