1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-08 06:06:45 +02:00

Using Registry Component instead of Config class

This commit is contained in:
Awilum
2018-05-05 03:10:47 +03:00
parent e8408866a2
commit 4fb354710e
3 changed files with 14 additions and 140 deletions

View File

@@ -12,7 +12,7 @@
namespace Flextype;
use Flextype\Component\Filesystem\Filesystem;
use Flextype\Component\{Filesystem\Filesystem, Registry\Registry};
use \Doctrine\Common\Cache as DoctrineCache;
class Cache
@@ -84,7 +84,7 @@ class Cache
static::$now = time();
// Create cache key to allow invalidate all cache on configuration changes.
static::$key = (Config::get('site.cache.prefix') ?? 'flextype') . '-' . md5(ROOT_DIR . Flextype::VERSION);
static::$key = (Registry::get('site.cache.prefix') ?? 'flextype') . '-' . md5(ROOT_DIR . Flextype::VERSION);
// Get Cache Driver
static::$driver = static::getCacheDriver();
@@ -101,7 +101,7 @@ class Cache
*/
public static function getCacheDriver()
{
$driver_name = Config::get('site.cache.driver');
$driver_name = Registry::get('site.cache.driver');
if (!$driver_name || $driver_name == 'auto') {
if (extension_loaded('apcu')) {
@@ -132,28 +132,28 @@ class Cache
break;
case 'memcache':
$memcache = new \Memcache();
$memcache->connect(Config::get('site.cache.memcache.server', 'localhost'),
Config::get('site.cache.memcache.port', 11211));
$memcache->connect(Registry::get('site.cache.memcache.server', 'localhost'),
Registry::get('site.cache.memcache.port', 11211));
$driver = new DoctrineCache\MemcacheCache();
$driver->setMemcache($memcache);
break;
case 'memcached':
$memcached = new \Memcached();
$memcached->addServer(Config::get('site.cache.memcached.server', 'localhost'),
Config::get('site.cache.memcache.port', 11211));
$memcached->addServer(Registry::get('site.cache.memcached.server', 'localhost'),
Registry::get('site.cache.memcache.port', 11211));
$driver = new DoctrineCache\MemcachedCache();
$driver->setMemcached($memcached);
break;
case 'redis':
$redis = new \Redis();
$socket = Config::get('site.cache.redis.socket', false);
$password = Config::get('site.cache.redis.password', false);
$socket = Registry::get('site.cache.redis.socket', false);
$password = Registry::get('site.cache.redis.password', false);
if ($socket) {
$redis->connect($socket);
} else {
$redis->connect(Config::get('site.cache.redis.server', 'localhost'),
Config::get('site.cache.redis.port', 6379));
$redis->connect(Registry::get('site.cache.redis.server', 'localhost'),
Registry::get('site.cache.redis.port', 6379));
}
// Authenticate with password if set
@@ -204,7 +204,7 @@ class Cache
*/
public function fetch(string $id)
{
if (Config::get('site.cache.enabled')) {
if (Registry::get('site.cache.enabled')) {
return static::$driver->fetch($id);
} else {
return false;
@@ -223,7 +223,7 @@ class Cache
*/
public function save(string $id, $data, $lifetime = null)
{
if (Config::get('site.cache.enabled')) {
if (Registry::get('site.cache.enabled')) {
if ($lifetime === null) {
$lifetime = static::getLifetime();
}
@@ -274,7 +274,7 @@ class Cache
public static function getLifetime()
{
if (static::$lifetime === null) {
static::$lifetime = Config::get('site.cache.lifetime') ?: 604800;
static::$lifetime = Registry::get('site.cache.lifetime') ?: 604800;
}
return static::$lifetime;

View File

@@ -1,119 +0,0 @@
<?php
/**
* @package Flextype
*
* @author Sergey Romanenko <awilum@yandex.ru>
* @link http://flextype.org
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flextype;
use Flextype\Component\{Arr\Arr, Filesystem\Filesystem};
use Symfony\Component\Yaml\Yaml;
class Config
{
/**
* An instance of the Config class
*
* @var object
* @access protected
*/
protected static $instance = null;
/**
* Config
*
* @var array
* @access protected
*/
protected static $config = [];
/**
* Protected clone method to enforce singleton behavior.
*
* @access protected
*/
protected function __clone()
{
// Nothing here.
}
/**
* Protected constructor since this is a static class.
*
* @access protected
*/
protected function __construct()
{
static::init();
}
/**
* Init Config
*
* @access protected
* @return void
*/
protected static function init() : void
{
if (Filesystem::fileExists($site_config = CONFIG_PATH . '/' . 'site.yml')) {
static::$config['site'] = Yaml::parseFile($site_config);
} else {
throw new RuntimeException("Flextype site config file does not exist.");
}
}
/**
* Set new or update existing config variable
*
* @access public
* @param string $key Key
* @param mixed $value Value
*/
public static function set($key, $value) : void
{
Arr::set(static::$config, $key, $value);
}
/**
* Get config variable
*
* @access public
* @param string $key Key
* @param mixed $default Default value
* @return mixed
*/
public static function get($key, $default = null)
{
return Arr::get(static::$config, $key, $default);
}
/**
* Get config array
*
* @access public
* @return array
*/
public static function getConfig() : array
{
return static::$config;
}
/**
* Return the Config instance.
* Create it if it's not already created.
*
* @access public
* @return object
*/
public static function instance()
{
return !isset(self::$instance) and self::$instance = new Config();
}
}

View File

@@ -1,7 +0,0 @@
<?php
namespace Flextype;
use Flextype\Component\{Event\Event, Http\Http};
?>
<script src="<?php echo Http::getBaseUrl(); ?>/site/themes/<?php echo Config::get('site.theme'); ?>/node_modules/jquery/dist/jquery.slim.min.js"></script>
<script src="<?php echo Http::getBaseUrl(); ?>/site/themes/<?php echo Config::get('site.theme'); ?>/node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
<?php Event::dispatch('onThemeFooter'); ?>