From 3d712771496b73dfe7d06c3860056f8d4836168d Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Sat, 4 May 2024 20:28:29 +0200 Subject: [PATCH] mvc - replace Phalcon autoloader with simplified version of our own. for https://github.com/opnsense/core/issues/6389 --- src/opnsense/mvc/app/config/loader.php | 20 +++--- .../app/library/OPNsense/Autoload/Loader.php | 63 +++++++++++++++++++ src/opnsense/mvc/script/load_phalcon.php | 27 +++----- 3 files changed, 80 insertions(+), 30 deletions(-) create mode 100644 src/opnsense/mvc/app/library/OPNsense/Autoload/Loader.php diff --git a/src/opnsense/mvc/app/config/loader.php b/src/opnsense/mvc/app/config/loader.php index 9c7397b23c8..6bc777513a6 100644 --- a/src/opnsense/mvc/app/config/loader.php +++ b/src/opnsense/mvc/app/config/loader.php @@ -1,14 +1,10 @@ setDirectories( - array( - $config->application->controllersDir, - $config->application->modelsDir, - $config->application->libraryDir - ) -)->register(); +require_once (__DIR__ . '/../../app/library/OPNsense/Autoload/Loader.php'); +use OPNsense\Autoload\Loader; +$loader_paths = [ + $config->application->controllersDir, + $config->application->modelsDir, + $config->application->libraryDir +]; +(new Loader($loader_paths))->register(); diff --git a/src/opnsense/mvc/app/library/OPNsense/Autoload/Loader.php b/src/opnsense/mvc/app/library/OPNsense/Autoload/Loader.php new file mode 100644 index 00000000000..964d83b0226 --- /dev/null +++ b/src/opnsense/mvc/app/library/OPNsense/Autoload/Loader.php @@ -0,0 +1,63 @@ +probe_dirs = $dirs; + } + + protected function autoload($className) + { + if (!in_array($className, $this->classes_loaded)) { + $class_path = str_replace("\\", DIRECTORY_SEPARATOR, $className) . '.php'; + foreach ($this->probe_dirs as $dirname) { + $dirname = rtrim($dirname, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; + if (is_file($dirname . $class_path)) { + require_once($dirname . $class_path); + } + } + $this->classes_loaded[] = $className; + } + } + + public function register() + { + if (!$this->is_registered) { + spl_autoload_register([$this, "autoload"],true); + $this->is_registered = true; + } + } +} diff --git a/src/opnsense/mvc/script/load_phalcon.php b/src/opnsense/mvc/script/load_phalcon.php index e3a703ed06c..dac2b1478d3 100644 --- a/src/opnsense/mvc/script/load_phalcon.php +++ b/src/opnsense/mvc/script/load_phalcon.php @@ -1,7 +1,7 @@ setDirectories( - array( - $phalcon_config->application->controllersDir, - $phalcon_config->application->modelsDir, - $phalcon_config->application->libraryDir - ) -)->register(); - -$di->set('config', $phalcon_config); - -unset($phalcon_config); +require_once (__DIR__ . '/../app/library/OPNsense/Autoload/Loader.php'); +use OPNsense\Autoload\Loader; +$loader_paths = [ + $config->application->controllersDir, + $config->application->modelsDir, + $config->application->libraryDir +]; +(new Loader($loader_paths))->register();