Start datastorage restructure

This commit is contained in:
Dennis Eichhorn 2018-02-28 21:43:29 +01:00
parent 797b1c1f00
commit b810e72588
19 changed files with 403 additions and 274 deletions

View File

@ -14,8 +14,7 @@ declare(strict_types=1);
namespace phpOMS\DataStorage\Cache;
use phpOMS\Config\OptionsInterface;
use phpOMS\Config\OptionsTrait;
use phpOMS\DataStorage\DataStoragePoolInterface;
/**
* Cache class.
@ -28,10 +27,8 @@ use phpOMS\Config\OptionsTrait;
* @link http://website.orange-management.de
* @since 1.0.0
*/
class CachePool implements OptionsInterface
class CachePool implements DataStoragePoolInterface
{
use OptionsTrait;
/**
* MemCache instance.
*

View File

@ -19,7 +19,7 @@ use phpOMS\Stdlib\Base\Enum;
/**
* Cache type enum.
*
* Possible caching types
* Cache types that are supported by the application
*
* @package phpOMS\DataStorage\Cache
* @license OMS License 1.0
@ -28,11 +28,8 @@ use phpOMS\Stdlib\Base\Enum;
*/
abstract class CacheType extends Enum
{
/* public */ const _INT = 0; /* Data is integer */
/* public */ const _STRING = 1; /* Data is string */
/* public */ const _ARRAY = 2; /* Data is array */
/* public */ const _SERIALIZABLE = 3; /* Data is object */
/* public */ const _FLOAT = 4; /* Data is float */
/* public */ const _BOOL = 5; /* Data is bool */
/* public */ const _JSONSERIALIZABLE = 6;
/* public */ const FILE = 'file';
/* public */ const MEMCACHED = 'sqlite';
/* public */ const REDIS = 'redis';
/* public */ const WINCACHE = 'win';
}

View File

@ -0,0 +1,38 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @package phpOMS\DataStorage\Cache\Connection
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://website.orange-management.de
*/
declare(strict_types=1);
namespace phpOMS\DataStorage\Cache\Connection;
use phpOMS\Stdlib\Base\Enum;
/**
* Cache type enum.
*
* Possible caching types
*
* @package phpOMS\DataStorage\Cache\Connection
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0
*/
abstract class CacheValueType extends Enum
{
/* public */ const _INT = 0; /* Data is integer */
/* public */ const _STRING = 1; /* Data is string */
/* public */ const _ARRAY = 2; /* Data is array */
/* public */ const _SERIALIZABLE = 3; /* Data is object */
/* public */ const _FLOAT = 4; /* Data is float */
/* public */ const _BOOL = 5; /* Data is bool */
/* public */ const _JSONSERIALIZABLE = 6;
}

View File

@ -0,0 +1,161 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @package phpOMS\DataStorage\Cache\Connection
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://website.orange-management.de
*/
declare(strict_types=1);
namespace phpOMS\DataStorage\Cache\Connection;
use phpOMS\DataStorage\Cache\CacheStatus;
/**
* Cache handler.
*
* Handles the cache connection.
* Implementing wrapper functions for multiple caches is planned (far away).
*
* @package phpOMS\DataStorage\Cache\Connection
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0
*/
abstract class ConnectionAbstract implements ConnectionInterface
{
/**
* Connection object.
*
* This can be used externally to define queries and execute them.
*
* @var ConnectionInterface
* @since 1.0.0
*/
private $con = null;
/**
* Database prefix.
*
* The database prefix name for unique table names
*
* @var string
* @since 1.0.0
*/
public $prefix = '';
/**
* Database data.
*
* @var string[]
* @since 1.0.0
*/
protected $dbdata = null;
/**
* Database type.
*
* @var string
* @since 1.0.0
*/
protected $type = 'undefined';
/**
* Database status.
*
* @var int
* @since 1.0.0
*/
protected $status = CacheStatus::INACTIVE;
/**
* {@inheritdoc}
*/
public function getType() : string
{
return $this->type;
}
/**
* {@inheritdoc}
*/
public function getStatus() : int
{
return $this->status;
}
/**
* Get database name.
*
* @return string
*
* @since 1.0.0
*/
public function getCache() : string
{
return $this->dbdata['database'] ?? '';
}
/**
* Get database host.
*
* @return string
*
* @since 1.0.0
*/
public function getHost() : string
{
return $this->dbdata['host'] ?? '';
}
/**
* Get database port.
*
* @return int
*
* @since 1.0.0
*/
public function getPort() : int
{
return (int) $this->dbdata['port'] ?? 0;
}
/**
* Get table prefix.
*
* @return string
*
* @since 1.0.0
*/
public function getPrefix() : string
{
return $this->prefix;
}
/**
* Object destructor.
*
* Sets the database connection to null
*
* @since 1.0.0
*/
public function __destruct()
{
$this->close();
}
/**
* {@inheritdoc}
*/
public function close() /* : void */
{
$this->con = null;
$this->status = CacheStatus::INACTIVE;
}
}

View File

@ -4,7 +4,7 @@
*
* PHP Version 7.1
*
* @package phpOMS\DataStorage\Cache
* @package phpOMS\DataStorage\Cache\Connection
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
@ -12,12 +12,14 @@
*/
declare(strict_types=1);
namespace phpOMS\DataStorage\Cache;
namespace phpOMS\DataStorage\Cache\Connection;
use phpOMS\DataStorage\Cache\CacheStatus;
/**
* Cache connection factory.
*
* @package phpOMS\DataStorage\Cache
* @package phpOMS\DataStorage\Cache\Connection
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0
@ -40,19 +42,25 @@ class CacheFactory
*
* Overwrites current connection if existing
*
* @param string[] $cacheData the basic database information for establishing a connection
* @param string[] $cacheData the basic cache information for establishing a connection
*
* @return CacheInterface
*
* @throws \InvalidArgumentException Throws this exception if the database is not supported.
* @throws \InvalidArgumentException Throws this exception if the cache is not supported.
*
* @since 1.0.0
*/
public static function create(array $cacheData) : CacheInterface
{
switch ($cacheData['type']) {
case 'file':
case CacheStatus::FILE:
return new FileCache($cacheData['path']);
case CacheStatus::REDIS:
return new RedisCache($cacheData);
case CacheStatus::MEMCACHED:
return new MemcachedCache($cacheData);
case CacheStatus::WINCACHE:
return new WinCache($cacheData);
default:
throw new \InvalidArgumentException('Cache "' . $cacheData['type'] . '" is not supported.');
}

View File

@ -4,7 +4,7 @@
*
* PHP Version 7.1
*
* @package phpOMS\DataStorage\Cache
* @package phpOMS\DataStorage\Cache\Connection
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
@ -12,19 +12,19 @@
*/
declare(strict_types=1);
namespace phpOMS\DataStorage\Cache;
namespace phpOMS\DataStorage\Cache\Connection;
use phpOMS\Stdlib\Base\Exception\InvalidEnumValue;
/**
* Cache interface.
*
* @package phpOMS\DataStorage\Cache
* @package phpOMS\DataStorage\Cache\Connection
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0
*/
interface CacheInterface
interface ConnectionInterface extends DataStorageConnectionInterface
{
/**

View File

@ -4,7 +4,7 @@
*
* PHP Version 7.1
*
* @package phpOMS\DataStorage\Cache
* @package phpOMS\DataStorage\Cache\Connection
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
@ -12,7 +12,7 @@
*/
declare(strict_types=1);
namespace phpOMS\DataStorage\Cache;
namespace phpOMS\DataStorage\Cache\Connection;
use phpOMS\Stdlib\Base\Exception\InvalidEnumValue;
use phpOMS\System\File\Local\Directory;
@ -23,12 +23,12 @@ use phpOMS\System\File\Local\File;
*
* PHP Version 7.1
*
* @package phpOMS\DataStorage\Cache
* @package phpOMS\DataStorage\Cache\Connection
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0
*/
class FileCache implements CacheInterface
class FileCache extends ConnectionAbstract
{
/**

View File

@ -4,7 +4,7 @@
*
* PHP Version 7.1
*
* @package phpOMS\DataStorage\Cache
* @package phpOMS\DataStorage\Cache\Connection
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
@ -12,17 +12,17 @@
*/
declare(strict_types=1);
namespace phpOMS\DataStorage\Cache;
namespace phpOMS\DataStorage\Cache\Connection;
/**
* Memcache class.
*
* @package phpOMS\DataStorage\Cache
* @package phpOMS\DataStorage\Cache\Connection
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0
*/
class MemCached implements CacheInterface
class MemCached extends ConnectionAbstract
{
/**

View File

@ -4,7 +4,7 @@
*
* PHP Version 7.1
*
* @package phpOMS\DataStorage\Cache
* @package phpOMS\DataStorage\Cache\Connection
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
@ -12,17 +12,17 @@
*/
declare(strict_types=1);
namespace phpOMS\DataStorage\Cache;
namespace phpOMS\DataStorage\Cache\Connection;
/**
* Null cache class.
*
* @package phpOMS\DataStorage\Cache
* @package phpOMS\DataStorage\Cache\Connection
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0
*/
class NullCache implements CacheInterface
class NullCache extends ConnectionAbstract
{
/**

View File

@ -4,7 +4,7 @@
*
* PHP Version 7.1
*
* @package phpOMS\DataStorage\Cache
* @package phpOMS\DataStorage\Cache\Connection
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
@ -12,19 +12,19 @@
*/
declare(strict_types=1);
namespace phpOMS\DataStorage\Cache;
namespace phpOMS\DataStorage\Cache\Connection;
/**
* RedisCache class.
*
* PHP Version 5.6
*
* @package phpOMS\DataStorage\Cache
* @package phpOMS\DataStorage\Cache\Connection
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0
*/
class RedisCache implements CacheInterface
class RedisCache extends ConnectionAbstract
{
/**

View File

@ -4,7 +4,7 @@
*
* PHP Version 7.1
*
* @package phpOMS\DataStorage\Cache
* @package phpOMS\DataStorage\Cache\Connection
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
@ -12,19 +12,19 @@
*/
declare(strict_types=1);
namespace phpOMS\DataStorage\Cache;
namespace phpOMS\DataStorage\Cache\Connection;
/**
* WinCache class.
*
* PHP Version 5.6
*
* @package phpOMS\DataStorage\Cache
* @package phpOMS\DataStorage\Cache\Connection
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0
*/
class WinCache implements CacheInterface
class WinCache extends ConnectionAbstract
{
/**

View File

@ -1,179 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @package phpOMS\DataStorage\Cache
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://website.orange-management.de
*/
declare(strict_types=1);
namespace phpOMS\DataStorage\Cache;
/**
* Memcache class.
*
* @package phpOMS\DataStorage\Cache
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0
*/
class MemCache implements CacheInterface
{
/**
* Memcache instance.
*
* @var \Memcache
* @since 1.0.0
*/
private $memc = null;
/**
* Only cache if data is larger than threshold (0-100).
*
* @var int
* @since 1.0.0
*/
private $threshold = 10;
private $status;
/**
* Constructor.
*
* @since 1.0.0
*/
public function __construct()
{
$this->memc = null;
}
/**
* Adding server to server pool.
*
* @param mixed $data Server data array
*
* @return void
*
* @since 1.0.0
*/
public function addServer($data)
{
$this->memc->addServer($data['host'], $data['port'], $data['timeout']);
}
/**
* {@inheritdoc}
*/
public function set($key, $value, int $expire = -1) /* : void */
{
$this->memc->set($key, $value, false, $expire);
}
/**
* {@inheritdoc}
*/
public function add($key, $value, int $expire = -1) : bool
{
return $this->memc->add($key, $value, false, $expire);
}
/**
* {@inheritdoc}
*/
public function get($key, int $expire = -1)
{
return $this->memc->get($key);
}
/**
* {@inheritdoc}
*/
public function delete($key, int $expire = -1) : bool
{
$this->memc->delete($key);
}
/**
* {@inheritdoc}
*/
public function flush(int $expire = 0) : bool
{
$this->memc->flush();
return true;
}
/**
* {@inheritdoc}
*/
public function flushAll() : bool
{
$this->memc->flush();
return true;
}
/**
* {@inheritdoc}
*/
public function replace($key, $value, int $expire = -1) : bool
{
$this->memc->replace($key, $value, false, $expire);
}
/**
* {@inheritdoc}
*/
public function stats() : array
{
/** @noinspection PhpMethodOrClassCallIsNotCaseSensitiveInspection */
return $this->memc->getExtendedStats();
}
/**
* {@inheritdoc}
*/
public function getThreshold() : int
{
return $this->threshold;
}
/**
* {@inheritdoc}
*/
public function setStatus(int $status) /* : void */
{
$this->status = $status;
}
/**
* Destructor.
*
* @since 1.0.0
*/
public function __destruct()
{
$this->close();
}
/**
* Closing cache.
*
* @return void
*
* @since 1.0.0
*/
public function close()
{
if ($this->memc !== null) {
$this->memc->close();
$this->memc = null;
}
}
}

View File

@ -0,0 +1,69 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @package phpOMS\DataStorage
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://website.orange-management.de
*/
declare(strict_types=1);
namespace phpOMS\DataStorage;
/**
* Database connection interface.
*
* @package phpOMS\DataStorage
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0
*/
interface DataStorageConnectionInterface
{
/**
* Connect to datastorage.
*
* Overwrites current connection if existing
*
* @param string[] $dbdata the basic datastorage information for establishing a connection
*
* @return void
*
* @todo make private
*
* @since 1.0.0
*/
public function connect(array $dbdata); /* : void */
/**
* Get the datastorage type.
*
* @return string
*
* @since 1.0.0
*/
public function getType() : string;
/**
* Get the datastorage status.
*
* @return int
*
* @since 1.0.0
*/
public function getStatus() : int;
/**
* Close datastorage connection.
*
* @return void
*
* @since 1.0.0
*/
public function close(); /* : void */
}

View File

@ -0,0 +1,75 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @package phpOMS\DataStorage
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://website.orange-management.de
*/
declare(strict_types=1);
namespace phpOMS\DataStorage;
/**
* Datamapper interface.
*
* DB, Cache, Session
*
* @package phpOMS\DataStorage
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0
*/
interface DataStoragePoolInterface
{
/**
* Add connection.
*
* @param mixed $key Connection key
* @param ConnectionInterface $db Connection
*
* @return bool
*
* @since 1.0.0
*/
public function add(string $key, ConnectionInterface $db);
/**
* Get connection.
*
* @param mixed $key Connection key
*
* @return mixed
*
* @since 1.0.0
*/
public function get(string $key = '');
/**
* Remove connection.
*
* @param mixed $key Connection key
*
* @return bool
*
* @since 1.0.0
*/
public function remove(string $key) : bool;
/**
* Create connection.
*
* @param mixed $key Connection key
* @param array $config Connection config data
*
* @return bool
*
* @since 1.0.0
*/
public function create($key, array $config) : bool;
}

View File

@ -46,6 +46,8 @@ abstract class ConnectionAbstract implements ConnectionInterface
* Database prefix.
*
* The database prefix name for unique table names
*
* @todo: make private? could add huge overhead since function call required
*
* @var string
* @since 1.0.0

View File

@ -25,49 +25,8 @@ use phpOMS\DataStorage\Database\Schema\Grammar\Grammar as SchemaGrammar;
* @link http://website.orange-management.de
* @since 1.0.0
*/
interface ConnectionInterface
interface ConnectionInterface extends DataStorageConnectionInterface
{
/**
* Connect to database.
*
* Overwrites current connection if existing
*
* @param string[] $dbdata the basic database information for establishing a connection
*
* @return void
*
* @since 1.0.0
*/
public function connect(array $dbdata); /* : void */
/**
* Get the database type.
*
* @return string
*
* @since 1.0.0
*/
public function getType() : string;
/**
* Get the database status.
*
* @return int
*
* @since 1.0.0
*/
public function getStatus() : int;
/**
* Close database connection.
*
* @return void
*
* @since 1.0.0
*/
public function close(); /* : void */
/**
* Return grammar for this connection.
*

View File

@ -14,6 +14,8 @@ declare(strict_types=1);
namespace phpOMS\DataStorage\Database;
use phpOMS\DataStorage\DataStoragePoolInterface;
use phpOMS\DataStorage\DataStorageConnectionInterface;
use phpOMS\DataStorage\Database\Connection\ConnectionAbstract;
use phpOMS\DataStorage\Database\Connection\ConnectionFactory;
@ -25,7 +27,7 @@ use phpOMS\DataStorage\Database\Connection\ConnectionFactory;
* @link http://website.orange-management.de
* @since 1.0.0
*/
class DatabasePool
class DatabasePool implements DataStoragePoolInterface
{
/**
@ -48,14 +50,14 @@ class DatabasePool
/**
* Add database.
*
* @param mixed $key Database key
* @param ConnectionAbstract $db Database
* @param mixed $key Database key
* @param ConnectionInterface $db Database
*
* @return bool
*
* @since 1.0.0
*/
public function add(string $key, ConnectionAbstract $db) : bool
public function add(string $key, ConnectionInterface $db) : bool
{
if (isset($this->pool[$key])) {
return false;
@ -71,11 +73,11 @@ class DatabasePool
*
* @param mixed $key Database key
*
* @return ConnectionAbstract|null
* @return ConnectionInterface|null
*
* @since 1.0.0
*/
public function get(string $key = '') /* : ?ConnectionAbstract */
public function get(string $key = '') /* : ?ConnectionInterface */
{
if ((!empty($key) && !isset($this->pool[$key])) || empty($this->pool)) {
return null;

View File

@ -4,7 +4,7 @@
*
* PHP Version 7.1
*
* @package TBD
* @package phpOMS\DataStorage\Database
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
@ -21,7 +21,7 @@ use phpOMS\Stdlib\Base\Enum;
*
* Possible database connection status
*
* @package Framework
* @package phpOMS\DataStorage\Database
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0

View File

@ -4,7 +4,7 @@
*
* PHP Version 7.1
*
* @package TBD
* @package phpOMS\DataStorage\Database
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
@ -21,7 +21,7 @@ use phpOMS\Stdlib\Base\Enum;
*
* Database types that are supported by the application
*
* @package Framework
* @package phpOMS\DataStorage\Database
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0