Move cache class to IOn namespace, use safer json for serialization in cache drivers

This commit is contained in:
Timothy Warren 2016-07-28 10:44:13 -04:00
parent 3d66fa9c11
commit ba6ada32f9
15 changed files with 88 additions and 34 deletions

View File

@ -13,6 +13,7 @@ use Monolog\Handler\RotatingFileHandler;
use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Response; use Zend\Diactoros\Response;
use Aviat\Ion\Config;
use Aviat\Ion\Di\Container; use Aviat\Ion\Di\Container;
use Aviat\Ion\Cache\CacheManager; use Aviat\Ion\Cache\CacheManager;
use Aviat\AnimeClient\Auth\HummingbirdAuth; use Aviat\AnimeClient\Auth\HummingbirdAuth;

View File

@ -10,4 +10,14 @@ pass = ""
port = "" port = ""
name = "default" name = "default"
database = "" database = ""
file = "/../../anime_collection.sqlite" file = "anime_collection.sqlite"
[cache]
type = "sqlite"
host = ""
user = ""
pass = ""
port = ""
name = "default"
database = ""
file = "anime_collection.sqlite"

View File

@ -17,12 +17,14 @@ use Aura\Session\SessionFactory;
use ConsoleKit\Command; use ConsoleKit\Command;
use ConsoleKit\Widgets\Box; use ConsoleKit\Widgets\Box;
use Aviat\Ion\Config;
use Aviat\Ion\Di\Container; use Aviat\Ion\Di\Container;
use Aviat\Ion\Cache\CacheManager; use Aviat\Ion\Cache\CacheManager;
use Aviat\AnimeClient\Config;
use Aviat\AnimeClient\AnimeClient; use Aviat\AnimeClient\AnimeClient;
use Aviat\AnimeClient\Auth\HummingbirdAuth; use Aviat\AnimeClient\Auth\HummingbirdAuth;
use Aviat\AnimeClient\Model; use Aviat\AnimeClient\Model;
use Aviat\AnimeClient\Util;
/** /**
* Base class for console command setup * Base class for console command setup
@ -77,6 +79,7 @@ class BaseCommand extends Command {
$container->set('manga-model', new Model\Manga($container)); $container->set('manga-model', new Model\Manga($container));
$container->set('auth', new HummingbirdAuth($container)); $container->set('auth', new HummingbirdAuth($container));
$container->set('util', new Util($container));
return $container; return $container;
}; };

View File

@ -59,7 +59,7 @@ class RedisDriver implements CacheDriverInterface {
*/ */
public function get($key) public function get($key)
{ {
return unserialize($this->redis->get($key)); return json_decode($this->redis->get($key));
} }
/** /**
@ -71,7 +71,7 @@ class RedisDriver implements CacheDriverInterface {
*/ */
public function set($key, $value) public function set($key, $value)
{ {
$this->redis->set($key, serialize($value)); $this->redis->set($key, json_encode($value));
return $this; return $this;
} }

View File

