Minor code cleanups

This commit is contained in:
Tony Air 2019-12-09 18:18:05 +07:00
parent 478c5fc922
commit a4adb0ab1d
4 changed files with 42 additions and 35 deletions

View File

@ -4,7 +4,7 @@
namespace Site\Controllers; namespace Site\Controllers;
use DNADesign\Elemental\Controllers\ElementController; use DNADesign\Elemental\Controllers\ElementController;
use Site\Templates\DeferedRequirements; use Site\Templates\DeferredRequirements;
class MapElementController extends ElementController class MapElementController extends ElementController
{ {
@ -12,10 +12,10 @@ class MapElementController extends ElementController
{ {
parent::init(); parent::init();
DeferedRequirements::loadCSS('https://api.tiles.mapbox.com/mapbox-gl-js/v0.48.0/mapbox-gl.css'); DeferredRequirements::loadCSS('https://api.tiles.mapbox.com/mapbox-gl-js/v0.48.0/mapbox-gl.css');
DeferedRequirements::loadJS('https://api.tiles.mapbox.com/mapbox-gl-js/v0.48.0/mapbox-gl.js'); DeferredRequirements::loadJS('https://api.tiles.mapbox.com/mapbox-gl-js/v0.48.0/mapbox-gl.js');
DeferedRequirements::loadCSS('app_Site.Controllers.MapElementController.css'); DeferredRequirements::loadCSS('app_Site.Controllers.MapElementController.css');
DeferedRequirements::loadJS('app_Site.Controllers.MapElementController.js'); DeferredRequirements::loadJS('app_Site.Controllers.MapElementController.js');
} }
} }

View File

