diff options
Diffstat (limited to 'app/Core')
-rw-r--r-- | app/Core/Cache.php | 58 | ||||
-rw-r--r-- | app/Core/FileCache.php | 41 | ||||
-rw-r--r-- | app/Core/Helper.php | 16 | ||||
-rw-r--r-- | app/Core/MemoryCache.php | 32 |
4 files changed, 147 insertions, 0 deletions
diff --git a/app/Core/Cache.php b/app/Core/Cache.php new file mode 100644 index 00000000..670a76e0 --- /dev/null +++ b/app/Core/Cache.php @@ -0,0 +1,58 @@ +<?php + +namespace Core; + +use Pimple\Container; + +abstract class Cache +{ + /** + * Container instance + * + * @access protected + * @var \Pimple\Container + */ + protected $container; + + abstract public function init(); + abstract public function set($key, $value); + abstract public function get($key); + abstract public function flush(); + abstract public function remove($key); + + /** + * Constructor + * + * @access public + * @param \Pimple\Container $container + */ + public function __construct(Container $container) + { + $this->container = $container; + $this->init(); + } + + /** + * Proxy cache + * + * Note: Arguments must be scalar types + * + * @access public + * @param string $container Container name + * @param string $method Container method + * @return mixed + */ + public function proxy($container, $method) + { + $args = func_get_args(); + $key = 'proxy_'.implode('_', $args); + $result = $this->get($key); + + if ($result === null) { + $result = call_user_func_array(array($this->container[$container], $method), array_splice($args, 2)); + $this->set($key, $result); + } + + return $result; + } +} diff --git a/app/Core/FileCache.php b/app/Core/FileCache.php new file mode 100644 index 00000000..2037f271 --- /dev/null +++ b/app/Core/FileCache.php @@ -0,0 +1,41 @@ +<?php + +namespace Core; + +class FileCache extends Cache +{ + const CACHE_FOLDER = 'data/cache/'; + + public function init() + { + if (! is_dir(self::CACHE_FOLDER)) { + mkdir(self::CACHE_FOLDER); + } + } + + public function set($key, $value) + { + file_put_contents(self::CACHE_FOLDER.$key, json_encode($value)); + } + + public function get($key) + { + if (file_exists(self::CACHE_FOLDER.$key)) { + return json_decode(file_get_contents(self::CACHE_FOLDER.$key), true); + } + + return null; + } + + public function flush() + { + foreach (glob(self::CACHE_FOLDER.'*') as $filename) { + @unlink($filename); + } + } + + public function remove($key) + { + @unlink(self::CACHE_FOLDER.$key); + } +} diff --git a/app/Core/Helper.php b/app/Core/Helper.php index 5eaa8dc9..1db8afc6 100644 --- a/app/Core/Helper.php +++ b/app/Core/Helper.php @@ -49,6 +49,22 @@ class Helper } /** + * Proxy cache helper for acl::isManagerActionAllowed() + * + * @access public + * @param integer $project_id + * @return boolean + */ + public function isManager($project_id) + { + if ($this->userSession->isAdmin()) { + return true; + } + + return $this->container['memoryCache']->proxy('acl', 'isManagerActionAllowed', $project_id); + } + + /** * Return the user full name * * @param array $user User properties diff --git a/app/Core/MemoryCache.php b/app/Core/MemoryCache.php new file mode 100644 index 00000000..f80a66ef --- /dev/null +++ b/app/Core/MemoryCache.php @@ -0,0 +1,32 @@ +<?php + +namespace Core; + +class MemoryCache extends Cache +{ + private $storage = array(); + + public function init() + { + } + + public function set($key, $value) + { + $this->storage[$key] = $value; + } + + public function get($key) + { + return isset($this->storage[$key]) ? $this->storage[$key] : null; + } + + public function flush() + { + $this->storage = array(); + } + + public function remove($key) + { + unset($this->storage[$key]); + } +} |