Implemented ObjectStorage into Core.
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
After careful deliberation it was decided to merge ObjectStorage into Core, as it's slowly getting used by every part of FuzeWorks.
This commit is contained in:
parent
0b0f4998b4
commit
b5bf0425f3
35
.drone.yml
35
.drone.yml
@ -2,23 +2,52 @@ kind: pipeline
|
|||||||
type: docker
|
type: docker
|
||||||
name: test
|
name: test
|
||||||
|
|
||||||
|
services:
|
||||||
|
- name: cache
|
||||||
|
image: redis
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: composer
|
- name: composer
|
||||||
image: composer:latest
|
image: composer:latest
|
||||||
commands:
|
commands:
|
||||||
- composer install
|
- composer install
|
||||||
|
|
||||||
- name: php81test
|
- name: PHP81CoreTest
|
||||||
image: registry.i15.nl/i15/fuzephp:8.1-alpine
|
image: registry.i15.nl/i15/fuzephp:8.1-alpine
|
||||||
commands:
|
commands:
|
||||||
- docker-php-ext-enable xdebug
|
- docker-php-ext-enable xdebug
|
||||||
- vendor/bin/phpunit -c test/phpunit.xml
|
- vendor/bin/phpunit -c test/phpunit.xml --testsuite core
|
||||||
|
|
||||||
|
- name: PHP81DummyProviderTest
|
||||||
|
image: registry.i15.nl/i15/fuzephp:8.1-alpine
|
||||||
|
commands:
|
||||||
|
- docker-php-ext-enable xdebug
|
||||||
|
- vendor/bin/phpunit -c test/phpunit.xml --testsuite storage
|
||||||
|
environment:
|
||||||
|
OBJECTSTORAGE_PROVIDER: DummyProvider
|
||||||
|
|
||||||
|
- name: PHP81FileProviderTest
|
||||||
|
image: registry.i15.nl/i15/fuzephp:8.1-alpine
|
||||||
|
commands:
|
||||||
|
- docker-php-ext-enable xdebug
|
||||||
|
- vendor/bin/phpunit -c test/phpunit.xml --testsuite storage
|
||||||
|
environment:
|
||||||
|
OBJECTSTORAGE_PROVIDER: FileProvider
|
||||||
|
|
||||||
|
- name: PHP81RedisProviderTest
|
||||||
|
image: registry.i15.nl/i15/fuzephp:8.1-alpine
|
||||||
|
commands:
|
||||||
|
- docker-php-ext-enable xdebug
|
||||||
|
- vendor/bin/phpunit -c test/phpunit.xml --testsuite storage
|
||||||
|
environment:
|
||||||
|
OBJECTSTORAGE_PROVIDER: RedisProvider
|
||||||
|
OBJECTSTORAGE_REDIS_HOST: cache
|
||||||
|
|
||||||
- name: coverage
|
- name: coverage
|
||||||
image: registry.i15.nl/i15/fuzephp:8.1-alpine
|
image: registry.i15.nl/i15/fuzephp:8.1-alpine
|
||||||
commands:
|
commands:
|
||||||
- docker-php-ext-enable xdebug
|
- docker-php-ext-enable xdebug
|
||||||
- vendor/bin/phpunit -c test/phpunit.xml --coverage-text
|
- vendor/bin/phpunit -c test/phpunit.xml --testsuite core --coverage-text
|
||||||
|
|
||||||
image_pull_secrets:
|
image_pull_secrets:
|
||||||
- dockerconfig
|
- dockerconfig
|
@ -10,7 +10,8 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=8.1.0"
|
"php": ">=8.1.0",
|
||||||
|
"psr/simple-cache": "1.0.1"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^9",
|
"phpunit/phpunit": "^9",
|
||||||
|
60
src/Config/config.storage.php
Normal file
60
src/Config/config.storage.php
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* FuzeWorks ObjectStorage Component.
|
||||||
|
*
|
||||||
|
* The FuzeWorks PHP FrameWork
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013-2020 i15
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
* @author i15
|
||||||
|
* @copyright Copyright (c) 2013 - 2020, i15. (https://i15.nl)
|
||||||
|
* @license https://opensource.org/licenses/MIT MIT License
|
||||||
|
*
|
||||||
|
* @since Version 1.3.0
|
||||||
|
*
|
||||||
|
* @version Version 1.3.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
use FuzeWorks\Core;
|
||||||
|
|
||||||
|
return [
|
||||||
|
// Which provider shall be used
|
||||||
|
// Options: DummyProvider, RedisProvider, FileProvider
|
||||||
|
'StorageProvider' => Core::getEnv('STORAGE_PROVIDER', null),
|
||||||
|
'DummyProvider' => [],
|
||||||
|
'RedisProvider' => [
|
||||||
|
// Type can be 'tcp' or 'unix'
|
||||||
|
'socket_type' => Core::getEnv('STORAGE_REDIS_SOCKET_TYPE', 'tcp'),
|
||||||
|
// If socket_type == 'unix', set the socket here
|
||||||
|
'socket' => Core::getEnv('STORAGE_REDIS_SOCKET', null),
|
||||||
|
// If socket_type == 'tcp', set the host here
|
||||||
|
'host' => Core::getEnv('STORAGE_REDIS_HOST', '127.0.0.1'),
|
||||||
|
// And some standard settings
|
||||||
|
'port' => Core::getEnv('STORAGE_REDIS_PORT', 6379),
|
||||||
|
'password' => Core::getEnv('STORAGE_REDIS_PASSWORD', null),
|
||||||
|
'timeout' => Core::getEnv('STORAGE_REDIS_TIMEOUT', 0),
|
||||||
|
'db_index' => Core::getEnv('STORAGE_REDIS_DBINDEX', 0),
|
||||||
|
],
|
||||||
|
'FileProvider' => [
|
||||||
|
// The directory where objects get stored by the FileProvider
|
||||||
|
'storage_directory' => Core::getEnv('STORAGE_FILE_DIRECTORY', Core::$tempDir)
|
||||||
|
]
|
||||||
|
];
|
40
src/FuzeWorks/Exception/StorageException.php
Normal file
40
src/FuzeWorks/Exception/StorageException.php
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* FuzeWorks ObjectStorage Component.
|
||||||
|
*
|
||||||
|
* The FuzeWorks PHP FrameWork
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013-2020 i15
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
* @author i15
|
||||||
|
* @copyright Copyright (c) 2013 - 2020, i15. (https://i15.nl)
|
||||||
|
* @license https://opensource.org/licenses/MIT MIT License
|
||||||
|
*
|
||||||
|
* @since Version 1.3.0
|
||||||
|
*
|
||||||
|
* @version Version 1.3.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace FuzeWorks\Exception;
|
||||||
|
|
||||||
|
class StorageException extends CoreException
|
||||||
|
{
|
||||||
|
}
|
@ -114,6 +114,13 @@ class Factory
|
|||||||
*/
|
*/
|
||||||
public Plugins $plugins;
|
public Plugins $plugins;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Storage Object
|
||||||
|
*
|
||||||
|
* @var Storage
|
||||||
|
*/
|
||||||
|
public Storage $storage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Factory instance constructor. Should only really be called once
|
* Factory instance constructor. Should only really be called once
|
||||||
* @throws FactoryException
|
* @throws FactoryException
|
||||||
@ -131,6 +138,7 @@ class Factory
|
|||||||
$this->libraries = new Libraries();
|
$this->libraries = new Libraries();
|
||||||
$this->helpers = new Helpers();
|
$this->helpers = new Helpers();
|
||||||
$this->plugins = new Plugins();
|
$this->plugins = new Plugins();
|
||||||
|
$this->storage = new Storage();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
140
src/FuzeWorks/Storage.php
Normal file
140
src/FuzeWorks/Storage.php
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* FuzeWorks Storage Component.
|
||||||
|
*
|
||||||
|
* The FuzeWorks PHP FrameWork
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013-2020 i15
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
* @author i15
|
||||||
|
* @copyright Copyright (c) 2013 - 2020, i15. (https://i15.nl)
|
||||||
|
* @license https://opensource.org/licenses/MIT MIT License
|
||||||
|
*
|
||||||
|
* @since Version 1.3.0
|
||||||
|
*
|
||||||
|
* @version Version 1.4.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace FuzeWorks;
|
||||||
|
use FuzeWorks\Exception\ConfigException;
|
||||||
|
use FuzeWorks\Exception\FactoryException;
|
||||||
|
use FuzeWorks\Exception\StorageException;
|
||||||
|
use FuzeWorks\Storage\iStorageProvider;
|
||||||
|
use FuzeWorks\Storage\StorageCache;
|
||||||
|
use Psr\SimpleCache\CacheInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Storage Class.
|
||||||
|
*
|
||||||
|
* This class doesn't do very much, except show the FuzeWorks\Configurator that this dependency has been loaded.
|
||||||
|
*
|
||||||
|
* @author i15
|
||||||
|
* @copyright Copyright (c) 2013 - 2020, i15. (https://i15.nl)
|
||||||
|
*/
|
||||||
|
class Storage
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The configuration file for Storage
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private array $cfg;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The currently selected StorageProvider
|
||||||
|
*
|
||||||
|
* @var iStorageProvider
|
||||||
|
*/
|
||||||
|
private iStorageProvider $provider;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The currently used CacheInterface
|
||||||
|
*
|
||||||
|
* @var CacheInterface
|
||||||
|
*/
|
||||||
|
private CacheInterface $cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches and returns the currently selected StorageProvider
|
||||||
|
*
|
||||||
|
* @return iStorageProvider
|
||||||
|
*@throws StorageException
|
||||||
|
*/
|
||||||
|
public function getStorage(): iStorageProvider
|
||||||
|
{
|
||||||
|
// If the provider is already loaded, return that one
|
||||||
|
if (isset($this->provider))
|
||||||
|
return $this->provider;
|
||||||
|
|
||||||
|
// Load the config, if it isn't loaded yet
|
||||||
|
if (!isset($this->cfg))
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
/** @var Config $configs */
|
||||||
|
$configs = Factory::getInstance('config');
|
||||||
|
$this->cfg = $configs->getConfig('storage')->toArray();
|
||||||
|
} catch (ConfigException | FactoryException $e) {
|
||||||
|
throw new StorageException("Could not get StorageProvider. No config file named 'config.storage.php' could be found.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the currently selected StorageProvider
|
||||||
|
$selected = $this->cfg['StorageProvider'];
|
||||||
|
if (is_null($selected))
|
||||||
|
throw new StorageException("Could not get StorageProvider. Selected provider is null!");
|
||||||
|
|
||||||
|
// Try and load the StorageProvider
|
||||||
|
$class = '\FuzeWorks\Storage\Provider\\' . $selected;
|
||||||
|
if (!class_exists($class, true))
|
||||||
|
throw new StorageException("Could not get StorageProvider. Selected provider '".$selected."' is not recognized.");
|
||||||
|
|
||||||
|
/** @var iStorageProvider $provider */
|
||||||
|
$provider = new $class();
|
||||||
|
if (!$provider instanceof iStorageProvider)
|
||||||
|
throw new StorageException("Could not get StorageProvider. Selected provider '".$selected."' is not an instance of iStorageProvider'.");
|
||||||
|
|
||||||
|
// Fetch the parameters
|
||||||
|
$params = isset($this->cfg[$selected]) && is_array($this->cfg[$selected]) ? $this->cfg[$selected] : [];
|
||||||
|
if (!$provider->init($params))
|
||||||
|
throw new StorageException("Could not get StorageProvider. Selected provider '".$selected."' failed to load.");
|
||||||
|
|
||||||
|
$this->provider = $provider;
|
||||||
|
return $this->provider;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a PSR compatible Cache object
|
||||||
|
*
|
||||||
|
* @return CacheInterface
|
||||||
|
* @throws StorageException
|
||||||
|
*/
|
||||||
|
public function getCache(): CacheInterface
|
||||||
|
{
|
||||||
|
if (isset($this->cache))
|
||||||
|
return $this->cache;
|
||||||
|
|
||||||
|
$storageProvider = $this->getStorage();
|
||||||
|
$this->cache = new StorageCache($storageProvider);
|
||||||
|
|
||||||
|
return $this->cache;
|
||||||
|
}
|
||||||
|
}
|
117
src/FuzeWorks/Storage/Provider/DummyProvider.php
Normal file
117
src/FuzeWorks/Storage/Provider/DummyProvider.php
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* FuzeWorks ObjectStorage Component.
|
||||||
|
*
|
||||||
|
* The FuzeWorks PHP FrameWork
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013-2020 i15
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
* @author i15
|
||||||
|
* @copyright Copyright (c) 2013 - 2020, i15. (https://i15.nl)
|
||||||
|
* @license https://opensource.org/licenses/MIT MIT License
|
||||||
|
*
|
||||||
|
* @since Version 1.3.0
|
||||||
|
*
|
||||||
|
* @version Version 1.3.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace FuzeWorks\Storage\Provider;
|
||||||
|
use FuzeWorks\Storage\iStorageProvider;
|
||||||
|
|
||||||
|
class DummyProvider implements iStorageProvider
|
||||||
|
{
|
||||||
|
|
||||||
|
private array $data = ['index' => [], 'data' => []];
|
||||||
|
|
||||||
|
public function init(array $providerConfig): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getIndex(): array
|
||||||
|
{
|
||||||
|
return $this->data['index'];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getItem(string $key)
|
||||||
|
{
|
||||||
|
if (!in_array($key, $this->data['index']))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return $this->data['data'][$key]['data'];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getItemMeta(string $key): ?array
|
||||||
|
{
|
||||||
|
if (!in_array($key, $this->data['index']))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return $this->data['data'][$key]['meta'];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getItems(array $keys = []): array
|
||||||
|
{
|
||||||
|
$output = [];
|
||||||
|
foreach ($keys as $key)
|
||||||
|
$output[$key] = $this->getItem($key);
|
||||||
|
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hasItem(string $key): bool
|
||||||
|
{
|
||||||
|
return in_array($key, $this->data['index']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function clear(): bool
|
||||||
|
{
|
||||||
|
return $this->deleteItems($this->getIndex());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function deleteItem(string $key): bool
|
||||||
|
{
|
||||||
|
// Remove the index
|
||||||
|
if (($k = array_search($key, $this->data['index'])) !== false) {
|
||||||
|
unset($this->data['index'][$k]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// And remove the data
|
||||||
|
unset($this->data['data'][$key]);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function deleteItems(array $keys): bool
|
||||||
|
{
|
||||||
|
foreach ($keys as $key)
|
||||||
|
$this->deleteItem($key);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function save(string $key, $value, array $metaData = []): bool
|
||||||
|
{
|
||||||
|
if (!in_array($key, $this->data['index']))
|
||||||
|
$this->data['index'][] = $key;
|
||||||
|
|
||||||
|
$this->data['data'][$key] = ['data' => $value, 'meta' => $metaData];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
256
src/FuzeWorks/Storage/Provider/FileProvider.php
Normal file
256
src/FuzeWorks/Storage/Provider/FileProvider.php
Normal file
@ -0,0 +1,256 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* FuzeWorks ObjectStorage Component.
|
||||||
|
*
|
||||||
|
* The FuzeWorks PHP FrameWork
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013-2020 i15
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
* @author i15
|
||||||
|
* @copyright Copyright (c) 2013 - 2020, i15. (https://i15.nl)
|
||||||
|
* @license https://opensource.org/licenses/MIT MIT License
|
||||||
|
*
|
||||||
|
* @since Version 1.3.0
|
||||||
|
*
|
||||||
|
* @version Version 1.3.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace FuzeWorks\Storage\Provider;
|
||||||
|
use FuzeWorks\Core;
|
||||||
|
use FuzeWorks\Exception\StorageException;
|
||||||
|
use FuzeWorks\Storage\iStorageProvider;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FileProvider
|
||||||
|
*
|
||||||
|
* An ObjectStorage provider which saves objects in files using the serialize() and unserialize() functions.
|
||||||
|
* The easiest to use StorageProvider, though with its caveats. It's nimble and works on nearly everything, but
|
||||||
|
* not particularly fast, and has some risks when multiple processes use the data at the same time.
|
||||||
|
*
|
||||||
|
* @todo Figure out a way to prevent indexes from clashing in multiple threads
|
||||||
|
* @todo Find a better way to sanitize key names other than hashing them using crc32
|
||||||
|
* @author i15
|
||||||
|
* @copyright Copyright (c) 2013 - 2020, i15. (https://i15.nl)
|
||||||
|
*/
|
||||||
|
class FileProvider implements iStorageProvider
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The directory where all objects are saved
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected string $directory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The file where the index for this provider is saved
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected string $indexFile;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The index of all objects stored
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected array $index;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $providerConfig
|
||||||
|
* @return bool
|
||||||
|
* @throws StorageException
|
||||||
|
*/
|
||||||
|
public function init(array $providerConfig): bool
|
||||||
|
{
|
||||||
|
// First load the directory from the providerConfig
|
||||||
|
$directory = $providerConfig['storage_directory'] ?? null;
|
||||||
|
|
||||||
|
// Check if the directory exists
|
||||||
|
if (!file_exists($directory) || !is_dir($directory))
|
||||||
|
throw new StorageException("Could not load FileProvider Storage. Provided storageDirectory is not a directory.");
|
||||||
|
|
||||||
|
// Check if that directory also is writeable
|
||||||
|
if (!Core::isReallyWritable($directory))
|
||||||
|
throw new StorageException("Could not load FileProvider Storage. Provided storageDirectory is not writeable.");
|
||||||
|
|
||||||
|
// Save the directory and indexFile, and load or initialize the indexFile
|
||||||
|
$this->directory = rtrim($directory);
|
||||||
|
$this->indexFile = $this->directory . DS . 'index.fwstorage';
|
||||||
|
|
||||||
|
// If the index does not exist yet, load it
|
||||||
|
if (!file_exists($this->indexFile))
|
||||||
|
{
|
||||||
|
if (!$this->write_file($this->indexFile, serialize(['index' => []])))
|
||||||
|
throw new StorageException("Could not load FileProvider Storage. Could not write index.");
|
||||||
|
|
||||||
|
chmod($this->indexFile, 0640);
|
||||||
|
}
|
||||||
|
|
||||||
|
// And finally, load the index
|
||||||
|
$this->index = unserialize(file_get_contents($this->indexFile))['index'];
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getIndex(): array
|
||||||
|
{
|
||||||
|
$out = [];
|
||||||
|
foreach ($this->index as $key => $val)
|
||||||
|
$out[] = $key;
|
||||||
|
|
||||||
|
return $out;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getItem(string $key)
|
||||||
|
{
|
||||||
|
// Convert the key
|
||||||
|
$file = $this->directory . DS . crc32($key) . '.fwstorage';
|
||||||
|
|
||||||
|
// If the key could not be found in the index, return null
|
||||||
|
if (!isset($this->index[$key]))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
// Check if the file exists. If not, delete the indexed value
|
||||||
|
if (!file_exists($file))
|
||||||
|
{
|
||||||
|
$this->deleteItem($key);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise try and load the metaData and contents
|
||||||
|
return unserialize(file_get_contents($file));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getItemMeta(string $key): ?array
|
||||||
|
{
|
||||||
|
// If the key could not be found in the index, return null
|
||||||
|
if (!isset($this->index[$key]))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
// Otherwise return the meta data
|
||||||
|
return $this->index[$key]['meta'];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getItems(array $keys = []): array
|
||||||
|
{
|
||||||
|
$output = [];
|
||||||
|
foreach ($keys as $key)
|
||||||
|
$output[$key] = $this->getItem($key);
|
||||||
|
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hasItem(string $key): bool
|
||||||
|
{
|
||||||
|
return isset($this->index[$key]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function clear(): bool
|
||||||
|
{
|
||||||
|
$keys = array_keys($this->index);
|
||||||
|
return $this->deleteItems($keys);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function deleteItem(string $key): bool
|
||||||
|
{
|
||||||
|
// Convert the key
|
||||||
|
$file = $this->directory . DS . crc32($key) . '.fwstorage';
|
||||||
|
|
||||||
|
// Remove the file first
|
||||||
|
if (file_exists($file))
|
||||||
|
unlink($file);
|
||||||
|
|
||||||
|
// And remove it from the index
|
||||||
|
if (isset($this->index[$key]))
|
||||||
|
unset($this->index[$key]);
|
||||||
|
|
||||||
|
// And commit the index
|
||||||
|
return $this->commitIndex();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function deleteItems(array $keys): bool
|
||||||
|
{
|
||||||
|
foreach ($keys as $key)
|
||||||
|
$this->deleteItem($key);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function save(string $key, $value, array $metaData = []): bool
|
||||||
|
{
|
||||||
|
// Convert the key
|
||||||
|
$file = $this->directory . DS . crc32($key) . '.fwstorage';
|
||||||
|
|
||||||
|
// Remove the file if it already exists
|
||||||
|
if (file_exists($file))
|
||||||
|
unlink($file);
|
||||||
|
|
||||||
|
// Write everything to the index
|
||||||
|
$this->index[$key] = ['meta' => $metaData];
|
||||||
|
$this->commitIndex();
|
||||||
|
|
||||||
|
// And write the contents
|
||||||
|
if ($this->write_file($file, serialize($value))) {
|
||||||
|
chmod($file, 0640);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function commitIndex(): bool
|
||||||
|
{
|
||||||
|
if ($this->write_file($this->indexFile, serialize(['index' => $this->index]))) {
|
||||||
|
chmod($this->indexFile, 0640);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write File
|
||||||
|
*
|
||||||
|
* Writes data to the file specified in the path.
|
||||||
|
* Creates a new file if non-existent.
|
||||||
|
*
|
||||||
|
* @param string $path File path
|
||||||
|
* @param string $data Data to write
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function write_file(string $path, string $data): bool
|
||||||
|
{
|
||||||
|
if ( ! $fp = @fopen($path, 'wb'))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
flock($fp, LOCK_EX);
|
||||||
|
|
||||||
|
for ($result = $written = 0, $length = strlen($data); $written < $length; $written += $result)
|
||||||
|
if (($result = fwrite($fp, substr($data, $written))) === false)
|
||||||
|
break;
|
||||||
|
|
||||||
|
flock($fp, LOCK_UN);
|
||||||
|
fclose($fp);
|
||||||
|
|
||||||
|
return is_int($result);
|
||||||
|
}
|
||||||
|
}
|
188
src/FuzeWorks/Storage/Provider/RedisProvider.php
Normal file
188
src/FuzeWorks/Storage/Provider/RedisProvider.php
Normal file
@ -0,0 +1,188 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* FuzeWorks ObjectStorage Component.
|
||||||
|
*
|
||||||
|
* The FuzeWorks PHP FrameWork
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013-2020 i15
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
* @author i15
|
||||||
|
* @copyright Copyright (c) 2013 - 2020, i15. (https://i15.nl)
|
||||||
|
* @license https://opensource.org/licenses/MIT MIT License
|
||||||
|
*
|
||||||
|
* @since Version 1.3.0
|
||||||
|
*
|
||||||
|
* @version Version 1.3.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace FuzeWorks\Storage\Provider;
|
||||||
|
use FuzeWorks\Exception\StorageException;
|
||||||
|
use FuzeWorks\Storage\iStorageProvider;
|
||||||
|
use Redis;
|
||||||
|
use RedisException;
|
||||||
|
|
||||||
|
class RedisProvider implements iStorageProvider
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Redis
|
||||||
|
*/
|
||||||
|
protected Redis $conn;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the RedisProvider, by connecting to the Redis Server.
|
||||||
|
*
|
||||||
|
* $providerConfig can contain the following keys:
|
||||||
|
* 'socket_type', being either 'socket' or 'tcp'
|
||||||
|
* 'socket', when socket_type is socket, contains a handle for the socket
|
||||||
|
* 'host', when socket_type is tcp, contains the hostname or ip of the server
|
||||||
|
* 'port', when socket_type is tcp, contains the port of the server
|
||||||
|
* 'timeout', when socket_type is tcp, contains the amount of time to attempt connecting before failure
|
||||||
|
* 'db_index', the specific database number to use on the Redis Server
|
||||||
|
*
|
||||||
|
* Throws any StorageException on failure.
|
||||||
|
*
|
||||||
|
* @param array $providerConfig
|
||||||
|
* @return bool
|
||||||
|
* @throws StorageException
|
||||||
|
*/
|
||||||
|
public function init(array $providerConfig): bool
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$this->conn = new Redis();
|
||||||
|
|
||||||
|
// Afterwards we attempt to connect to the server
|
||||||
|
$socketType = $providerConfig['socket_type'];
|
||||||
|
if ($socketType === 'unix')
|
||||||
|
$success = $this->conn->connect($providerConfig['socket']);
|
||||||
|
elseif ($socketType === 'tcp')
|
||||||
|
$success = $this->conn->connect($providerConfig['host'], $providerConfig['port'], $providerConfig['timeout']);
|
||||||
|
else
|
||||||
|
$success = false;
|
||||||
|
|
||||||
|
// If failed, throw an exception informing so
|
||||||
|
if (!$success)
|
||||||
|
throw new StorageException("Could not load RedisProvider Storage. Unable to connect to server.");
|
||||||
|
|
||||||
|
// If authentication is required, attempt to do so with the provided password
|
||||||
|
if (isset($providerConfig['password']) && !$this->conn->auth($providerConfig['password']))
|
||||||
|
throw new StorageException("Could not load RedisProvider Storage. Authentication failure.");
|
||||||
|
|
||||||
|
// If a db_index is provided, use that one accordingly
|
||||||
|
if (isset($providerConfig['db_index']) && is_int($providerConfig['db_index']))
|
||||||
|
$this->conn->select($providerConfig['db_index']);
|
||||||
|
|
||||||
|
// And if all goes well, report a true
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// If any sort of failure has occurred along the way,
|
||||||
|
} catch (RedisException $e) {
|
||||||
|
throw new StorageException("Could not load RedisProvider Storage. RedisException thrown: '" . $e->getMessage() . "'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getIndex(): array
|
||||||
|
{
|
||||||
|
return $this->conn->sMembers('StorageIndex');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getItem(string $key)
|
||||||
|
{
|
||||||
|
// If the requested key is not part of the index, this item is not tracked and should therefore
|
||||||
|
// return null.
|
||||||
|
if (!$this->conn->sIsMember('StorageIndex', $key))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
// If the data doesn't exist, return null
|
||||||
|
if (!$this->conn->hExists('fwstorage_' . $key, 'data'))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return unserialize($this->conn->hGet('fwstorage_' . $key, 'data'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getItemMeta(string $key): ?array
|
||||||
|
{
|
||||||
|
// If the requested key is not part of the index, this item is not tracked and should therefore
|
||||||
|
// return null.
|
||||||
|
if (!$this->conn->sIsMember('StorageIndex', $key))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
// If the data doesn't exist, return null
|
||||||
|
if (!$this->conn->hExists('fwstorage_' . $key, 'meta'))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return unserialize($this->conn->hGet('fwstorage_' . $key, 'meta'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getItems(array $keys = []): array
|
||||||
|
{
|
||||||
|
$output = [];
|
||||||
|
foreach ($keys as $key)
|
||||||
|
$output[$key] = $this->getItem($key);
|
||||||
|
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hasItem(string $key): bool
|
||||||
|
{
|
||||||
|
return $this->conn->sIsMember('StorageIndex', $key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function clear(): bool
|
||||||
|
{
|
||||||
|
return $this->deleteItems($this->getIndex());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function deleteItem(string $key): bool
|
||||||
|
{
|
||||||
|
// If the requested key is not part of the index, this item is not tracked and should therefore
|
||||||
|
// return null.
|
||||||
|
if ($this->conn->sIsMember('StorageIndex', $key))
|
||||||
|
$this->conn->sRem('StorageIndex', $key);
|
||||||
|
|
||||||
|
if ($this->conn->exists('fwstorage_' . $key))
|
||||||
|
$this->conn->del('fwstorage_' . $key);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function deleteItems(array $keys): bool
|
||||||
|
{
|
||||||
|
foreach ($keys as $key)
|
||||||
|
$this->deleteItem($key);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function save(string $key, $value, array $metaData = []): bool
|
||||||
|
{
|
||||||
|
// If the requested key is not part of the index, this item is not tracked and should therefore
|
||||||
|
// return null.
|
||||||
|
if (!$this->conn->sIsMember('StorageIndex', $key))
|
||||||
|
$this->conn->sAdd('StorageIndex', $key);
|
||||||
|
|
||||||
|
// Write to the hash
|
||||||
|
$this->conn->hSet('fwstorage_' . $key, 'data', serialize($value));
|
||||||
|
$this->conn->hSet('fwstorage_' . $key, 'meta', serialize($metaData));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
128
src/FuzeWorks/Storage/StorageCache.php
Normal file
128
src/FuzeWorks/Storage/StorageCache.php
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* FuzeWorks ObjectStorage Component.
|
||||||
|
*
|
||||||
|
* The FuzeWorks PHP FrameWork
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013-2020 i15
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
* @author i15
|
||||||
|
* @copyright Copyright (c) 2013 - 2020, i15. (https://i15.nl)
|
||||||
|
* @license https://opensource.org/licenses/MIT MIT License
|
||||||
|
*
|
||||||
|
* @since Version 1.3.0
|
||||||
|
*
|
||||||
|
* @version Version 1.3.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace FuzeWorks\Storage;
|
||||||
|
use Psr\SimpleCache\CacheInterface;
|
||||||
|
|
||||||
|
class StorageCache implements CacheInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
private iStorageProvider $provider;
|
||||||
|
|
||||||
|
public function __construct(iStorageProvider $provider)
|
||||||
|
{
|
||||||
|
$this->provider = $provider;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function testTTL(string $key)
|
||||||
|
{
|
||||||
|
$meta = $this->provider->getItemMeta('fwcache_' . $key);
|
||||||
|
if (!is_null($meta) && $meta['ttl'] > 0 && time() > $meta['time'] + $meta['ttl'])
|
||||||
|
$this->provider->deleteItem('fwcache_' . $key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get($key, $default = null)
|
||||||
|
{
|
||||||
|
// Remove the item if its TTL has expired
|
||||||
|
$this->testTTL($key);
|
||||||
|
|
||||||
|
// Fetch the value
|
||||||
|
$res = $this->provider->getItem('fwcache_' . $key);
|
||||||
|
|
||||||
|
// If there is no value, return the default
|
||||||
|
return is_null($res) ? $default : $res;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function set($key, $value, $ttl = null): bool
|
||||||
|
{
|
||||||
|
$meta = [
|
||||||
|
'time' => time(),
|
||||||
|
'ttl' => is_int($ttl) ? $ttl : 0
|
||||||
|
];
|
||||||
|
|
||||||
|
return $this->provider->save('fwcache_' . $key, $value, $meta);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function delete($key): bool
|
||||||
|
{
|
||||||
|
return $this->provider->deleteItem('fwcache_' . $key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function clear(): bool
|
||||||
|
{
|
||||||
|
// Fetch the index set
|
||||||
|
$index = $this->provider->getIndex();
|
||||||
|
foreach ($index as $entry)
|
||||||
|
{
|
||||||
|
if (substr($entry, 0, 8) === 'fwcache_')
|
||||||
|
$this->provider->deleteItem($entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getMultiple($keys, $default = null): array
|
||||||
|
{
|
||||||
|
$out = [];
|
||||||
|
foreach ($keys as $key)
|
||||||
|
{
|
||||||
|
$out[$key] = $this->get($key, $default);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $out;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setMultiple($values, $ttl = null): bool
|
||||||
|
{
|
||||||
|
foreach ($values as $key => $value)
|
||||||
|
$this->set($key, $value, $ttl);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function deleteMultiple($keys): bool
|
||||||
|
{
|
||||||
|
foreach ($keys as $key)
|
||||||
|
$this->delete($key);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function has($key): bool
|
||||||
|
{
|
||||||
|
$this->testTTL($key);
|
||||||
|
return $this->provider->hasItem('fwcache_' . $key);
|
||||||
|
}
|
||||||
|
}
|
52
src/FuzeWorks/Storage/iStorageProvider.php
Normal file
52
src/FuzeWorks/Storage/iStorageProvider.php
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* FuzeWorks ObjectStorage Component.
|
||||||
|
*
|
||||||
|
* The FuzeWorks PHP FrameWork
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013-2020 i15
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
* @author i15
|
||||||
|
* @copyright Copyright (c) 2013 - 2020, i15. (https://i15.nl)
|
||||||
|
* @license https://opensource.org/licenses/MIT MIT License
|
||||||
|
*
|
||||||
|
* @since Version 1.3.0
|
||||||
|
*
|
||||||
|
* @version Version 1.3.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace FuzeWorks\Storage;
|
||||||
|
|
||||||
|
interface iStorageProvider
|
||||||
|
{
|
||||||
|
|
||||||
|
public function init(array $providerConfig): bool;
|
||||||
|
public function getIndex(): array;
|
||||||
|
public function getItem(string $key);
|
||||||
|
public function getItemMeta(string $key): ?array;
|
||||||
|
public function getItems(array $keys = []): array;
|
||||||
|
public function hasItem(string $key): bool;
|
||||||
|
public function clear(): bool;
|
||||||
|
public function deleteItem(string $key): bool;
|
||||||
|
public function deleteItems(array $keys): bool;
|
||||||
|
public function save(string $key, $value, array $metaData = []): bool;
|
||||||
|
|
||||||
|
}
|
@ -214,6 +214,7 @@ class factoryTest extends CoreTestAbstract
|
|||||||
$this->assertSame($factory->events, $factory2->events);
|
$this->assertSame($factory->events, $factory2->events);
|
||||||
$this->assertSame($factory->libraries, $factory2->libraries);
|
$this->assertSame($factory->libraries, $factory2->libraries);
|
||||||
$this->assertSame($factory->helpers, $factory2->helpers);
|
$this->assertSame($factory->helpers, $factory2->helpers);
|
||||||
|
$this->assertSame($factory->storage, $factory2->storage);
|
||||||
|
|
||||||
// And test when changing one classInstance
|
// And test when changing one classInstance
|
||||||
$factory->newInstance('Helpers');
|
$factory->newInstance('Helpers');
|
||||||
|
@ -12,8 +12,12 @@
|
|||||||
</exclude>
|
</exclude>
|
||||||
</coverage>
|
</coverage>
|
||||||
<testsuites>
|
<testsuites>
|
||||||
<testsuite name="Core Suite">
|
<testsuite name="core">
|
||||||
<directory>./</directory>
|
<directory>./core</directory>
|
||||||
|
<directory>./events</directory>
|
||||||
|
</testsuite>
|
||||||
|
<testsuite name="storage">
|
||||||
|
<directory>./storage</directory>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
</testsuites>
|
</testsuites>
|
||||||
</phpunit>
|
</phpunit>
|
200
test/storage/StorageCacheTest.php
Normal file
200
test/storage/StorageCacheTest.php
Normal file
@ -0,0 +1,200 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* FuzeWorks ObjectStorage Component.
|
||||||
|
*
|
||||||
|
* The FuzeWorks PHP FrameWork
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013-2020 i15
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
* @author i15
|
||||||
|
* @copyright Copyright (c) 2013 - 2020, i15. (https://i15.nl)
|
||||||
|
* @license https://opensource.org/licenses/MIT MIT License
|
||||||
|
*
|
||||||
|
* @since Version 1.3.0
|
||||||
|
*
|
||||||
|
* @version Version 1.3.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace storage;
|
||||||
|
|
||||||
|
use FuzeWorks\Factory;
|
||||||
|
use FuzeWorks\Storage;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Psr\SimpleCache\CacheInterface;
|
||||||
|
|
||||||
|
class StorageCacheTest extends TestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
private CacheInterface $cache;
|
||||||
|
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
$this->loadProvider();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function tearDown(): void
|
||||||
|
{
|
||||||
|
parent::tearDown();
|
||||||
|
|
||||||
|
// Always clear the cache after every test
|
||||||
|
$this->cache->clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function loadProvider()
|
||||||
|
{
|
||||||
|
/** @var Storage $objectStorageComponent */
|
||||||
|
$objectStorageComponent = Factory::getInstance('storage');
|
||||||
|
$this->cache = $objectStorageComponent->getCache();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFoundation()
|
||||||
|
{
|
||||||
|
$this->assertInstanceOf('\Psr\SimpleCache\CacheInterface', $this->cache);
|
||||||
|
$this->assertInstanceOf('\FuzeWorks\Storage\StorageCache', $this->cache);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testFoundation
|
||||||
|
*/
|
||||||
|
public function testSetGetAndHas()
|
||||||
|
{
|
||||||
|
$testData = ['hello', 'world'];
|
||||||
|
|
||||||
|
// First check the data isn't there
|
||||||
|
$this->assertFalse($this->cache->has('testData'));
|
||||||
|
|
||||||
|
// Then write it
|
||||||
|
$this->assertTrue($this->cache->set('testData', $testData));
|
||||||
|
|
||||||
|
// Assert it is there and check its contents
|
||||||
|
$this->assertTrue($this->cache->has('testData'));
|
||||||
|
$this->assertEquals(['hello', 'world'], $this->cache->get('testData'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testSetGetAndHas
|
||||||
|
*/
|
||||||
|
public function testGetDefaultValue()
|
||||||
|
{
|
||||||
|
// Verify that no value exists
|
||||||
|
$this->assertFalse($this->cache->has('testData'));
|
||||||
|
$this->assertNull($this->cache->get('testData'));
|
||||||
|
|
||||||
|
// And check if the default value is returned
|
||||||
|
$this->assertEquals('default!', $this->cache->get('testData', 'default!'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testSetGetAndHas
|
||||||
|
*/
|
||||||
|
public function testDeleteValue()
|
||||||
|
{
|
||||||
|
// Verify that none exist
|
||||||
|
$this->assertFalse($this->cache->has('testData'));
|
||||||
|
|
||||||
|
// Write some data
|
||||||
|
$this->assertTrue($this->cache->set('testData', 'someValue'));
|
||||||
|
$this->assertEquals('someValue', $this->cache->get('testData'));
|
||||||
|
$this->assertTrue($this->cache->has('testData'));
|
||||||
|
|
||||||
|
// Delete it
|
||||||
|
$this->assertTrue($this->cache->delete('testData'));
|
||||||
|
$this->assertFalse($this->cache->has('testData'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testDeleteValue
|
||||||
|
*/
|
||||||
|
public function testClear()
|
||||||
|
{
|
||||||
|
// Write some data
|
||||||
|
$this->assertTrue($this->cache->set('testData1', 'value1'));
|
||||||
|
$this->assertTrue($this->cache->set('testData2', 'value2'));
|
||||||
|
|
||||||
|
// Then clear it off
|
||||||
|
$this->assertTrue($this->cache->clear());
|
||||||
|
$this->assertFalse($this->cache->has('testData1'));
|
||||||
|
$this->assertFalse($this->cache->has('testData2'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testDeleteValue
|
||||||
|
*/
|
||||||
|
public function testMultiple()
|
||||||
|
{
|
||||||
|
// First check that none of the variables exist
|
||||||
|
$this->assertFalse($this->cache->has('testData1'));
|
||||||
|
$this->assertFalse($this->cache->has('testData2'));
|
||||||
|
$this->assertFalse($this->cache->has('testData3'));
|
||||||
|
|
||||||
|
// With a get multiple, and default
|
||||||
|
$this->assertEquals([
|
||||||
|
'testData1' => 'default',
|
||||||
|
'testData2' => 'default',
|
||||||
|
'testData3' => 'default'
|
||||||
|
], $this->cache->getMultiple(['testData1', 'testData2', 'testData3'], 'default'));
|
||||||
|
|
||||||
|
// Write multiple
|
||||||
|
$this->assertTrue($this->cache->setMultiple([
|
||||||
|
'testData1' => 'value1',
|
||||||
|
'testData2' => 'value2',
|
||||||
|
'testData3' => 'value3'
|
||||||
|
]));
|
||||||
|
|
||||||
|
// Test the contents
|
||||||
|
$this->assertEquals([
|
||||||
|
'testData1' => 'value1',
|
||||||
|
'testData2' => 'value2',
|
||||||
|
'testData3' => 'value3'
|
||||||
|
], $this->cache->getMultiple(['testData1', 'testData2', 'testData3'], 'default'));
|
||||||
|
|
||||||
|
// And also delete them all
|
||||||
|
$this->assertTrue($this->cache->deleteMultiple(['testData1', 'testData2', 'testData3']));
|
||||||
|
$this->assertFalse($this->cache->has('testData1'));
|
||||||
|
$this->assertFalse($this->cache->has('testData2'));
|
||||||
|
$this->assertFalse($this->cache->has('testData3'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testSetGetAndHas
|
||||||
|
*/
|
||||||
|
public function testTTL()
|
||||||
|
{
|
||||||
|
$testData = ['hello', 'world'];
|
||||||
|
|
||||||
|
// First check the data isn't there
|
||||||
|
$this->assertFalse($this->cache->has('testData'));
|
||||||
|
|
||||||
|
// Then write it
|
||||||
|
$this->assertTrue($this->cache->set('testData', $testData, 1));
|
||||||
|
|
||||||
|
// Assert it is there and check its contents
|
||||||
|
$this->assertTrue($this->cache->has('testData'));
|
||||||
|
$this->assertEquals(['hello', 'world'], $this->cache->get('testData'));
|
||||||
|
|
||||||
|
// Then wait 2 secs
|
||||||
|
sleep(2);
|
||||||
|
|
||||||
|
// And check again
|
||||||
|
$this->assertFalse($this->cache->has('testData'));
|
||||||
|
}
|
||||||
|
}
|
218
test/storage/StorageProviderTest.php
Normal file
218
test/storage/StorageProviderTest.php
Normal file
@ -0,0 +1,218 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* FuzeWorks ObjectStorage Component.
|
||||||
|
*
|
||||||
|
* The FuzeWorks PHP FrameWork
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013-2020 i15
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
* @author i15
|
||||||
|
* @copyright Copyright (c) 2013 - 2020, i15. (https://i15.nl)
|
||||||
|
* @license https://opensource.org/licenses/MIT MIT License
|
||||||
|
*
|
||||||
|
* @since Version 1.3.0
|
||||||
|
*
|
||||||
|
* @version Version 1.3.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace storage;
|
||||||
|
|
||||||
|
use FuzeWorks\Factory;
|
||||||
|
use FuzeWorks\Storage\iStorageProvider;
|
||||||
|
use FuzeWorks\Storage;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ObjectStorageProviderTest
|
||||||
|
*
|
||||||
|
* @todo getIndex() method
|
||||||
|
*/
|
||||||
|
class StorageProviderTest extends TestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
private iStorageProvider $provider;
|
||||||
|
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
$this->loadProvider();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function tearDown(): void
|
||||||
|
{
|
||||||
|
parent::tearDown();
|
||||||
|
|
||||||
|
// Always clear the provider at the end
|
||||||
|
$this->provider->clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function loadProvider()
|
||||||
|
{
|
||||||
|
/** @var Storage $objectStorageComponent */
|
||||||
|
$objectStorageComponent = Factory::getInstance('storage');
|
||||||
|
$this->provider = $objectStorageComponent->getStorage();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFoundation()
|
||||||
|
{
|
||||||
|
$this->assertInstanceOf('\FuzeWorks\Storage\iStorageProvider', $this->provider);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testFoundation
|
||||||
|
*/
|
||||||
|
public function testSave()
|
||||||
|
{
|
||||||
|
$testData = ['hello', 'world', 'foo' => 'bar'];
|
||||||
|
|
||||||
|
// First assert it does not exist yet
|
||||||
|
$this->assertNull($this->provider->getItem('testData'));
|
||||||
|
$this->assertFalse($this->provider->hasItem('testData'));
|
||||||
|
|
||||||
|
// Write the data
|
||||||
|
$this->assertTrue($this->provider->save('testData', $testData));
|
||||||
|
|
||||||
|
// Read the data
|
||||||
|
$this->assertTrue($this->provider->hasItem('testData'));
|
||||||
|
$this->assertEquals(['hello', 'world', 'foo' => 'bar'], $this->provider->getItem('testData'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testSave
|
||||||
|
*/
|
||||||
|
public function testDeleteItem()
|
||||||
|
{
|
||||||
|
$testData = ['o', 'm', 'g'];
|
||||||
|
|
||||||
|
// First assert that the data does not exist yet
|
||||||
|
$this->assertNull($this->provider->getItem('testDeleteData'));
|
||||||
|
|
||||||
|
// Write the data
|
||||||
|
$this->assertTrue($this->provider->save('testDeleteData', $testData));
|
||||||
|
|
||||||
|
// Read the data
|
||||||
|
$this->assertEquals(['o', 'm', 'g'], $this->provider->getItem('testDeleteData'));
|
||||||
|
|
||||||
|
// Delete the data
|
||||||
|
$this->assertTrue($this->provider->deleteItem('testDeleteData'));
|
||||||
|
|
||||||
|
// And test that it is truly gone
|
||||||
|
$this->assertNull($this->provider->getItem('testDeleteData'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testDeleteItem
|
||||||
|
*/
|
||||||
|
public function testDeleteItems()
|
||||||
|
{
|
||||||
|
$testData = 'lord';
|
||||||
|
$testData2 = 'almighty';
|
||||||
|
|
||||||
|
// First assert that the data does not exist yet
|
||||||
|
$this->assertNull($this->provider->getItem('testDeleteData1'));
|
||||||
|
$this->assertNull($this->provider->getItem('testDeleteData2'));
|
||||||
|
|
||||||
|
// Write the data
|
||||||
|
$this->assertTrue($this->provider->save('testDeleteData1', $testData));
|
||||||
|
$this->assertTrue($this->provider->save('testDeleteData2', $testData2));
|
||||||
|
|
||||||
|
// Read the data
|
||||||
|
$this->assertEquals(
|
||||||
|
['testDeleteData1' => 'lord', 'testDeleteData2' => 'almighty'],
|
||||||
|
$this->provider->getItems(['testDeleteData1', 'testDeleteData2'])
|
||||||
|
);
|
||||||
|
|
||||||
|
// Delete the data
|
||||||
|
$this->assertTrue($this->provider->deleteItems(['testDeleteData1', 'testDeleteData2']));
|
||||||
|
|
||||||
|
// And test that it is truly gone
|
||||||
|
$this->assertNull($this->provider->getItem('testDeleteData1'));
|
||||||
|
$this->assertNull($this->provider->getItem('testDeleteData2'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testDeleteItems
|
||||||
|
*/
|
||||||
|
public function testClear()
|
||||||
|
{
|
||||||
|
$testData = ['not', 'my', 'department'];
|
||||||
|
|
||||||
|
// First assert it does not exist yet
|
||||||
|
$this->assertNull($this->provider->getItem('testClearData'));
|
||||||
|
|
||||||
|
$this->provider->save('testClearData', $testData);
|
||||||
|
|
||||||
|
// Test that it can be read
|
||||||
|
$this->assertEquals(['not', 'my', 'department'], $this->provider->getItem('testClearData'));
|
||||||
|
|
||||||
|
// Then attempt to clean it
|
||||||
|
$this->assertTrue($this->provider->clear());
|
||||||
|
|
||||||
|
// And check that it cannot be read
|
||||||
|
$this->assertNull($this->provider->getItem('testClearData'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItemNotExist()
|
||||||
|
{
|
||||||
|
$this->assertNull($this->provider->getItem('doesNotExist'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetMultipleItems()
|
||||||
|
{
|
||||||
|
$testData1 = ['tao', 'te', 'ching'];
|
||||||
|
$testData2 = ['plato', 'aristotle'];
|
||||||
|
|
||||||
|
// First assert they do not exist
|
||||||
|
$this->assertNull($this->provider->getItem('philo1'));
|
||||||
|
$this->assertNull($this->provider->getItem('philo2'));
|
||||||
|
$this->assertEquals(['philo1' => null, 'philo2' => null], $this->provider->getItems(['philo1', 'philo2']));
|
||||||
|
|
||||||
|
// Then write both
|
||||||
|
$this->assertTrue($this->provider->save('philo1', $testData1));
|
||||||
|
$this->assertTrue($this->provider->save('philo2', $testData2));
|
||||||
|
|
||||||
|
// Then read
|
||||||
|
$this->assertEquals([
|
||||||
|
'philo1' => ['tao', 'te', 'ching'],
|
||||||
|
'philo2' => ['plato', 'aristotle']
|
||||||
|
], $this->provider->getItems(['philo1', 'philo2']));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItemMetaData()
|
||||||
|
{
|
||||||
|
$testData = ['meine', 'gute'];
|
||||||
|
$metaData = ['someKey' => 'someValue'];
|
||||||
|
|
||||||
|
// First assert that the data does not exist
|
||||||
|
$this->assertNull($this->provider->getItem('testData'));
|
||||||
|
$this->assertNull($this->provider->getItemMeta('testData'));
|
||||||
|
|
||||||
|
// Then save the data
|
||||||
|
$this->assertTrue($this->provider->save('testData', $testData, $metaData));
|
||||||
|
|
||||||
|
// Check the metaData
|
||||||
|
$this->assertEquals(['someKey' => 'someValue'], $this->provider->getItemMeta('testData'));
|
||||||
|
|
||||||
|
// Remove the key
|
||||||
|
$this->provider->deleteItem('testData');
|
||||||
|
$this->assertNull($this->provider->getItemMeta('testData'));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user