mirror of
https://github.com/flextype/flextype.git
synced 2025-08-18 10:51:21 +02:00
Flextype Core: Themes API - updates #165
- method init() from now will init all themes and create themes registry item - new private method getThemesCacheID() added. - new public method getThemes() added. - from now methods getTemplates() and getPartials() will return full information about founded files.
This commit is contained in:
@@ -31,84 +31,172 @@ class Themes
|
||||
$this->flextype = $flextype;
|
||||
}
|
||||
|
||||
/**
|
||||
* Init themes
|
||||
*/
|
||||
public function init($flextype, $app)
|
||||
{
|
||||
|
||||
// Get current theme
|
||||
$theme = $this->flextype['registry']->get('settings.theme');
|
||||
|
||||
// Set empty themes items
|
||||
// Set empty themes list item
|
||||
$this->flextype['registry']->set('themes', []);
|
||||
|
||||
// Create Unique Cache ID for Theme
|
||||
$theme_cache_id = md5('theme' . filemtime(PATH['themes'] . '/' . $theme . '/' . 'settings.json') .
|
||||
filemtime(PATH['themes'] . '/' . $theme . '/' . 'theme.json'));
|
||||
// Get themes list
|
||||
$themes_list = $this->getThemes();
|
||||
|
||||
// Get Theme mafifest file and write to settings.themes array
|
||||
if ($this->flextype['cache']->contains($theme_cache_id)) {
|
||||
$this->flextype['registry']->set('themes.' . $this->flextype['registry']->get('settings.theme'), $this->flextype['cache']->fetch($theme_cache_id));
|
||||
} else {
|
||||
if (Filesystem::has($theme_settings = PATH['themes'] . '/' . $theme . '/' . 'settings.json') and
|
||||
Filesystem::has($theme_config = PATH['themes'] . '/' . $theme . '/' . 'theme.json')) {
|
||||
$theme_settings = JsonParser::decode(Filesystem::read($theme_settings));
|
||||
$theme_config = JsonParser::decode(Filesystem::read($theme_config));
|
||||
$_theme = array_merge($theme_settings, $theme_config);
|
||||
$this->flextype['registry']->set('themes.' . $this->flextype['registry']->get('settings.theme'), $_theme);
|
||||
$this->flextype['cache']->save($theme_cache_id, $_theme);
|
||||
// If themes list isnt empty then create themes cache ID and go through the themes list...
|
||||
if (is_array($themes_list) && count($themes_list) > 0) {
|
||||
|
||||
// Get themes cache ID
|
||||
$themes_cache_id = $this->getThemesCacheID($themes_list);
|
||||
|
||||
// Get themes list from cache or scan themes folder and create new themes cache item in the registry
|
||||
if ($this->flextype['cache']->contains($themes_cache_id)) {
|
||||
$this->flextype['registry']->set('themes', $this->flextype['cache']->fetch($themes_cache_id));
|
||||
} else {
|
||||
|
||||
// Go through the themes list...
|
||||
foreach ($themes_list as $theme) {
|
||||
|
||||
// Get theme settings
|
||||
if (Filesystem::has($theme_settings_file = PATH['themes'] . '/' . $theme['dirname'] . '/settings.json')) {
|
||||
if (($content = Filesystem::read($theme_settings_file)) === false) {
|
||||
throw new \RuntimeException('Load file: ' . $theme_settings_file . ' - failed!');
|
||||
} else {
|
||||
$theme_settings = JsonParser::decode($content);
|
||||
}
|
||||
}
|
||||
|
||||
// Get theme manifest
|
||||
if (Filesystem::has($theme_manifest_file = PATH['themes'] . '/' . $theme['dirname'] . '/theme.json')) {
|
||||
if (($content = Filesystem::read($theme_manifest_file)) === false) {
|
||||
throw new \RuntimeException('Load file: ' . $theme_manifest_file . ' - failed!');
|
||||
} else {
|
||||
$theme_manifest = JsonParser::decode($content);
|
||||
}
|
||||
}
|
||||
|
||||
$themes[$theme['dirname']] = array_merge($theme_settings, $theme_manifest);
|
||||
}
|
||||
|
||||
// Save parsed themes list in the registry themes
|
||||
$this->flextype['registry']->set('themes', $themes);
|
||||
|
||||
// Save parsed themes list in the cache
|
||||
$this->flextype['cache']->save($themes_cache_id, $themes);
|
||||
}
|
||||
}
|
||||
|
||||
// Emit onThemesInitialized
|
||||
$this->flextype['emitter']->emit('onThemesInitialized');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get partials for current theme
|
||||
* Get themes cache ID
|
||||
*
|
||||
* @param array $themes_list Themes list
|
||||
* @access protected
|
||||
* @return string
|
||||
*/
|
||||
private function getThemesCacheID(array $themes_list) : string
|
||||
{
|
||||
// Themes cache id
|
||||
$_themes_cache_id = '';
|
||||
|
||||
// Go through themes list...
|
||||
if (is_array($themes_list) && count($themes_list) > 0) {
|
||||
foreach ($themes_list as $theme) {
|
||||
if (Filesystem::has($_themes_settings = PATH['themes'] . '/' . $theme['dirname'] . '/settings.json') and
|
||||
Filesystem::has($_themes_manifest = PATH['themes'] . '/' . $theme['dirname'] . '/plugin.json')) {
|
||||
$_themes_cache_id .= filemtime($_themes_settings) . filemtime($_themes_manifest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create Unique Cache ID for Themes
|
||||
$themes_cache_id = md5('themes' . PATH['themes'] . $_themes_cache_id);
|
||||
|
||||
// Return themes cache id
|
||||
return $themes_cache_id;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get list of themes
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function getPartials() : array
|
||||
public function getThemes()
|
||||
{
|
||||
$partials = [];
|
||||
// Init themes list
|
||||
$themes_list = [];
|
||||
|
||||
// Get themes list
|
||||
$_themes_list = Filesystem::listContents(PATH['themes']);
|
||||
|
||||
// Go through founded themes
|
||||
foreach($_themes_list as $theme) {
|
||||
if ($theme['type'] == 'dir' && Filesystem::has($theme['path'] . '/' . 'theme.json')) {
|
||||
$themes_list[] = $theme;
|
||||
}
|
||||
}
|
||||
|
||||
return $themes_list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get partials for theme
|
||||
*
|
||||
* @access public
|
||||
* @param string $theme Theme id
|
||||
* @return array
|
||||
*/
|
||||
public function getPartials(string $theme) : array
|
||||
{
|
||||
// Init partials list
|
||||
$partials_list = [];
|
||||
|
||||
// Get partials files
|
||||
$_partials = Filesystem::listContents(PATH['themes'] . '/' . $this->flextype['registry']->get('settings.theme') . '/templates/partials/');
|
||||
$_partials_list = Filesystem::listContents(PATH['themes'] . '/' . $theme . '/templates/partials/');
|
||||
|
||||
// If there is any partials file then go...
|
||||
if (count($_partials) > 0) {
|
||||
foreach ($_partials as $partial) {
|
||||
if (count($_partials_list) > 0) {
|
||||
foreach ($_partials_list as $partial) {
|
||||
if ($partial['type'] == 'file' && $partial['extension'] == 'html') {
|
||||
$partials[$partial['basename']] = $partial['basename'];
|
||||
$partials_list[] = $partial;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// return partials
|
||||
return $partials;
|
||||
return $partials_list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get templates for current theme
|
||||
* Get templates for theme
|
||||
*
|
||||
* @access public
|
||||
* @param string $theme Theme id
|
||||
* @return array
|
||||
*/
|
||||
public function getTemplates() : array
|
||||
public function getTemplates(string $theme) : array
|
||||
{
|
||||
$templates = [];
|
||||
// Init templates list
|
||||
$templates_list = [];
|
||||
|
||||
// Get templates files
|
||||
$_templates = Filesystem::listContents(PATH['themes'] . '/' . $this->flextype['registry']->get('settings.theme') . '/templates/');
|
||||
$_templates_list = Filesystem::listContents(PATH['themes'] . '/' . $theme . '/templates/');
|
||||
|
||||
// If there is any template file then go...
|
||||
if (count($_templates) > 0) {
|
||||
foreach ($_templates as $template) {
|
||||
if (count($_templates_list) > 0) {
|
||||
foreach ($_templates_list as $template) {
|
||||
if ($template['type'] == 'file' && $template['extension'] == 'html') {
|
||||
$templates[$template['basename']] = $template['basename'];
|
||||
$templates_list[] = $template;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// return templates
|
||||
return $templates;
|
||||
return $templates_list;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user