160 lines
5.2 KiB
PHP
160 lines
5.2 KiB
PHP
<?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\ObjectStorage;
|
|
use FuzeWorks\Config;
|
|
use FuzeWorks\Configurator;
|
|
use FuzeWorks\Exception\ConfigException;
|
|
use FuzeWorks\Exception\FactoryException;
|
|
use FuzeWorks\Factory;
|
|
use FuzeWorks\iComponent;
|
|
use FuzeWorks\ObjectStorage\Exception\ObjectStorageException;
|
|
use Psr\SimpleCache\CacheInterface;
|
|
|
|
/**
|
|
* ObjectStorageComponent 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 ObjectStorageComponent implements iComponent
|
|
{
|
|
|
|
/**
|
|
* The configuration file for ObjectStorage
|
|
*
|
|
* @var array
|
|
*/
|
|
private array $cfg;
|
|
|
|
/**
|
|
* The currently selected ObjectStorageProvider
|
|
*
|
|
* @var iObjectStorageProvider
|
|
*/
|
|
private iObjectStorageProvider $provider;
|
|
|
|
/**
|
|
* The currently used CacheInterface
|
|
*
|
|
* @var CacheInterface
|
|
*/
|
|
private CacheInterface $cache;
|
|
|
|
public function getName(): string
|
|
{
|
|
return 'ObjectStorageComponent';
|
|
}
|
|
|
|
public function getClasses(): array
|
|
{
|
|
return ['storage' => $this];
|
|
}
|
|
|
|
/**
|
|
* Fetches and returns the currently selected ObjectStorageProvider
|
|
*
|
|
* @throws ObjectStorageException
|
|
* @return iObjectStorageProvider
|
|
*/
|
|
public function getStorage(): iObjectStorageProvider
|
|
{
|
|
// 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('objectstorage')->toArray();
|
|
} catch (ConfigException | FactoryException $e) {
|
|
throw new ObjectStorageException("Could not get ObjectStorageProvider. No config file named 'config.objectstorage.php' could be found.");
|
|
}
|
|
}
|
|
|
|
// Get the currently selected ObjectStorageProvider
|
|
$selected = $this->cfg['ObjectStorageProvider'];
|
|
if (is_null($selected))
|
|
throw new ObjectStorageException("Could not get ObjectStorageProvider. Selected provider is null!");
|
|
|
|
// Try and load the ObjectStorageProvider
|
|
$class = '\FuzeWorks\ObjectStorage\Provider\\' . $selected;
|
|
if (!class_exists($class, true))
|
|
throw new ObjectStorageException("Could not get ObjectStorageProvider. Selected provider '".$selected."' is not recognized.");
|
|
|
|
/** @var iObjectStorageProvider $provider */
|
|
$provider = new $class();
|
|
if (!$provider instanceof iObjectStorageProvider)
|
|
throw new ObjectStorageException("Could not get ObjectStorageProvider. Selected provider '".$selected."' is not an instance of iObjectStorageProvider'.");
|
|
|
|
// Fetch the parameters
|
|
$params = isset($this->cfg[$selected]) && is_array($this->cfg[$selected]) ? $this->cfg[$selected] : [];
|
|
if (!$provider->init($params))
|
|
throw new ObjectStorageException("Could not get ObjectStorageProvider. Selected provider '".$selected."' failed to load.");
|
|
|
|
$this->provider = $provider;
|
|
return $this->provider;
|
|
}
|
|
|
|
/**
|
|
* Returns a PSR compatible Cache object
|
|
*
|
|
* @return CacheInterface
|
|
* @throws ObjectStorageException
|
|
*/
|
|
public function getCache(): CacheInterface
|
|
{
|
|
if (isset($this->cache))
|
|
return $this->cache;
|
|
|
|
$storageProvider = $this->getStorage();
|
|
$this->cache = new ObjectStorageCache($storageProvider);
|
|
|
|
return $this->cache;
|
|
}
|
|
|
|
public function onAddComponent(Configurator $configurator)
|
|
{
|
|
}
|
|
|
|
public function onCreateContainer(Factory $container)
|
|
{
|
|
}
|
|
} |