@ -20,7 +20,7 @@ use SilverStripe\ORM\ArrayList;
use DNADesign\Elemental\Models\ElementContent; use DNADesign\Elemental\Models\ElementContent;
use DNADesign\Elemental\Models\ElementalArea; use DNADesign\Elemental\Models\ElementalArea;
use DNADesign\ElementalUserForms\Control\ElementFormController; use DNADesign\ElementalUserForms\Control\ElementFormController;
use Site\Templates\DeferedRequirements; use Site\Templates\DeferredRequirements;
class PageController extends ContentController class PageController extends ContentController
{ {
@ -31,9 +31,9 @@ class PageController extends ContentController
private $site_message; private $site_message;
private $search_term; private $search_term;
public function init() protected function init()
{ {
DeferedRequirements::Auto(); DeferredRequirements::Auto();
return parent::init(); return parent::init();
} }

View File

@ -1,5 +1,7 @@
<?php <?php
/** @noinspection PhpUnusedPrivateFieldInspection */
namespace Site\Templates; namespace Site\Templates;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
@ -8,9 +10,8 @@ use SilverStripe\View\Requirements;
use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Config;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
use SilverStripe\Core\Path; use SilverStripe\Core\Path;
use SilverStripe\Core\Manifest\ManifestFileFinder;
class DeferedRequirements implements TemplateGlobalProvider class DeferredRequirements implements TemplateGlobalProvider
{ {
private static $css = []; private static $css = [];
private static $js = []; private static $js = [];
@ -24,7 +25,7 @@ class DeferedRequirements implements TemplateGlobalProvider
/** /**
* @return array * @return array
*/ */
public static function get_template_global_variables() public static function get_template_global_variables(): array
{ {
return [ return [
'AutoRequirements' => 'Auto', 'AutoRequirements' => 'Auto',
@ -33,7 +34,7 @@ class DeferedRequirements implements TemplateGlobalProvider
]; ];
} }
public static function Auto($class = false) public static function Auto($class = false): string
{ {
$config = Config::inst()->get(self::class); $config = Config::inst()->get(self::class);
$projectName = WebpackTemplateProvider::projectName(); $projectName = WebpackTemplateProvider::projectName();
@ -69,41 +70,41 @@ class DeferedRequirements implements TemplateGlobalProvider
self::loadJS($mainTheme.'.js'); self::loadJS($mainTheme.'.js');
// Custom controller requirements // Custom controller requirements
$class = get_class(Controller::curr()); $curr_class = $class ?: get_class(Controller::curr());
if (isset($config['custom_requirements'][$class])) { if (isset($config['custom_requirements'][$curr_class])) {
foreach ($config['custom_requirements'][$class] as $file) { foreach ($config['custom_requirements'][$curr_class] as $file) {
if (strpos($file, '.css')) { if (strpos($file, '.css')) {
DeferedRequirements::loadCSS($file); self::loadCSS($file);
} }
if (strpos($file, '.js')) { if (strpos($file, '.js')) {
DeferedRequirements::loadJS($file); self::loadJS($file);
} }
} }
} }
$class = str_replace('\\', '.', $class); $curr_class = str_replace('\\', '.', $curr_class);
// Controller requirements // Controller requirements
$themePath = Path::join($cssPath, $mainTheme.'_'.$class . '.css'); $themePath = Path::join($cssPath, $mainTheme.'_'.$curr_class . '.css');
$projectPath = Path::join($cssPath, $projectName.'_'.$class . '.css'); $projectPath = Path::join($cssPath, $projectName.'_'.$curr_class . '.css');
if ($mainTheme && file_exists($themePath)) { if ($mainTheme && file_exists($themePath)) {
self::loadCSS($mainTheme.'_'.$class . '.css'); self::loadCSS($mainTheme.'_'.$curr_class . '.css');
} elseif (file_exists($projectPath)) { } elseif (file_exists($projectPath)) {
self::loadCSS($projectName.'_'.$class . '.css'); self::loadCSS($projectName.'_'.$curr_class . '.css');
} }
$themePath = Path::join($jsPath, $mainTheme.'_'.$class . '.js'); $themePath = Path::join($jsPath, $mainTheme.'_'.$curr_class . '.js');
$projectPath = Path::join($jsPath, $projectName.'_'.$class . '.js'); $projectPath = Path::join($jsPath, $projectName.'_'.$curr_class . '.js');
if ($mainTheme && file_exists($themePath)) { if ($mainTheme && file_exists($themePath)) {
self::loadJS($mainTheme.'_'.$class . '.js'); self::loadJS($mainTheme.'_'.$curr_class . '.js');
} elseif (file_exists($projectPath)) { } elseif (file_exists($projectPath)) {
self::loadJS($projectName.'_'.$class . '.js'); self::loadJS($projectName.'_'.$curr_class . '.js');
} }
return self::forTemplate(); return self::forTemplate();
} }
public static function loadCSS($css) public static function loadCSS($css): void
{ {
$external = (mb_strpos($css, '//') === 0 || mb_strpos($css, 'http') === 0); $external = (mb_strpos($css, '//') === 0 || mb_strpos($css, 'http') === 0);
if ($external || (self::$deferred && !self::_webpackActive())) { if ($external || (self::$deferred && !self::_webpackActive())) {
@ -113,7 +114,7 @@ class DeferedRequirements implements TemplateGlobalProvider
} }
} }
public static function loadJS($js) public static function loadJS($js): void
{ {
/*$external = (mb_substr($js, 0, 2) === '//' || mb_substr($js, 0, 4) === 'http'); /*$external = (mb_substr($js, 0, 2) === '//' || mb_substr($js, 0, 4) === 'http');
if ($external || (self::$defered && !self::_webpackActive())) {*/ if ($external || (self::$defered && !self::_webpackActive())) {*/
@ -125,17 +126,17 @@ class DeferedRequirements implements TemplateGlobalProvider
} }
} }
protected static function _webpackActive() protected static function _webpackActive(): bool
{ {
return class_exists('WebpackTemplateProvider') && WebpackTemplateProvider::isActive(); return class_exists('WebpackTemplateProvider') && WebpackTemplateProvider::isActive();
} }
public static function setDeferred($bool) public static function setDeferred($bool): void
{ {
self::$deferred = $bool; self::$deferred = $bool;
} }
public static function forTemplate() public static function forTemplate(): string
{ {
$result = ''; $result = '';
foreach (self::$css as $css) { foreach (self::$css as $css) {
@ -158,9 +159,9 @@ class DeferedRequirements implements TemplateGlobalProvider
return $result; return $result;
} }
private static function get_url($url) private static function get_url($url): string
{ {
$config = Config::inst()->get(self::class); $config = self::config();
// external URL // external URL
if (strpos($url, '//') !== false) { if (strpos($url, '//') !== false) {
@ -174,9 +175,14 @@ class DeferedRequirements implements TemplateGlobalProvider
: '?'.$version // new param : '?'.$version // new param
: ''; // no version defined : ''; // no version defined
$static_domain = $config['static_domain']; //$static_domain = $config['static_domain'];
$static_domain = $static_domain ?: ''; //$static_domain = $static_domain ?: '';
return $url.$version; return $url.$version;
} }
public static function config(): array
{
return Config::inst()->get(__CLASS__);
}
} }

View File

@ -1,4 +1,5 @@
<?php <?php
/** @noinspection PhpUnusedPrivateFieldInspection */
/** /**
* Directs assets requests to Webpack server or to static files * Directs assets requests to Webpack server or to static files