From 893584696b673655bf49c2afb1624a2d27d74048 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Thu, 28 Jul 2016 10:44:13 -0400 Subject: [PATCH] Move cache class to IOn namespace, use safer json for serialization in cache drivers --- app/bootstrap.php | 1 + app/config/database.toml.example | 12 ++++++++++- src/Aviat/AnimeClient/Command/BaseCommand.php | 5 ++++- src/Aviat/Ion/Cache/Driver/RedisDriver.php | 4 ++-- src/Aviat/Ion/Cache/Driver/SQLDriver.php | 14 ++++++++++--- src/Aviat/{AnimeClient => Ion}/Config.php | 15 +++++++------- src/Aviat/Ion/Exception/ConfigException.php | 20 +++++++++++++++++++ tests/AnimeClient/DispatcherTest.php | 2 +- tests/AnimeClient/MenuGeneratorTest.php | 7 +------ .../Model/AnimeCollectionModelTest.php | 3 ++- .../{CoreTest.php => RequirementsTest.php} | 20 +++++++++++++------ tests/AnimeClient/UrlGeneratorTest.php | 3 +-- tests/AnimeClient_TestCase.php | 11 +++++++++- tests/Ion/Cache/Driver/RedisDriver2Test.php | 3 +-- tests/{AnimeClient => Ion}/ConfigTest.php | 2 +- 15 files changed, 88 insertions(+), 34 deletions(-) rename src/Aviat/{AnimeClient => Ion}/Config.php (83%) create mode 100644 src/Aviat/Ion/Exception/ConfigException.php rename tests/AnimeClient/{CoreTest.php => RequirementsTest.php} (63%) rename tests/{AnimeClient => Ion}/ConfigTest.php (98%) diff --git a/app/bootstrap.php b/app/bootstrap.php index 6b3701ab..38c96305 100644 --- a/app/bootstrap.php +++ b/app/bootstrap.php @@ -13,6 +13,7 @@ use Monolog\Handler\RotatingFileHandler; use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\Response; +use Aviat\Ion\Config; use Aviat\Ion\Di\Container; use Aviat\Ion\Cache\CacheManager; use Aviat\AnimeClient\Auth\HummingbirdAuth; diff --git a/app/config/database.toml.example b/app/config/database.toml.example index d6d8656d..18b1ecc9 100644 --- a/app/config/database.toml.example +++ b/app/config/database.toml.example @@ -10,4 +10,14 @@ pass = "" port = "" name = "default" database = "" -file = "/../../anime_collection.sqlite" \ No newline at end of file +file = "anime_collection.sqlite" + +[cache] +type = "sqlite" +host = "" +user = "" +pass = "" +port = "" +name = "default" +database = "" +file = "anime_collection.sqlite" \ No newline at end of file diff --git a/src/Aviat/AnimeClient/Command/BaseCommand.php b/src/Aviat/AnimeClient/Command/BaseCommand.php index 2f4b9b48..bd50731f 100644 --- a/src/Aviat/AnimeClient/Command/BaseCommand.php +++ b/src/Aviat/AnimeClient/Command/BaseCommand.php @@ -17,12 +17,14 @@ use Aura\Session\SessionFactory; use ConsoleKit\Command; use ConsoleKit\Widgets\Box; +use Aviat\Ion\Config; use Aviat\Ion\Di\Container; use Aviat\Ion\Cache\CacheManager; -use Aviat\AnimeClient\Config; + use Aviat\AnimeClient\AnimeClient; use Aviat\AnimeClient\Auth\HummingbirdAuth; use Aviat\AnimeClient\Model; +use Aviat\AnimeClient\Util; /** * Base class for console command setup @@ -77,6 +79,7 @@ class BaseCommand extends Command { $container->set('manga-model', new Model\Manga($container)); $container->set('auth', new HummingbirdAuth($container)); + $container->set('util', new Util($container)); return $container; }; diff --git a/src/Aviat/Ion/Cache/Driver/RedisDriver.php b/src/Aviat/Ion/Cache/Driver/RedisDriver.php index b0b436d6..85e140d5 100644 --- a/src/Aviat/Ion/Cache/Driver/RedisDriver.php +++ b/src/Aviat/Ion/Cache/Driver/RedisDriver.php @@ -59,7 +59,7 @@ class RedisDriver implements CacheDriverInterface { */ 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) { - $this->redis->set($key, serialize($value)); + $this->redis->set($key, json_encode($value)); return $this; } diff --git a/src/Aviat/Ion/Cache/Driver/SQLDriver.php b/src/Aviat/Ion/Cache/Driver/SQLDriver.php index 483494e4..f3bbc64d 100644 --- a/src/Aviat/Ion/Cache/Driver/SQLDriver.php +++ b/src/Aviat/Ion/Cache/Driver/SQLDriver.php @@ -14,6 +14,7 @@ namespace Aviat\Ion\Cache\Driver; use Aviat\Ion\ConfigInterface; use Aviat\Ion\Cache\CacheDriverInterface; +use Aviat\Ion\Exception\ConfigException; use Aviat\Ion\Model\DB; /** @@ -31,11 +32,18 @@ class SQLDriver extends DB implements CacheDriverInterface { * Create the driver object * * @param ConfigInterface $config + * @throws ConfigException */ public function __construct(ConfigInterface $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); if ( ! empty($row)) { - return unserialize($row['value']); + return json_decode($row['value']); } return NULL; @@ -71,7 +79,7 @@ class SQLDriver extends DB implements CacheDriverInterface { { $this->db->set([ 'key' => $key, - 'value' => serialize($value), + 'value' => json_encode($value), ]); $this->db->insert('cache'); diff --git a/src/Aviat/AnimeClient/Config.php b/src/Aviat/Ion/Config.php similarity index 83% rename from src/Aviat/AnimeClient/Config.php rename to src/Aviat/Ion/Config.php index 3cbf9195..c0dea9f0 100644 --- a/src/Aviat/AnimeClient/Config.php +++ b/src/Aviat/Ion/Config.php @@ -1,19 +1,19 @@ 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('mcrypt')); + } - // Check for pdo_sqlite + public function testHasMcrypt() + { + $this->assertTrue(extension_loaded('mcrypt')); + } + + public function testHasPDO() + { $this->assertTrue(class_exists('PDO')); + } + + public function testHasPDOSqlite() + { $drivers = PDO::getAvailableDrivers(); $this->assertTrue(in_array('sqlite', $drivers)); } - } \ No newline at end of file diff --git a/tests/AnimeClient/UrlGeneratorTest.php b/tests/AnimeClient/UrlGeneratorTest.php index 3635dedb..aea2e044 100644 --- a/tests/AnimeClient/UrlGeneratorTest.php +++ b/tests/AnimeClient/UrlGeneratorTest.php @@ -1,7 +1,6 @@ 'default', 'database' => '', 'file' => ':memory:', + ], + 'cache' => [ + 'type' => 'sqlite', + 'host' => '', + 'user' => '', + 'pass' => '', + 'port' => '', + 'name' => 'default', + 'database' => '', + 'file' => ':memory:', ] ], 'routes' => [ diff --git a/tests/Ion/Cache/Driver/RedisDriver2Test.php b/tests/Ion/Cache/Driver/RedisDriver2Test.php index 4962c35a..62f5482d 100644 --- a/tests/Ion/Cache/Driver/RedisDriver2Test.php +++ b/tests/Ion/Cache/Driver/RedisDriver2Test.php @@ -2,8 +2,7 @@ require_once('CacheDriverBase.php'); -use Aviat\AnimeClient\Config; -use Aviat\Ion\Di\Container; +use Aviat\Ion\Config; use Aviat\Ion\Cache\Driver\RedisDriver; class CacheRedisDriverTestTwo extends AnimeClient_TestCase { diff --git a/tests/AnimeClient/ConfigTest.php b/tests/Ion/ConfigTest.php similarity index 98% rename from tests/AnimeClient/ConfigTest.php rename to tests/Ion/ConfigTest.php index b3134843..3cd84de2 100644 --- a/tests/AnimeClient/ConfigTest.php +++ b/tests/Ion/ConfigTest.php @@ -1,6 +1,6 @@