Fix bugs, improve perfs and use SimpleLogger instead of Monolog
This commit is contained in:
58
app/Core/Cache.php
Normal file
58
app/Core/Cache.php
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
41
app/Core/FileCache.php
Normal file
41
app/Core/FileCache.php
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -48,6 +48,22 @@ class Helper
|
||||
return $this->container[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
|
||||
32
app/Core/MemoryCache.php
Normal file
32
app/Core/MemoryCache.php
Normal file
@@ -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]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user