From 5370b98936418b2b548c023813039b8bf2074a55 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 13 Apr 2018 17:31:56 +0200 Subject: [inc/Module] Overhaul dependency loading and handling of js/css This is supposed to prevent unneeded js/css from being pulled in --- inc/module.inc.php | 63 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 23 deletions(-) (limited to 'inc/module.inc.php') diff --git a/inc/module.inc.php b/inc/module.inc.php index 7610c720..fa2c4b98 100644 --- a/inc/module.inc.php +++ b/inc/module.inc.php @@ -35,7 +35,7 @@ class Module if ($module === false) return false; if ($activate) { - $module->activate(); + $module->activate(1, true); } return !$module->hasMissingDependencies(); } @@ -139,9 +139,11 @@ class Module */ private $category = false; + private $clientPlugin = false; private $depsMissing = false; private $depsChecked = false; private $activated = false; + private $directActivation = false; private $dependencies = array(); private $name; /** @@ -165,6 +167,9 @@ class Module if (isset($json['category']) && is_string($json['category'])) { $this->category = $json['category']; } + if (isset($json['client-plugin'])) { + $this->clientPlugin = (bool)$json['client-plugin']; + } $this->name = $name; } @@ -184,21 +189,33 @@ class Module return new $class(); } - public function activate($depth = 1) + public function activate($depth, $direct) { - if ($this->activated !== false || $this->depsMissing) - return $this->activated !== false; + if ($this->depsMissing) + return false; + if ($this->activated !== false && ($this->directActivation || !$direct)) + return true; + if ($depth === null && $direct === null) { + // This is the current page, always load its scripts + $this->clientPlugin = true; + $direct = true; + } + if ($this->activated === false) { + spl_autoload_register(function ($class) { + $file = 'modules/' . $this->name . '/inc/' . preg_replace('/[^a-z0-9]/', '', strtolower($class)) . '.inc.php'; + if (!file_exists($file)) + return; + require_once $file; + }); + } $this->activated = $depth; - spl_autoload_register(function($class) { - $file = 'modules/' . $this->name . '/inc/' . preg_replace('/[^a-z0-9]/', '', strtolower($class)) . '.inc.php'; - if (!file_exists($file)) - return; - require_once $file; - }); + if ($direct) { + $this->directActivation = true; + } foreach ($this->dependencies as $dep) { $get = self::get($dep); if ($get !== false) { - $get->activate($depth + 1); + $get->activate($depth + 1, $direct && $this->clientPlugin); } } return true; @@ -263,26 +280,26 @@ class Module return 'modules/' . $this->name; } - public function getScripts($externalOnly) + public function getScripts() { - if (!$externalOnly) { - if (!isset($this->scripts['clientscript.js']) && file_exists($this->getDir() . '/clientscript.js')) { - $this->scripts['clientscript.js'] = false; + if ($this->directActivation && $this->clientPlugin) { + if (!in_array('clientscript.js', $this->scripts)) { + $this->scripts[] = 'clientscript.js'; } - return array_keys($this->scripts); + return $this->scripts; } - return array_keys(array_filter($this->scripts)); + return []; } - public function getCss($externalOnly) + public function getCss() { - if (!$externalOnly) { - if (!isset($this->css['style.css']) && file_exists($this->getDir() . '/style.css')) { - $this->css['style.css'] = false; + if ($this->directActivation && $this->clientPlugin) { + if (!in_array('style.css', $this->css)) { + $this->css[] = 'style.css'; } - return array_keys($this->css); + return $this->css; } - return array_keys(array_filter($this->css)); + return []; } } -- cgit v1.2.3-55-g7522 From 1f18997b2d72a5aea772f0f9d6ddb61718fbd380 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 27 Apr 2018 14:00:57 +0200 Subject: Don't try to load non-existent style.css/clientscript.js --- inc/module.inc.php | 4 ++-- modules-available/js_chart/config.json | 3 --- modules-available/js_circles/config.json | 3 --- modules-available/js_moment/config.json | 3 --- modules-available/js_stupidtable/config.json | 3 --- 5 files changed, 2 insertions(+), 14 deletions(-) (limited to 'inc/module.inc.php') diff --git a/inc/module.inc.php b/inc/module.inc.php index fa2c4b98..6d160787 100644 --- a/inc/module.inc.php +++ b/inc/module.inc.php @@ -283,7 +283,7 @@ class Module public function getScripts() { if ($this->directActivation && $this->clientPlugin) { - if (!in_array('clientscript.js', $this->scripts)) { + if (!in_array('clientscript.js', $this->scripts) && file_exists($this->getDir() . '/clientscript.js')) { $this->scripts[] = 'clientscript.js'; } return $this->scripts; @@ -294,7 +294,7 @@ class Module public function getCss() { if ($this->directActivation && $this->clientPlugin) { - if (!in_array('style.css', $this->css)) { + if (!in_array('style.css', $this->css) && file_exists($this->getDir() . '/style.css')) { $this->css[] = 'style.css'; } return $this->css; diff --git a/modules-available/js_chart/config.json b/modules-available/js_chart/config.json index 3cf0d162..5adc886f 100644 --- a/modules-available/js_chart/config.json +++ b/modules-available/js_chart/config.json @@ -1,7 +1,4 @@ { - "css": [ - "style.css" - ], "scripts": [ "clientscript.js" ], diff --git a/modules-available/js_circles/config.json b/modules-available/js_circles/config.json index 3cf0d162..5adc886f 100644 --- a/modules-available/js_circles/config.json +++ b/modules-available/js_circles/config.json @@ -1,7 +1,4 @@ { - "css": [ - "style.css" - ], "scripts": [ "clientscript.js" ], diff --git a/modules-available/js_moment/config.json b/modules-available/js_moment/config.json index 69bb0aa9..96c02bce 100644 --- a/modules-available/js_moment/config.json +++ b/modules-available/js_moment/config.json @@ -1,8 +1,5 @@ { "dependencies": [], - "css": [ - "style.css" - ], "scripts": [ "clientscript.js" ], diff --git a/modules-available/js_stupidtable/config.json b/modules-available/js_stupidtable/config.json index 3cf0d162..5adc886f 100644 --- a/modules-available/js_stupidtable/config.json +++ b/modules-available/js_stupidtable/config.json @@ -1,7 +1,4 @@ { - "css": [ - "style.css" - ], "scripts": [ "clientscript.js" ], -- cgit v1.2.3-55-g7522