File io/data/store/DataStoreFactory.class.php
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94:
<?php
/**
* This factory returns you the data store that are the best for your requirements.
* @package IO
* @subpackage Data\store
* @copyright © 2005-2019 PHPBoost
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
* @author Benoit SAUTEL <ben.popeye@phpboost.com>
* @version PHPBoost 5.2 - last update: 2015 06 29
* @since PHPBoost 3.0 - 2009 12 09
* @contributor Loic ROUCHON <horn@phpboost.com>
* @contributor Julien BRISWALTER <j1.seth@phpboost.com>
*/
class DataStoreFactory
{
private static $apc_available = null;
private static $apc_enabled = null;
/**
* Returns an efficient data store whose life span can be not infinite.
* @param string $id Identifier of the data store.
* @return DataStore The best data store you can use with the current configuration
*/
public static function get_ram_store($id)
{
if (self::is_apc_available() && self::is_apc_enabled())
{
return new APCDataStore($id);
}
return new RAMDataStore();
}
/**
* Returns an infinite-life span data store that can be not very efficient.
* @param string $id Identifier of the data store.
* @return DataStore The best data store you can use with the current configuration
*/
public static function get_filesystem_store($id)
{
if (self::is_apc_available() && self::is_apc_enabled())
{
return new APCDataStore($id);
}
return new FileSystemDataStore($id);
}
public static function is_apc_available()
{
if (self::$apc_available === null)
{
if (function_exists('apc_cache_info') && (extension_loaded('apc') || extension_loaded('apcu')) && ini_get('apc.enabled'))
{
self::$apc_available = true;
}
else
{
self::$apc_available = false;
}
}
return self::$apc_available;
}
public static function is_apc_enabled()
{
if (self::$apc_available !== null && self::$apc_enabled === null)
{
$file = new File(PATH_TO_ROOT . '/cache/apc.php');
if ($file->exists())
{
include $file->get_path();
if (isset($enable_apc))
{
return $enable_apc;
}
}
}
return false;
}
/**
* Enables or disables APC. Writes in the file /cache/apc.php
* @param bool $enabled
* @throws IOException If the file cannot be written
*/
public static function set_apc_enabled($enabled)
{
$file = new File(PATH_TO_ROOT . '/cache/apc.php');
$file->write('<?php $enable_apc = ' . ($enabled ? 'true' : 'false') . ';');
$file->close();
}
}
?>