From 8bcfbef6fb612234c5a808004ba4721246786e30 Mon Sep 17 00:00:00 2001 From: Awilum Date: Sun, 15 Mar 2020 12:01:13 +0300 Subject: [PATCH] refactor(core): remove Themes API from the core #414 --- flextype/core/Themes.php | 258 --------------------------------------- 1 file changed, 258 deletions(-) delete mode 100644 flextype/core/Themes.php diff --git a/flextype/core/Themes.php b/flextype/core/Themes.php deleted file mode 100644 index 3064c86f..00000000 --- a/flextype/core/Themes.php +++ /dev/null @@ -1,258 +0,0 @@ -flextype = $flextype; - } - - /** - * Init themes - */ - public function init($flextype, $app) : void - { - // Set empty themes list item - $this->flextype['registry']->set('themes', []); - - // Get themes list - $themes_list = $this->getThemes(); - - // If Themes List isnt empty then continue - if (! is_array($themes_list) || count($themes_list) <= 0) { - return; - } - - // 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 { - $themes = []; - $themes_settings = []; - $themes_manifest = []; - $default_theme_settings = []; - $site_theme_settings = []; - $default_theme_manifest = []; - - // Go through the themes list... - foreach ($themes_list as $theme) { - - // Set custom theme directory - $custom_theme_settings_dir = PATH['config']['site'] . '/themes/' . $theme['dirname']; - - // Set default theme settings and manifest files - $default_theme_settings_file = PATH['themes'] . '/' . $theme['dirname'] . '/settings.yaml'; - $default_theme_manifest_file = PATH['themes'] . '/' . $theme['dirname'] . '/theme.yaml'; - - // Set custom theme settings and manifest files - $custom_theme_settings_file = PATH['config']['site'] . '/themes/' . $theme['dirname'] . '/settings.yaml'; - - // Create custom theme settings directory - ! Filesystem::has($custom_theme_settings_dir) and Filesystem::createDir($custom_theme_settings_dir); - - // Check if default theme settings file exists - if (! Filesystem::has($default_theme_settings_file)) { - throw new RuntimeException('Load ' . $theme['dirname'] . ' theme settings - failed!'); - } - - // Get default theme manifest content - $default_theme_settings_file_content = Filesystem::read($default_theme_settings_file); - if (trim($default_theme_settings_file_content) === '') { - $default_theme_settings = []; - } else { - $default_theme_settings = $this->flextype['parser']->decode($default_theme_settings_file_content, 'yaml'); - } - - // Create custom theme settings file - ! Filesystem::has($custom_theme_settings_file) and Filesystem::write($custom_theme_settings_file, $default_theme_settings_file_content); - - // Get custom theme settings content - $custom_theme_settings_file_content = Filesystem::read($custom_theme_settings_file); - if (trim($custom_theme_settings_file_content) === '') { - $custom_theme_settings = []; - } else { - $custom_theme_settings = $this->flextype['parser']->decode($custom_theme_settings_file_content, 'yaml'); - } - - // Check if default theme manifest file exists - if (! Filesystem::has($default_theme_manifest_file)) { - RuntimeException('Load ' . $theme['dirname'] . ' theme manifest - failed!'); - } - - // Get default theme manifest content - $default_theme_manifest_file_content = Filesystem::read($default_theme_manifest_file); - $default_theme_manifest = $this->flextype['parser']->decode($default_theme_manifest_file_content, 'yaml'); - - // Merge theme settings and manifest data - $themes[$theme['dirname']]['manifest'] = $default_theme_manifest; - $themes[$theme['dirname']]['settings'] = array_replace_recursive($default_theme_settings, $custom_theme_settings); - - } - - // 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 Themes Cache ID - * - * @param array $themes_list Themes list - * - * @access protected - */ - private function getThemesCacheID(array $themes_list) : string - { - // Themes Cache ID - $_themes_cache_id = ''; - - // Go through... - if (is_array($themes_list) && count($themes_list) > 0) { - foreach ($themes_list as $theme) { - $default_theme_settings_file = PATH['themes'] . '/' . $theme['dirname'] . '/settings.yaml'; - $default_theme_manifest_file = PATH['themes'] . '/' . $theme['dirname'] . '/theme.yaml'; - $site_theme_settings_file = PATH['config']['site'] . '/themes/' . $theme['dirname'] . '/settings.yaml'; - - $f1 = Filesystem::has($default_theme_settings_file) ? filemtime($default_theme_settings_file) : ''; - $f2 = Filesystem::has($default_theme_manifest_file) ? filemtime($default_theme_manifest_file) : ''; - $f3 = Filesystem::has($site_theme_settings_file) ? filemtime($site_theme_settings_file) : ''; - - $_themes_cache_id .= $f1 . $f2 . $f3; - } - } - - // 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 - * - * @return array - * - * @access public - */ - public function getThemes() : array - { - // 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.yaml')) { - continue; - } - - $themes_list[] = $theme; - } - - return $themes_list; - } - - /** - * Get partials for theme - * - * @param string $theme Theme id - * - * @return array - * - * @access public - */ - public function getPartials(string $theme) : array - { - // Init partials list - $partials_list = []; - - // Get partials files - $_partials_list = Filesystem::listContents(PATH['themes'] . '/' . $theme . '/templates/partials/'); - - // If there is any partials file then go... - if (count($_partials_list) > 0) { - foreach ($_partials_list as $partial) { - if ($partial['type'] !== 'file' || $partial['extension'] !== 'html') { - continue; - } - - $partials_list[] = $partial; - } - } - - // return partials - return $partials_list; - } - - /** - * Get templates for theme - * - * @param string $theme Theme id - * - * @return array - * - * @access public - */ - public function getTemplates(string $theme) : array - { - // Init templates list - $templates_list = []; - - // Get templates files - $_templates_list = Filesystem::listContents(PATH['themes'] . '/' . $theme . '/templates/'); - - // If there is any template file then go... - if (count($_templates_list) > 0) { - foreach ($_templates_list as $template) { - if ($template['type'] !== 'file' || $template['extension'] !== 'html') { - continue; - } - - $templates_list[] = $template; - } - } - - // return templates - return $templates_list; - } -}