Skip to content

Commit

Permalink
Site 1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Awilum committed Aug 6, 2020
1 parent 0547529 commit deee3d7
Show file tree
Hide file tree
Showing 4 changed files with 301 additions and 2 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<a name="1.2.0"></a>
# [1.2.0](https://github.com/flextype-plugins/site/compare/v1.1.0...v1.2.0) (2020-08-06)

### Features

* **core** add Themes functionality
* **core** add ability to include Theme bootstrap file.

<a name="1.1.0"></a>
# [1.1.0](https://github.com/flextype-plugins/site/compare/v1.0.2...v1.1.0) (2020-08-05)

Expand Down
20 changes: 20 additions & 0 deletions app/Controllers/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ class SiteController extends Container
*/
public $entry = [];

/**
* __construct
*/
public function __construct($flextype, $app)
{
$this->container = $flextype;
$this->app = $app;
}

/**
* Index page
*
Expand Down Expand Up @@ -93,6 +102,8 @@ public function index(Request $request, Response $response, array $args) : Respo
// Set template path for current entry
$path = 'themes/' . $this->registry->get('plugins.site.settings.theme') . '/' . (empty($this->entry['template']) ? 'templates/default' : 'templates/' . $this->entry['template']) . '.html';

self::includeCurrentThemeBootstrap($this->container, $this->app);

if (! Filesystem::has(PATH['project'] . '/' . $path)) {
return $response->write("Template {$this->entry['template']} not found");
}
Expand All @@ -104,6 +115,15 @@ public function index(Request $request, Response $response, array $args) : Respo
return $this->twig->render($response, $path, ['entry' => $this->entry, 'query' => $query, 'uri' => $uri]);
}

private static function includeCurrentThemeBootstrap($flextype, $app)
{
$bootstrap_path = 'themes/' . $flextype->registry->get('plugins.site.settings.theme') . '/bootstrap.php';

if (Filesystem::has(PATH['project'] . '/' . $bootstrap_path)) {
include_once PATH['project'] . '/' . $bootstrap_path;
}
}

/**
* Error404 page
*
Expand Down
258 changes: 258 additions & 0 deletions app/Models/Themes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
<?php

declare(strict_types=1);

/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/

namespace Flextype\Plugin\Site\Models;

use Flextype\Component\Filesystem\Filesystem;
use RuntimeException;
use function array_merge;
use function array_replace_recursive;
use function count;
use function filemtime;
use function is_array;
use function md5;

class Themes
{
/**
* Flextype Dependency Container
*/
private $flextype;

/**
* Private construct method to enforce singleton behavior.
*
* @access private
*/
public function __construct($flextype)
{
$this->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['project'] . '/config/themes/' . $theme['dirname'];

// Set default theme settings and manifest files
$default_theme_settings_file = PATH['project'] . '/themes/' . $theme['dirname'] . '/settings.yaml';
$default_theme_manifest_file = PATH['project'] . '/themes/' . $theme['dirname'] . '/theme.yaml';

// Set custom theme settings and manifest files
$custom_theme_settings_file = PATH['project'] . '/config/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['yaml']->decode($default_theme_settings_file_content);
}

// 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['yaml']->decode($custom_theme_settings_file_content);
}

// 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['yaml']->decode($default_theme_manifest_file_content);

// 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['project'] . '/themes/' . $theme['dirname'] . '/settings.yaml';
$default_theme_manifest_file = PATH['project'] . '/themes/' . $theme['dirname'] . '/theme.yaml';
$site_theme_settings_file = PATH['project'] . '/config/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['project'] . '/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['project'] . '/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['project'] . '/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['project'] . '/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;
}
}
17 changes: 15 additions & 2 deletions dependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,23 @@
namespace Flextype\Plugin\Site;

use Flextype\Plugin\Site\Controllers\SiteController;
use Flextype\Plugin\Site\Models\Themes;

/**
* Add themes service to Flextype container
*/
$flextype['themes'] = static function ($flextype) use ($app) {
return new Themes($flextype, $app);
};

/**
* Init themes
*/
$flextype['themes']->init($flextype, $app);

/**
* Add site controller to Flextype container
*/
$flextype['SiteController'] = static function ($container) {
return new SiteController($container);
$flextype['SiteController'] = static function ($flextype) use ($app) {
return new SiteController($flextype, $app);
};

0 comments on commit deee3d7

Please sign in to comment.