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 ++++++++++++++++++++++++++++++++++-------------------- inc/render.inc.php | 4 ++-- 2 files changed, 42 insertions(+), 25 deletions(-) (limited to 'inc') 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 []; } } diff --git a/inc/render.inc.php b/inc/render.inc.php index 0ce39dbe..4b1d3643 100644 --- a/inc/render.inc.php +++ b/inc/render.inc.php @@ -84,7 +84,7 @@ class Render '; // Include any module specific styles foreach ($modules as $module) { - $files = $module->getCss($module !== $pageModule); + $files = $module->getCss(); foreach ($files as $file) { echo ''; } @@ -113,7 +113,7 @@ class Render '; foreach ($modules as $module) { - $files = $module->getScripts($module !== $pageModule); + $files = $module->getScripts(); foreach ($files as $file) { echo ''; } -- cgit v1.2.3-55-g7522