@ -14,6 +14,7 @@ namespace Aviat\Ion\Cache\Driver;
use Aviat\Ion\ConfigInterface; use Aviat\Ion\ConfigInterface;
use Aviat\Ion\Cache\CacheDriverInterface; use Aviat\Ion\Cache\CacheDriverInterface;
use Aviat\Ion\Exception\ConfigException;
use Aviat\Ion\Model\DB; use Aviat\Ion\Model\DB;
/** /**
@ -31,11 +32,18 @@ class SQLDriver extends DB implements CacheDriverInterface {
* Create the driver object * Create the driver object
* *
* @param ConfigInterface $config * @param ConfigInterface $config
* @throws ConfigException
*/ */
public function __construct(ConfigInterface $config) public function __construct(ConfigInterface $config)
{ {
parent::__construct($config); parent::__construct($config);
$this->db = \Query($this->db_config['collection']);
if ( ! array_key_exists('cache', $this->db_config))
{
throw new ConfigException("Missing '[cache]' section in database config.");
}
$this->db = \Query($this->db_config['cache']);
} }
/** /**
@ -54,7 +62,7 @@ class SQLDriver extends DB implements CacheDriverInterface {
$row = $query->fetch(\PDO::FETCH_ASSOC); $row = $query->fetch(\PDO::FETCH_ASSOC);
if ( ! empty($row)) if ( ! empty($row))
{ {
return unserialize($row['value']); return json_decode($row['value']);
} }
return NULL; return NULL;
@ -71,7 +79,7 @@ class SQLDriver extends DB implements CacheDriverInterface {
{ {
$this->db->set([ $this->db->set([
'key' => $key, 'key' => $key,
'value' => serialize($value), 'value' => json_encode($value),
]); ]);
$this->db->insert('cache'); $this->db->insert('cache');

View File

@ -1,19 +1,19 @@
<?php <?php
/** /**
* Hummingbird Anime Client * Ion
* *
* An API client for Hummingbird to manage anime and manga watch lists * Building blocks for web development
* *
* @package HummingbirdAnimeClient * @package Ion
* @author Timothy J. Warren * @author Timothy J. Warren
* @copyright Copyright (c) 2015 - 2016 * @copyright Copyright (c) 2015 - 2016
* @link https://github.com/timw4mail/HummingBirdAnimeClient
* @license MIT * @license MIT
*/ */
namespace Aviat\AnimeClient; namespace Aviat\Ion;
use Aviat\Ion\ConfigInterface;
use Aviat\Ion\Exception\ConfigException;
use InvalidArgumentException; use InvalidArgumentException;
@ -22,7 +22,7 @@ use InvalidArgumentException;
*/ */
class Config implements ConfigInterface { class Config implements ConfigInterface {
use \Aviat\Ion\ArrayWrapper; use ArrayWrapper;
/** /**
* Config object * Config object
@ -46,6 +46,7 @@ class Config implements ConfigInterface {
* *
* @param array|string $key * @param array|string $key
* @return mixed * @return mixed
* @throws ConfigException
*/ */
public function get($key) public function get($key)
{ {

View File

@ -0,0 +1,20 @@
<?php
/**
* Ion
*
* Building blocks for web development
*
* @package Ion
* @author Timothy J. Warren
* @copyright Copyright (c) 2015 - 2016
* @license MIT
*/
namespace Aviat\Ion\Exception;
/**
* Exception for bad configuration
*/
class ConfigException extends \InvalidArgumentException {
}

View File

@ -6,9 +6,9 @@ use Monolog\Handler\TestHandler;
use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Response; use Zend\Diactoros\Response;
use Aviat\Ion\Config;
use Aviat\Ion\Di\Container; use Aviat\Ion\Di\Container;
use Aviat\AnimeClient\Dispatcher; use Aviat\AnimeClient\Dispatcher;
use Aviat\AnimeClient\Config;
use Aviat\AnimeClient\UrlGenerator; use Aviat\AnimeClient\UrlGenerator;

View File

@ -1,11 +1,6 @@
<?php <?php
use Aura\Html\HelperLocatorFactory;
use Aviat\Ion\Friend; use Aviat\Ion\Friend;
use Aviat\Ion\Di\Container;
use Aviat\AnimeClient\Helper;
use Aviat\AnimeClient\Config;
use Aviat\AnimeClient\MenuGenerator; use Aviat\AnimeClient\MenuGenerator;
class MenuGeneratorTest extends AnimeClient_TestCase { class MenuGeneratorTest extends AnimeClient_TestCase {

View File

@ -1,7 +1,8 @@
<?php <?php
use Aviat\Ion\Config;
use Aviat\Ion\Friend; use Aviat\Ion\Friend;
use Aviat\AnimeClient\Config;
use Aviat\AnimeClient\Model\AnimeCollection as AnimeCollectionModel; use Aviat\AnimeClient\Model\AnimeCollection as AnimeCollectionModel;
class AnimeCollectionModelTest extends AnimeClient_TestCase { class AnimeCollectionModelTest extends AnimeClient_TestCase {

View File

@ -1,22 +1,30 @@
<?php <?php
class CoreTest extends AnimeClient_TestCase { class RequirementsTest extends AnimeClient_TestCase {
public function testPHPVersion() public function testPHPVersion()
{ {
$this->assertTrue(version_compare(PHP_VERSION, "5.4", "ge")); $this->assertTrue(version_compare(PHP_VERSION, "5.4", "ge"));
} }
public function testRequirements() public function testHasGd()
{ {
// Check required extensions
$this->assertTrue(extension_loaded('gd')); $this->assertTrue(extension_loaded('gd'));
$this->assertTrue(extension_loaded('mcrypt')); }
// Check for pdo_sqlite public function testHasMcrypt()
{
$this->assertTrue(extension_loaded('mcrypt'));
}
public function testHasPDO()
{
$this->assertTrue(class_exists('PDO')); $this->assertTrue(class_exists('PDO'));
}
public function testHasPDOSqlite()
{
$drivers = PDO::getAvailableDrivers(); $drivers = PDO::getAvailableDrivers();
$this->assertTrue(in_array('sqlite', $drivers)); $this->assertTrue(in_array('sqlite', $drivers));
} }
} }

View File

@ -1,7 +1,6 @@
<?php <?php
use Aviat\Ion\Di\Container; use Aviat\Ion\Config;
use Aviat\AnimeClient\Config;
use Aviat\AnimeClient\UrlGenerator; use Aviat\AnimeClient\UrlGenerator;
class UrlGeneratorTest extends AnimeClient_TestCase { class UrlGeneratorTest extends AnimeClient_TestCase {

View File

@ -9,7 +9,6 @@ use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Response as HttpResponse; use Zend\Diactoros\Response as HttpResponse;
use Aviat\AnimeClient\AnimeClient; use Aviat\AnimeClient\AnimeClient;
use Aviat\AnimeClient\Config;
define('ROOT_DIR', __DIR__ . '/../'); define('ROOT_DIR', __DIR__ . '/../');
define('TEST_DATA_DIR', __DIR__ . '/test_data'); define('TEST_DATA_DIR', __DIR__ . '/test_data');
@ -62,6 +61,16 @@ class AnimeClient_TestCase extends PHPUnit_Framework_TestCase {
'name' => 'default', 'name' => 'default',
'database' => '', 'database' => '',
'file' => ':memory:', 'file' => ':memory:',
],
'cache' => [
'type' => 'sqlite',
'host' => '',
'user' => '',
'pass' => '',
'port' => '',
'name' => 'default',
'database' => '',
'file' => ':memory:',
] ]
], ],
'routes' => [ 'routes' => [

View File

@ -2,8 +2,7 @@
require_once('CacheDriverBase.php'); require_once('CacheDriverBase.php');
use Aviat\AnimeClient\Config; use Aviat\Ion\Config;
use Aviat\Ion\Di\Container;
use Aviat\Ion\Cache\Driver\RedisDriver; use Aviat\Ion\Cache\Driver\RedisDriver;
class CacheRedisDriverTestTwo extends AnimeClient_TestCase { class CacheRedisDriverTestTwo extends AnimeClient_TestCase {

View File

@ -1,6 +1,6 @@
<?php <?php
use \Aviat\AnimeClient\Config; use Aviat\Ion\Config;
class ConfigTest extends AnimeClient_TestCase { class ConfigTest extends AnimeClient_TestCase {