1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-12 16:14:16 +02:00

Flextype Slim Integration - next round of integration

This commit is contained in:
Awilum
2019-03-01 16:38:33 +03:00
parent c65f5fb41b
commit aca40d94e4
7 changed files with 150 additions and 113 deletions

View File

@@ -18,6 +18,11 @@ use \Doctrine\Common\Cache as DoctrineCache;
class Cache
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* Unique cache key
*
@@ -51,8 +56,10 @@ class Cache
*
* @access private
*/
public function __construct()
public function __construct($flextype)
{
$this->flextype = $flextype;
// Create Cache Directory
!Filesystem::has(PATH['cache']) and Filesystem::createDir(PATH['cache']);
@@ -60,7 +67,7 @@ class Cache
$this->now = time();
// Create cache key to allow invalidate all cache on configuration changes.
$this->key = (Registry::get('settings.cache.prefix') ?? 'flextype') . '-' . md5(PATH['site'] . 'Flextype::VERSION');
$this->key = ($this->flextype['registry']->get('settings.cache.prefix') ?? 'flextype') . '-' . md5(PATH['site'] . 'Flextype::VERSION');
// Get Cache Driver
$this->driver = $this->getCacheDriver();
@@ -79,7 +86,7 @@ class Cache
public function getCacheDriver()
{
// Try to set default cache driver name
$driver_name = $this->setDefaultCacheDriverName(Registry::get('settings.cache.driver'));
$driver_name = $this->setDefaultCacheDriverName($this->flextype['registry']->get('settings.cache.driver'));
// Set cache driver
return $this->setCacheDriver($driver_name);
@@ -143,8 +150,8 @@ class Cache
// Create doctrine cache directory if its not exists
!Filesystem::has($cache_directory) and Filesystem::createDir($cache_directory);
$db = new \SQLite3($cache_directory . Registry::get('settings.cache.sqlite3.database', 'flextype') . '.db');
$driver = new DoctrineCache\SQLite3Cache($db, Registry::get('settings.cache.sqlite3.table', 'flextype'));
$db = new \SQLite3($cache_directory . $this->flextype['registry']->get('settings.cache.sqlite3.database', 'flextype') . '.db');
$driver = new DoctrineCache\SQLite3Cache($db, $this->flextype['registry']->get('settings.cache.sqlite3.table', 'flextype'));
return $driver;
}
@@ -158,8 +165,8 @@ class Cache
{
$memcached = new \Memcached();
$memcached->addServer(
Registry::get('settings.cache.memcached.server', 'localhost'),
Registry::get('settings.cache.memcache.port', 11211)
$this->flextype['registry']->get('settings.cache.memcached.server', 'localhost'),
$this->flextype['registry']->get('settings.cache.memcache.port', 11211)
);
$driver = new DoctrineCache\MemcachedCache();
$driver->setMemcached($memcached);
@@ -215,15 +222,15 @@ class Cache
protected function setRedisCacheDriver()
{
$redis = new \Redis();
$socket = Registry::get('settings.cache.redis.socket', false);
$password = Registry::get('settings.cache.redis.password', false);
$socket = $this->flextype['registry']->get('settings.cache.redis.socket', false);
$password = $this->flextype['registry']->get('settings.cache.redis.password', false);
if ($socket) {
$redis->connect($socket);
} else {
$redis->connect(
Registry::get('settings.cache.redis.server', 'localhost'),
Registry::get('settings.cache.redis.port', 6379)
$this->flextype['registry']->get('settings.cache.redis.server', 'localhost'),
$this->flextype['registry']->get('settings.cache.redis.port', 6379)
);
}
@@ -308,7 +315,7 @@ class Cache
*/
public function fetch(string $id)
{
if (Registry::get('settings.cache.enabled')) {
if ($this->flextype['registry']->get('settings.cache.enabled')) {
return $this->$driver->fetch($id);
} else {
return false;
@@ -323,7 +330,7 @@ class Cache
*/
public function contains($id)
{
if (Registry::get('settings.cache.enabled')) {
if ($this->flextype['registry']->get('settings.cache.enabled')) {
return $this->$driver->contains(($id));
} else {
return false;
@@ -342,7 +349,7 @@ class Cache
*/
public function save(string $id, $data, $lifetime = null)
{
if (Registry::get('settings.cache.enabled')) {
if ($this->flextype['registry']->get('settings.cache.enabled')) {
if ($lifetime === null) {
$lifetime = $this->getLifetime();
}
@@ -394,7 +401,7 @@ class Cache
public function getLifetime()
{
if ($this->$lifetime === null) {
$this->$lifetime = Registry::get('settings.cache.lifetime') ?: 604800;
$this->$lifetime = $this->flextype['registry']->get('settings.cache.lifetime') ?: 604800;
}
return $this->$lifetime;

View File

@@ -18,7 +18,6 @@ use Flextype\Component\Registry\Registry;
class Entries
{
/**
* Flextype Dependency Container
*/
@@ -67,7 +66,7 @@ class Entries
if ($entry_decoded = YamlParser::decode($entry_body)) {
// Create default entry items
$entry_decoded['date'] = $entry_decoded['date'] ?? date(Registry::get('settings.date_format'), Filesystem::getTimestamp($entry_file));
$entry_decoded['date'] = $entry_decoded['date'] ?? date($this->flextype['registry']->get('settings.date_format'), Filesystem::getTimestamp($entry_file));
$entry_decoded['slug'] = $entry_decoded['slug'] ?? ltrim(rtrim($entry, '/'), '/');
// Save to cache

View File

@@ -17,6 +17,21 @@ use Flextype\Component\Registry\Registry;
class Fieldsets
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* __construct
*
* @access public
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Fetch Fieldsets for current theme
*
@@ -141,7 +156,7 @@ class Fieldsets
*/
private function _dir_location() : string
{
return PATH['themes'] . '/' . Registry::get('settings.theme') . '/fieldsets/';
return PATH['themes'] . '/' . $this->flextype['registry']->get('settings.theme') . '/fieldsets/';
}
/**
@@ -153,6 +168,6 @@ class Fieldsets
*/
private function _file_location(string $name) : string
{
return PATH['themes'] . '/' . Registry::get('settings.theme') . '/fieldsets/' . $name . '.yaml';
return PATH['themes'] . '/' . $this->flextype['registry']->get('settings.theme') . '/fieldsets/' . $name . '.yaml';
}
}

View File

@@ -19,7 +19,6 @@ use Flextype\Component\Registry\Registry;
class Plugins
{
/**
* Flextype Dependency Container
*/
@@ -33,9 +32,9 @@ class Plugins
private $locales = [];
/**
* Private construct method to enforce singleton behavior.
* __construct
*
* @access private
* @access public
*/
public function __construct($flextype, $app)
{
@@ -53,7 +52,7 @@ class Plugins
private function init($flextype, $app) : void
{
// Set empty plugins item
Registry::set('plugins', []);
$this->flextype['registry']->set('plugins', []);
// Get Plugins List
$plugins_list = Filesystem::listContents(PATH['plugins']);
@@ -66,7 +65,7 @@ class Plugins
// Get plugins list from cache or scan plugins folder and create new plugins cache item
if ($this->flextype['cache']->contains($plugins_cache_id)) {
Registry::set('plugins', $this->flextype['cache']->fetch($plugins_cache_id));
$this->flextype['registry']->set('plugins', $this->flextype['cache']->fetch($plugins_cache_id));
} else {
// If Plugins List isnt empty
@@ -98,7 +97,7 @@ class Plugins
$_plugins_config[basename($_plugin_config, '.yaml')] = array_merge($plugin_settings, $plugin_config);
}
Registry::set('plugins', $_plugins_config);
$this->flextype['registry']->set('plugins', $_plugins_config);
$this->flextype['cache']->save($plugins_cache_id, $_plugins_config);
}
}
@@ -107,7 +106,7 @@ class Plugins
Plugins::includeEnabledPlugins($flextype, $app);
Event::dispatch('onPluginsInitialized');
//Event::dispatch('onPluginsInitialized');
}
}
@@ -173,9 +172,9 @@ class Plugins
*/
private function includeEnabledPlugins($flextype, $app) : void
{
if (is_array(Registry::get('plugins')) && count(Registry::get('plugins')) > 0) {
foreach (Registry::get('plugins') as $plugin_name => $plugin) {
if (Registry::get('plugins.' . $plugin_name . '.enabled')) {
if (is_array($this->flextype['registry']->get('plugins')) && count($this->flextype['registry']->get('plugins')) > 0) {
foreach ($this->flextype['registry']->get('plugins') as $plugin_name => $plugin) {
if ($this->flextype['registry']->get('plugins.' . $plugin_name . '.enabled')) {
include_once PATH['plugins'] . '/' . $plugin_name . '/' . $plugin_name . '.php';
}
}

View File

@@ -16,6 +16,19 @@ use Flextype\Component\Filesystem\Filesystem;
class Snippets
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* __construct
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Get snippet
*

View File

@@ -30,75 +30,9 @@ use League\Glide\Responses\SlimResponseFactory;
*/
define ('FLEXTYPE_VERSION', '0.8.3');
// Set empty settings array
Registry::set('settings', []);
// Set settings files path
$default_settings_file_path = PATH['config']['default'] . '/settings.yaml';
$site_settings_file_path = PATH['config']['site'] . '/settings.yaml';
// Set settings if Flextype settings and Site settings config files exist
if (Filesystem::has($default_settings_file_path) && Filesystem::has($site_settings_file_path)) {
if (($content = Filesystem::read($default_settings_file_path)) === false) {
throw new \RuntimeException('Load file: ' . $default_settings_file_path . ' - failed!');
} else {
$default_settings = YamlParser::decode($content);
}
if (($content = Filesystem::read($site_settings_file_path)) === false) {
throw new \RuntimeException('Load file: ' . $site_settings_file_path . ' - failed!');
} else {
$site_settings = YamlParser::decode($content);
}
// Merge settings
$settings = array_replace_recursive($default_settings, $site_settings);
// Set settings
Registry::set('settings', $settings);
} else {
throw new \RuntimeException("Flextype settings and Site settings config files does not exist.");
}
// Set internal encoding
function_exists('mb_language') and mb_language('uni');
function_exists('mb_regex_encoding') and mb_regex_encoding(Registry::get('settings.charset'));
function_exists('mb_internal_encoding') and mb_internal_encoding(Registry::get('settings.charset'));
/**
* Set error handler
*
* @access private
*/
// Display Errors
if (Registry::get('settings.errors.display')) {
//define('DEVELOPMENT', true);
error_reporting(-1);
} else {
//define('DEVELOPMENT', false);
error_reporting(0);
}
// Create directory for logs
!Filesystem::has(LOGS_PATH) and Filesystem::createDir(LOGS_PATH);
// Set Error handler
//set_error_handler('Flextype\Component\ErrorHandler\ErrorHandler::error');
//register_shutdown_function('Flextype\Component\ErrorHandler\ErrorHandler::fatal');
//set_exception_handler('Flextype\Component\ErrorHandler\ErrorHandler::exception');
// Set default timezone
date_default_timezone_set(Registry::get('settings.timezone'));
// Start the session
Session::start();
// Get Themes Instance
//Themes::getInstance();
// Configure application
$config = [
'settings' => [
@@ -132,10 +66,80 @@ $app = new \Slim\App($config);
$flextype = $app->getContainer();
/**
* Add fieldsets service to Flextype container
* Add registry service to Flextype container
*/
$flextype['cache'] = function($container) {
return new Cache();
$flextype['registry'] = function($container) {
return new Registry();
};
// Set empty settings array
$flextype['registry']->set('settings', []);
// Set settings files path
$default_settings_file_path = PATH['config']['default'] . '/settings.yaml';
$site_settings_file_path = PATH['config']['site'] . '/settings.yaml';
// Set settings if Flextype settings and Site settings config files exist
if (Filesystem::has($default_settings_file_path) && Filesystem::has($site_settings_file_path)) {
if (($content = Filesystem::read($default_settings_file_path)) === false) {
throw new \RuntimeException('Load file: ' . $default_settings_file_path . ' - failed!');
} else {
$default_settings = YamlParser::decode($content);
}
if (($content = Filesystem::read($site_settings_file_path)) === false) {
throw new \RuntimeException('Load file: ' . $site_settings_file_path . ' - failed!');
} else {
$site_settings = YamlParser::decode($content);
}
// Merge settings
$settings = array_replace_recursive($default_settings, $site_settings);
// Set settings
$flextype['registry']->set('settings', $settings);
} else {
throw new \RuntimeException("Flextype settings and Site settings config files does not exist.");
}
// Set internal encoding
function_exists('mb_language') and mb_language('uni');
function_exists('mb_regex_encoding') and mb_regex_encoding($flextype['registry']->get('settings.charset'));
function_exists('mb_internal_encoding') and mb_internal_encoding($flextype['registry']->get('settings.charset'));
/**
* Set error handler
*
* @access private
*/
// Display Errors
if ($flextype['registry']->get('settings.errors.display')) {
//define('DEVELOPMENT', true);
error_reporting(-1);
} else {
//define('DEVELOPMENT', false);
error_reporting(0);
}
// Create directory for logs
!Filesystem::has(LOGS_PATH) and Filesystem::createDir(LOGS_PATH);
// Set Error handler
//set_error_handler('Flextype\Component\ErrorHandler\ErrorHandler::error');
//register_shutdown_function('Flextype\Component\ErrorHandler\ErrorHandler::fatal');
//set_exception_handler('Flextype\Component\ErrorHandler\ErrorHandler::exception');
// Set default timezone
date_default_timezone_set($flextype['registry']->get('settings.timezone'));
/**
* Add cache service to Flextype container
*/
$flextype['cache'] = function($container) use ($flextype) {
return new Cache($flextype);
};
/**
@@ -199,15 +203,15 @@ $flextype['images'] = function($container) {
/**
* Add fieldsets service to Flextype container
*/
$flextype['fieldsets'] = function($container) {
return new Fieldsets();
$flextype['fieldsets'] = function($container) use ($flextype) {
return new Fieldsets($flextype);
};
/**
* Add snippets service to Flextype container
*/
$flextype['snippets'] = function($container) {
return new Snippets();
$flextype['snippets'] = function($container) use ($flextype){
return new Snippets($flextype);
};
/**

View File

@@ -19,7 +19,7 @@ $app->get('{uri:.+}', function (Request $request, Response $response, array $arg
// If uri is empty then it is main page else use entry uri
if ($uri === '/') {
$entry_uri = Registry::get('settings.entries.main');
$entry_uri = $this->get('registry')->get('settings.entries.main');
} else {
$entry_uri = ltrim($uri, '/');
}
@@ -35,10 +35,10 @@ $app->get('{uri:.+}', function (Request $request, Response $response, array $arg
//Http::setResponseStatus(404);
$entry['title'] = Registry::get('settings.entries.error404.title');
$entry['description'] = Registry::get('settings.entries.error404.description');
$entry['content'] = Registry::get('settings.entries.error404.content');
$entry['template'] = Registry::get('settings.entries.error404.template');
$entry['title'] = $this->get('registry')->get('settings.entries.error404.title');
$entry['description'] = $this->get('registry')->get('settings.entries.error404.description');
$entry['content'] = $this->get('registry')->get('settings.entries.error404.content');
$entry['template'] = $this->get('registry')->get('settings.entries.error404.template');
//$response->withStatus(404);
@@ -50,17 +50,17 @@ $app->get('{uri:.+}', function (Request $request, Response $response, array $arg
//Http::setResponseStatus(404);
//$response->withStatus(404);
$entry['title'] = Registry::get('settings.entries.error404.title');
$entry['description'] = Registry::get('settings.entries.error404.description');
$entry['content'] = Registry::get('settings.entries.error404.content');
$entry['template'] = Registry::get('settings.entries.error404.template');
$entry['title'] = $this->get('registry')->get('settings.entries.error404.title');
$entry['description'] = $this->get('registry')->get('settings.entries.error404.description');
$entry['content'] = $this->get('registry')->get('settings.entries.error404.content');
$entry['template'] = $this->get('registry')->get('settings.entries.error404.template');
}
$path = 'themes/' . Registry::get('settings.theme') . '/' . (empty($entry['template']) ? 'templates/default' : 'templates/' . $entry['template']) . '.html';
$path = 'themes/' . $this->get('registry')->get('settings.theme') . '/' . (empty($entry['template']) ? 'templates/default' : 'templates/' . $entry['template']) . '.html';
return $this->view->render($response,
$path, [
'entry' => $entry,
'registry' => Registry::registry()
'registry' => $this->get('registry')->dump()
]);
});