Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
+71
-60
@@ -11,21 +11,32 @@
|
||||
namespace IPS\Data;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use Exception;
|
||||
use IPS\Data\Cache\None;
|
||||
use IPS\Data\Store\Exception as StoreException;
|
||||
use OutOfRangeException;
|
||||
use function defined;
|
||||
use function is_array;
|
||||
use const IPS\CACHE_METHOD;
|
||||
use const IPS\STORE_CONFIG;
|
||||
use const IPS\STORE_METHOD;
|
||||
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract Storage Class
|
||||
*/
|
||||
abstract class _Store extends AbstractData
|
||||
abstract class Store extends AbstractData
|
||||
{
|
||||
/**
|
||||
* @brief Instance
|
||||
*/
|
||||
protected static $instance;
|
||||
protected static ?Store $instance = NULL;
|
||||
|
||||
/**
|
||||
* Available Data Store Methods
|
||||
@@ -45,32 +56,32 @@ abstract class _Store extends AbstractData
|
||||
/**
|
||||
* Get instance
|
||||
*
|
||||
* @return \IPS\Data\Store
|
||||
* @return Store
|
||||
*/
|
||||
public static function i()
|
||||
public static function i(): Store
|
||||
{
|
||||
if( static::$instance === NULL )
|
||||
{
|
||||
try
|
||||
{
|
||||
if( !isset( static::availableMethods()[ \IPS\STORE_METHOD ] ) )
|
||||
if( !isset( static::availableMethods()[ STORE_METHOD ] ) )
|
||||
{
|
||||
throw new \IPS\Data\Store\Exception;
|
||||
throw new StoreException;
|
||||
}
|
||||
|
||||
$classname = static::availableMethods()[ \IPS\STORE_METHOD ];
|
||||
$classname = static::availableMethods()[ STORE_METHOD ];
|
||||
|
||||
if( !$classname::supported() )
|
||||
{
|
||||
throw new \IPS\Data\Store\Exception;
|
||||
throw new StoreException;
|
||||
}
|
||||
|
||||
static::$instance = new $classname( json_decode( \IPS\STORE_CONFIG, TRUE ) );
|
||||
static::$instance = new $classname( json_decode( STORE_CONFIG, TRUE ) );
|
||||
}
|
||||
catch( \IPS\Data\Store\Exception $e )
|
||||
catch( StoreException $e )
|
||||
{
|
||||
/* Fall back to MySQL (if not using it currently, and then FileSystem if we are) */
|
||||
if ( \IPS\STORE_METHOD !== 'Database' )
|
||||
if ( STORE_METHOD !== 'Database' )
|
||||
{
|
||||
$classname = static::availableMethods()['Database'];
|
||||
}
|
||||
@@ -79,7 +90,7 @@ abstract class _Store extends AbstractData
|
||||
$classname = static::availableMethods()['FileSystem'];
|
||||
}
|
||||
|
||||
static::$instance = new $classname( json_decode( \IPS\STORE_CONFIG, TRUE ) );
|
||||
static::$instance = new $classname( json_decode( STORE_CONFIG, TRUE ) );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,33 +100,33 @@ abstract class _Store extends AbstractData
|
||||
/**
|
||||
* @brief Always needed Store keys
|
||||
*/
|
||||
public $initLoad = array();
|
||||
public array $initLoad = array();
|
||||
|
||||
/**
|
||||
* @brief Template store keys
|
||||
*/
|
||||
public $templateLoad = array();
|
||||
public array $templateLoad = array();
|
||||
|
||||
/**
|
||||
* @brief Log
|
||||
*/
|
||||
public $log = array();
|
||||
public array $log = array();
|
||||
|
||||
/**
|
||||
* Test to see if this store works
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function testStore()
|
||||
public static function testStore(): bool
|
||||
{
|
||||
try
|
||||
{
|
||||
$classname = 'IPS\Data\Store\\' . \IPS\STORE_METHOD;
|
||||
$store = new $classname( json_decode( \IPS\STORE_CONFIG, TRUE ) );
|
||||
$classname = 'IPS\Data\Store\\' . STORE_METHOD;
|
||||
$store = new $classname( json_decode( STORE_CONFIG, TRUE ) );
|
||||
|
||||
return $store->test();
|
||||
}
|
||||
catch( \Exception $e )
|
||||
catch( Exception $e )
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
@@ -138,7 +149,7 @@ abstract class _Store extends AbstractData
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function test()
|
||||
public function test(): bool
|
||||
{
|
||||
/* Set a test key */
|
||||
$key = 'test_' . md5( mt_rand() );
|
||||
@@ -154,7 +165,7 @@ abstract class _Store extends AbstractData
|
||||
|
||||
unset( $this->$key );
|
||||
|
||||
return ( $value == 1 ) ? TRUE : FALSE;
|
||||
return $value == 1;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
@@ -163,10 +174,10 @@ abstract class _Store extends AbstractData
|
||||
/**
|
||||
* Magic Method: Get
|
||||
*
|
||||
* @param string $key Key
|
||||
* @return string Value from the _datastore
|
||||
* @param string $key Key
|
||||
* @return mixed Value from the _datastore
|
||||
*/
|
||||
public function __get( $key )
|
||||
public function __get( string $key ): mixed
|
||||
{
|
||||
/* Try to get it from the cache store... */
|
||||
try
|
||||
@@ -174,25 +185,25 @@ abstract class _Store extends AbstractData
|
||||
/* If caching is enabled, and this isn't the special map of hashes... */
|
||||
if ( $this->useCache( $key ) )
|
||||
{
|
||||
if ( \IPS\Data\Cache::i()->checkKeys() === false )
|
||||
if ( Cache::i()->checkKeys() === false )
|
||||
{
|
||||
if ( isset( \IPS\Data\Cache::i()->$key ) and \IPS\Data\Cache::i()->$key !== NULL )
|
||||
if ( isset( Cache::i()->$key ) and Cache::i()->$key !== NULL )
|
||||
{
|
||||
/* Cache engine always returns current content, so return it */
|
||||
return \IPS\Data\Cache::i()->$key;
|
||||
return Cache::i()->$key;
|
||||
}
|
||||
|
||||
/* Still here? throw an exception so we get it from the data storage engine */
|
||||
throw new \OutOfRangeException;
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* It exists in the caching engine, and we know the hash of the correct value... */
|
||||
$cacheKeys = ( isset( $this->cacheKeys ) and \is_array( $this->cacheKeys ) ) ? $this->cacheKeys : array();
|
||||
if ( array_key_exists( $key, $cacheKeys ) and isset( \IPS\Data\Cache::i()->$key ) )
|
||||
$cacheKeys = ( isset( $this->cacheKeys ) and is_array( $this->cacheKeys ) ) ? $this->cacheKeys : array();
|
||||
if ( array_key_exists( $key, $cacheKeys ) and isset( Cache::i()->$key ) )
|
||||
{
|
||||
/* Get it... */
|
||||
$value = \IPS\Data\Cache::i()->$key;
|
||||
$value = Cache::i()->$key;
|
||||
|
||||
/* But only use it if the hash matches */
|
||||
if( $cacheKeys[ $key ] == md5( json_encode( $value ) ) )
|
||||
@@ -204,11 +215,11 @@ abstract class _Store extends AbstractData
|
||||
}
|
||||
|
||||
/* Still here? throw an exception so we get it from the data storage engine */
|
||||
throw new \OutOfRangeException;
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
|
||||
/* If we couldn't get it from the cache engine, get it from the data storage engine */
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
/* Actually get it... */
|
||||
$value = parent::__get( $key );
|
||||
@@ -217,12 +228,12 @@ abstract class _Store extends AbstractData
|
||||
if ( $this->useCache( $key ) )
|
||||
{
|
||||
/* Set it in the caching engine... */
|
||||
\IPS\Data\Cache::i()->$key = $value;
|
||||
Cache::i()->$key = $value;
|
||||
|
||||
if ( \IPS\Data\Cache::i()->checkKeys() === true )
|
||||
if ( Cache::i()->checkKeys() === true )
|
||||
{
|
||||
/* And set the hash in the cacheKeys hash map */
|
||||
$cacheKeys = ( isset( $this->cacheKeys ) and \is_array( $this->cacheKeys ) ) ? $this->cacheKeys : array();
|
||||
$cacheKeys = ( isset( $this->cacheKeys ) and is_array( $this->cacheKeys ) ) ? $this->cacheKeys : array();
|
||||
$cacheKeys[ $key ] = md5( json_encode( $value ) );
|
||||
$this->cacheKeys = $cacheKeys;
|
||||
}
|
||||
@@ -235,11 +246,11 @@ abstract class _Store extends AbstractData
|
||||
/**
|
||||
* Magic Method: Set
|
||||
*
|
||||
* @param string $key Key
|
||||
* @param string $value Value
|
||||
* @param string $key Key
|
||||
* @param mixed $value Value
|
||||
* @return void
|
||||
*/
|
||||
public function __set( $key, $value )
|
||||
public function __set( string $key, mixed $value )
|
||||
{
|
||||
/* Actually set it in the data storage engine */
|
||||
parent::__set( $key, $value );
|
||||
@@ -248,14 +259,14 @@ abstract class _Store extends AbstractData
|
||||
if ( $this->useCache( $key ) )
|
||||
{
|
||||
/* Then also set it in the cache... */
|
||||
\IPS\Data\Cache::i()->$key = $value;
|
||||
Cache::i()->$key = $value;
|
||||
|
||||
/* And set the hash in the cacheKeys hash map */
|
||||
if ( \IPS\Data\Cache::i()->checkKeys() === true )
|
||||
if ( Cache::i()->checkKeys() === true )
|
||||
{
|
||||
if ( ( isset( $this->cacheKeys ) and \is_array( $this->cacheKeys ) ) )
|
||||
if ( ( isset( $this->cacheKeys ) and is_array( $this->cacheKeys ) ) )
|
||||
{
|
||||
$cacheKeys = \is_array( $this->cacheKeys ) ? $this->cacheKeys : array();
|
||||
$cacheKeys = $this->cacheKeys;
|
||||
$cacheKeys[ $key ] = md5( json_encode( $value ) );
|
||||
$this->cacheKeys = $cacheKeys;
|
||||
}
|
||||
@@ -270,28 +281,28 @@ abstract class _Store extends AbstractData
|
||||
/**
|
||||
* Magic Method: Isset
|
||||
*
|
||||
* @param string $key Key
|
||||
* @param string $key Key
|
||||
* @return bool
|
||||
*/
|
||||
public function __isset( $key )
|
||||
public function __isset( string $key ): bool
|
||||
{
|
||||
/* If caching is enabled, and this isn't the special map of hashes, try to get it from the cache store... */
|
||||
if ( $this->useCache( $key ) )
|
||||
{
|
||||
/* Does it exist in the caching engine? */
|
||||
if ( isset( \IPS\Data\Cache::i()->$key ) )
|
||||
if ( isset( Cache::i()->$key ) )
|
||||
{
|
||||
/* Get it... */
|
||||
$value = \IPS\Data\Cache::i()->$key;
|
||||
$value = Cache::i()->$key;
|
||||
|
||||
if ( \IPS\Data\Cache::i()->checkKeys() === false )
|
||||
if ( Cache::i()->checkKeys() === false )
|
||||
{
|
||||
/* Cache engine always returns current content, so return it */
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/* Only use it if the hash matches */
|
||||
$cacheKeys = ( isset( $this->cacheKeys ) and \is_array( $this->cacheKeys ) ) ? $this->cacheKeys : array();
|
||||
$cacheKeys = ( isset( $this->cacheKeys ) and is_array( $this->cacheKeys ) ) ? $this->cacheKeys : array();
|
||||
if ( isset( $cacheKeys[ $key ] ) and $cacheKeys[ $key ] == md5( json_encode( $value ) ) )
|
||||
{
|
||||
return TRUE;
|
||||
@@ -306,10 +317,10 @@ abstract class _Store extends AbstractData
|
||||
/**
|
||||
* Magic Method: Unset
|
||||
*
|
||||
* @param string $key Key
|
||||
* @param string $key Key
|
||||
* @return void
|
||||
*/
|
||||
public function __unset( $key )
|
||||
public function __unset( string $key )
|
||||
{
|
||||
/* Unset it in the data storage engine */
|
||||
parent::__unset( $key );
|
||||
@@ -318,17 +329,17 @@ abstract class _Store extends AbstractData
|
||||
if ( $this->useCache( $key ) )
|
||||
{
|
||||
/* Remove it from the cache store... */
|
||||
unset( \IPS\Data\Cache::i()->$key );
|
||||
unset( Cache::i()->$key );
|
||||
|
||||
if ( \IPS\Data\Cache::i()->checkKeys() === true )
|
||||
if ( Cache::i()->checkKeys() === true )
|
||||
{
|
||||
/* And from the special map of hashes */
|
||||
try
|
||||
{
|
||||
$cacheKeys = \is_array( $this->cacheKeys ) ? $this->cacheKeys : array();
|
||||
$cacheKeys = is_array( $this->cacheKeys ) ? $this->cacheKeys : array();
|
||||
}
|
||||
/* If the cache doesn't exist we shouldn't error out with an uncaught OutOfRangeException */
|
||||
catch( \OutOfRangeException $e )
|
||||
catch( OutOfRangeException $e )
|
||||
{
|
||||
$cacheKeys = array();
|
||||
}
|
||||
@@ -346,11 +357,11 @@ abstract class _Store extends AbstractData
|
||||
/**
|
||||
* Check the cache for this key?
|
||||
*
|
||||
* @param string $key The store key
|
||||
* @param string $key The store key
|
||||
* @return boolean
|
||||
*/
|
||||
protected function useCache( $key )
|
||||
protected function useCache( string $key ): bool
|
||||
{
|
||||
return ( \IPS\CACHE_METHOD != \IPS\STORE_METHOD and ( ! \IPS\Data\Cache::i() instanceof \IPS\Data\Cache\None ) and $key !== 'cacheKeys' );
|
||||
return ( CACHE_METHOD != STORE_METHOD and ( ! Cache::i() instanceof None ) and $key !== 'cacheKeys' );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user