Add abstract cache layer
This commit is contained in:
@@ -24,6 +24,8 @@ Improvements:
|
|||||||
* Remove unnecessary margin for calendar header
|
* Remove unnecessary margin for calendar header
|
||||||
* Show localized documentation if available
|
* Show localized documentation if available
|
||||||
* Add event subtask.delete
|
* Add event subtask.delete
|
||||||
|
* Add abstract storage layer
|
||||||
|
* Add abstract cache layer
|
||||||
|
|
||||||
Bug fixes:
|
Bug fixes:
|
||||||
|
|
||||||
|
|||||||
@@ -1,58 +0,0 @@
|
|||||||
<?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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
38
app/Core/Cache/Base.php
Normal file
38
app/Core/Cache/Base.php
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Core\Cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base class for cache drivers
|
||||||
|
*
|
||||||
|
* @package cache
|
||||||
|
* @author Frederic Guillot
|
||||||
|
*/
|
||||||
|
abstract class Base
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Proxy cache
|
||||||
|
*
|
||||||
|
* Note: Arguments must be scalar types
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param string $class Class instance
|
||||||
|
* @param string $method Container method
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function proxy($class, $method)
|
||||||
|
{
|
||||||
|
$args = func_get_args();
|
||||||
|
array_shift($args);
|
||||||
|
|
||||||
|
$key = 'proxy:'.get_class($class).':'.implode(':', $args);
|
||||||
|
$result = $this->get($key);
|
||||||
|
|
||||||
|
if ($result === null) {
|
||||||
|
$result = call_user_func_array(array($class, $method), array_splice($args, 1));
|
||||||
|
$this->set($key, $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
||||||
45
app/Core/Cache/CacheInterface.php
Normal file
45
app/Core/Cache/CacheInterface.php
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Core\Cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cache Interface
|
||||||
|
*
|
||||||
|
* @package cache
|
||||||
|
* @author Frederic Guillot
|
||||||
|
*/
|
||||||
|
interface CacheInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Save a new value in the cache
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param string $key
|
||||||
|
* @param string $value
|
||||||
|
*/
|
||||||
|
public function set($key, $value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch value from cache
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param string $key
|
||||||
|
* @return mixed Null when not found, cached value otherwise
|
||||||
|
*/
|
||||||
|
public function get($key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear all cache
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
public function flush();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove cached value
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param string $key
|
||||||
|
*/
|
||||||
|
public function remove($key);
|
||||||
|
}
|
||||||
65
app/Core/Cache/MemoryCache.php
Normal file
65
app/Core/Cache/MemoryCache.php
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Core\Cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Memory Cache
|
||||||
|
*
|
||||||
|
* @package cache
|
||||||
|
* @author Frederic Guillot
|
||||||
|
*/
|
||||||
|
class MemoryCache extends Base implements CacheInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Container
|
||||||
|
*
|
||||||
|
* @access private
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private $storage = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save a new value in the cache
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param string $key
|
||||||
|
* @param string $value
|
||||||
|
*/
|
||||||
|
public function set($key, $value)
|
||||||
|
{
|
||||||
|
$this->storage[$key] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch value from cache
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param string $key
|
||||||
|
* @return mixed Null when not found, cached value otherwise
|
||||||
|
*/
|
||||||
|
public function get($key)
|
||||||
|
{
|
||||||
|
return isset($this->storage[$key]) ? $this->storage[$key] : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear all cache
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
public function flush()
|
||||||
|
{
|
||||||
|
$this->storage = array();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove cached value
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param string $key
|
||||||
|
*/
|
||||||
|
public function remove($key)
|
||||||
|
{
|
||||||
|
unset($this->storage[$key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
<?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]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -99,7 +99,7 @@ class User extends \Core\Base
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->memoryCache->proxy('acl', 'handleProjectAdminPermissions', $project_id);
|
return $this->memoryCache->proxy($this->container['acl'], 'handleProjectAdminPermissions', $project_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -114,7 +114,7 @@ class User extends \Core\Base
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->memoryCache->proxy('acl', 'handleProjectManagerPermissions', $project_id);
|
return $this->memoryCache->proxy($this->container['acl'], 'handleProjectManagerPermissions', $project_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -72,12 +72,14 @@ class ClassProvider implements ServiceProviderInterface
|
|||||||
'Helper',
|
'Helper',
|
||||||
'HttpClient',
|
'HttpClient',
|
||||||
'Lexer',
|
'Lexer',
|
||||||
'MemoryCache',
|
|
||||||
'Request',
|
'Request',
|
||||||
'Router',
|
'Router',
|
||||||
'Session',
|
'Session',
|
||||||
'Template',
|
'Template',
|
||||||
),
|
),
|
||||||
|
'Core\Cache' => array(
|
||||||
|
'MemoryCache',
|
||||||
|
),
|
||||||
'Integration' => array(
|
'Integration' => array(
|
||||||
'BitbucketWebhook',
|
'BitbucketWebhook',
|
||||||
'GithubWebhook',
|
'GithubWebhook',
|
||||||
|
|||||||
62
tests/units/Core/Cache/MemoryCacheTest.php
Normal file
62
tests/units/Core/Cache/MemoryCacheTest.php
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once __DIR__.'/../../Base.php';
|
||||||
|
|
||||||
|
use Core\Cache\MemoryCache;
|
||||||
|
|
||||||
|
class MemoryCacheTest extends Base
|
||||||
|
{
|
||||||
|
public function testKeyNotFound()
|
||||||
|
{
|
||||||
|
$c = new MemoryCache;
|
||||||
|
$this->assertEquals(null, $c->get('mykey'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSetValue()
|
||||||
|
{
|
||||||
|
$c = new MemoryCache;
|
||||||
|
$c->set('mykey', 'myvalue');
|
||||||
|
$this->assertEquals('myvalue', $c->get('mykey'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRemoveValue()
|
||||||
|
{
|
||||||
|
$c = new MemoryCache;
|
||||||
|
$c->set('mykey', 'myvalue');
|
||||||
|
$c->remove('mykey');
|
||||||
|
$this->assertEquals(null, $c->get('mykey'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFlushAll()
|
||||||
|
{
|
||||||
|
$c = new MemoryCache;
|
||||||
|
$c->set('mykey', 'myvalue');
|
||||||
|
$c->flush();
|
||||||
|
$this->assertEquals(null, $c->get('mykey'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testProxy()
|
||||||
|
{
|
||||||
|
$c = new MemoryCache;
|
||||||
|
|
||||||
|
$class = $this
|
||||||
|
->getMockBuilder('stdClass')
|
||||||
|
->setMethods(array('doSomething'))
|
||||||
|
->getMock();
|
||||||
|
|
||||||
|
$class
|
||||||
|
->expects($this->once())
|
||||||
|
->method('doSomething')
|
||||||
|
->with(
|
||||||
|
$this->equalTo(1),
|
||||||
|
$this->equalTo(2)
|
||||||
|
)
|
||||||
|
->will($this->returnValue(3));
|
||||||
|
|
||||||
|
// First call will store the computed value
|
||||||
|
$this->assertEquals(3, $c->proxy($class, 'doSomething', 1, 2));
|
||||||
|
|
||||||
|
// Second call get directly the cached value
|
||||||
|
$this->assertEquals(3, $c->proxy($class, 'doSomething', 1, 2));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,10 @@
|
|||||||
require_once __DIR__.'/../Base.php';
|
require_once __DIR__.'/../Base.php';
|
||||||
|
|
||||||
use Helper\User;
|
use Helper\User;
|
||||||
|
use Model\Project;
|
||||||
|
use Model\ProjectPermission;
|
||||||
|
use Model\User as UserModel;
|
||||||
|
use Core\Session;
|
||||||
|
|
||||||
class UserHelperTest extends Base
|
class UserHelperTest extends Base
|
||||||
{
|
{
|
||||||
@@ -13,4 +17,166 @@ class UserHelperTest extends Base
|
|||||||
$this->assertEquals('CN', $h->getInitials('chuck norris'));
|
$this->assertEquals('CN', $h->getInitials('chuck norris'));
|
||||||
$this->assertEquals('A', $h->getInitials('admin'));
|
$this->assertEquals('A', $h->getInitials('admin'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testIsProjectAdministrationAllowedForProjectAdmin()
|
||||||
|
{
|
||||||
|
$h = new User($this->container);
|
||||||
|
$p = new Project($this->container);
|
||||||
|
$pp = new ProjectPermission($this->container);
|
||||||
|
$u = new UserModel($this->container);
|
||||||
|
$session = new Session;
|
||||||
|
|
||||||
|
// We create our user
|
||||||
|
$this->assertEquals(2, $u->create(array('username' => 'unittest', 'password' => 'unittest')));
|
||||||
|
|
||||||
|
// We create a project and set our user as project manager
|
||||||
|
$this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
|
||||||
|
$this->assertTrue($pp->addMember(1, 2));
|
||||||
|
$this->assertTrue($pp->isMember(1, 2));
|
||||||
|
$this->assertFalse($pp->isManager(1, 2));
|
||||||
|
|
||||||
|
// We fake a session for him
|
||||||
|
$session['user'] = array(
|
||||||
|
'id' => 2,
|
||||||
|
'is_admin' => false,
|
||||||
|
'is_project_admin' => true,
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertTrue($h->isProjectAdministrationAllowed(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIsProjectAdministrationAllowedForProjectMember()
|
||||||
|
{
|
||||||
|
$h = new User($this->container);
|
||||||
|
$p = new Project($this->container);
|
||||||
|
$pp = new ProjectPermission($this->container);
|
||||||
|
$u = new UserModel($this->container);
|
||||||
|
$session = new Session;
|
||||||
|
|
||||||
|
// We create our user
|
||||||
|
$this->assertEquals(2, $u->create(array('username' => 'unittest', 'password' => 'unittest')));
|
||||||
|
|
||||||
|
// We create a project and set our user as project member
|
||||||
|
$this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
|
||||||
|
$this->assertTrue($pp->addMember(1, 2));
|
||||||
|
$this->assertTrue($pp->isMember(1, 2));
|
||||||
|
$this->assertFalse($pp->isManager(1, 2));
|
||||||
|
|
||||||
|
// We fake a session for him
|
||||||
|
$session['user'] = array(
|
||||||
|
'id' => 2,
|
||||||
|
'is_admin' => false,
|
||||||
|
'is_project_admin' => false,
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertFalse($h->isProjectAdministrationAllowed(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIsProjectAdministrationAllowedForProjectManager()
|
||||||
|
{
|
||||||
|
$h = new User($this->container);
|
||||||
|
$p = new Project($this->container);
|
||||||
|
$pp = new ProjectPermission($this->container);
|
||||||
|
$u = new UserModel($this->container);
|
||||||
|
$session = new Session;
|
||||||
|
|
||||||
|
// We create our user
|
||||||
|
$this->assertEquals(2, $u->create(array('username' => 'unittest', 'password' => 'unittest')));
|
||||||
|
|
||||||
|
// We create a project and set our user as project member
|
||||||
|
$this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
|
||||||
|
$this->assertTrue($pp->addManager(1, 2));
|
||||||
|
$this->assertTrue($pp->isMember(1, 2));
|
||||||
|
$this->assertTrue($pp->isManager(1, 2));
|
||||||
|
|
||||||
|
// We fake a session for him
|
||||||
|
$session['user'] = array(
|
||||||
|
'id' => 2,
|
||||||
|
'is_admin' => false,
|
||||||
|
'is_project_admin' => false,
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertFalse($h->isProjectAdministrationAllowed(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIsProjectManagementAllowedForProjectAdmin()
|
||||||
|
{
|
||||||
|
$h = new User($this->container);
|
||||||
|
$p = new Project($this->container);
|
||||||
|
$pp = new ProjectPermission($this->container);
|
||||||
|
$u = new UserModel($this->container);
|
||||||
|
$session = new Session;
|
||||||
|
|
||||||
|
// We create our user
|
||||||
|
$this->assertEquals(2, $u->create(array('username' => 'unittest', 'password' => 'unittest')));
|
||||||
|
|
||||||
|
// We create a project and set our user as project manager
|
||||||
|
$this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
|
||||||
|
$this->assertTrue($pp->addMember(1, 2));
|
||||||
|
$this->assertTrue($pp->isMember(1, 2));
|
||||||
|
$this->assertFalse($pp->isManager(1, 2));
|
||||||
|
|
||||||
|
// We fake a session for him
|
||||||
|
$session['user'] = array(
|
||||||
|
'id' => 2,
|
||||||
|
'is_admin' => false,
|
||||||
|
'is_project_admin' => true,
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertTrue($h->isProjectManagementAllowed(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIsProjectManagementAllowedForProjectMember()
|
||||||
|
{
|
||||||
|
$h = new User($this->container);
|
||||||
|
$p = new Project($this->container);
|
||||||
|
$pp = new ProjectPermission($this->container);
|
||||||
|
$u = new UserModel($this->container);
|
||||||
|
$session = new Session;
|
||||||
|
|
||||||
|
// We create our user
|
||||||
|
$this->assertEquals(2, $u->create(array('username' => 'unittest', 'password' => 'unittest')));
|
||||||
|
|
||||||
|
// We create a project and set our user as project member
|
||||||
|
$this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
|
||||||
|
$this->assertTrue($pp->addMember(1, 2));
|
||||||
|
$this->assertTrue($pp->isMember(1, 2));
|
||||||
|
$this->assertFalse($pp->isManager(1, 2));
|
||||||
|
|
||||||
|
// We fake a session for him
|
||||||
|
$session['user'] = array(
|
||||||
|
'id' => 2,
|
||||||
|
'is_admin' => false,
|
||||||
|
'is_project_admin' => false,
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertFalse($h->isProjectManagementAllowed(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIsProjectManagementAllowedForProjectManager()
|
||||||
|
{
|
||||||
|
$h = new User($this->container);
|
||||||
|
$p = new Project($this->container);
|
||||||
|
$pp = new ProjectPermission($this->container);
|
||||||
|
$u = new UserModel($this->container);
|
||||||
|
$session = new Session;
|
||||||
|
|
||||||
|
// We create our user
|
||||||
|
$this->assertEquals(2, $u->create(array('username' => 'unittest', 'password' => 'unittest')));
|
||||||
|
|
||||||
|
// We create a project and set our user as project member
|
||||||
|
$this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
|
||||||
|
$this->assertTrue($pp->addManager(1, 2));
|
||||||
|
$this->assertTrue($pp->isMember(1, 2));
|
||||||
|
$this->assertTrue($pp->isManager(1, 2));
|
||||||
|
|
||||||
|
// We fake a session for him
|
||||||
|
$session['user'] = array(
|
||||||
|
'id' => 2,
|
||||||
|
'is_admin' => false,
|
||||||
|
'is_project_admin' => false,
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertTrue($h->isProjectManagementAllowed(1));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user