aboutsummaryrefslogtreecommitdiff
path: root/lib/autoload.php
diff options
context:
space:
mode:
authorGravatar Alexis Degrugillier <aledeg@users.noreply.github.com> 2020-12-26 06:47:39 -0500
committerGravatar GitHub <noreply@github.com> 2020-12-26 12:47:39 +0100
commit46cb89adf842e2fbac254fc99355d6577e4e86eb (patch)
treefde13083862e0762b46250a3aafbf8788c5ba640 /lib/autoload.php
parentabfbeb6b71a5144145bd39beb6b774e1fce0c94b (diff)
Extract autoloading process (#3283)
* Extract autoloading process The process sits in its own file now to ease future improvements. * Change the autoload process Before, the autoload process was too restricted. It was really dependant on our code tree. It was hard to add more classes to be loaded automatically. On top of that, it did not support autoloading classes following the PSR-4 recommendation. Now, the autoload process is more open. It supports partially the PSR-4 recommendation, there is no specific code to load Minz classes or PHPMailer classes. This is the starting point to reorganize the codebase to introduce long waiting changes as seen in #789. It would be a nice to later rework the tree, rename classes, and add namespace in a fashion that follows the PSR-4. Then specific FRSS workarounds in the autoload could be dropped.
Diffstat (limited to 'lib/autoload.php')
-rw-r--r--lib/autoload.php74
1 files changed, 74 insertions, 0 deletions
diff --git a/lib/autoload.php b/lib/autoload.php
new file mode 100644
index 000000000..5be952876
--- /dev/null
+++ b/lib/autoload.php
@@ -0,0 +1,74 @@
+<?php
+
+class ClassLoader {
+ private $searchPath = [];
+
+ /**
+ * Register path in which to look for a class.
+ */
+ public function registerPath($path) {
+ if (is_string($path)) {
+ $this->searchPath[] = $path;
+ }
+ if (is_array($path)) {
+ array_push($this->searchPath, ...$path);
+ }
+ }
+
+ /**
+ * Load class file if found.
+ */
+ public function loadClass($class)
+ {
+ if ($file = $this->findFile($class)) {
+ require $file;
+ }
+ }
+
+ /**
+ * Find the file containing the class definition.
+ */
+ public function findFile($class) {
+ // This match most of classes directly
+ foreach ($this->searchPath as $path) {
+ $file = $path . DIRECTORY_SEPARATOR . str_replace(['\\', '_'], DIRECTORY_SEPARATOR, $class) . '.php';
+ if (file_exists($file)) {
+ return $file;
+ }
+ }
+
+ // This match FRSS model classes
+ $freshrssClass = str_replace('FreshRSS_', '', $class);
+ foreach ($this->searchPath as $path) {
+ $file = $path . DIRECTORY_SEPARATOR . str_replace(['\\', '_'], DIRECTORY_SEPARATOR, $freshrssClass) . '.php';
+ if (file_exists($file)) {
+ return $file;
+ }
+ }
+
+ // This match FRSS other classes
+ list(, $classType) = explode('_', $freshrssClass);
+ foreach ($this->searchPath as $path) {
+ $file = $path . DIRECTORY_SEPARATOR . $classType . 's' . DIRECTORY_SEPARATOR . str_replace('_', '', $freshrssClass) . '.php';
+ if (file_exists($file)) {
+ return $file;
+ }
+ }
+ }
+
+ /**
+ * Register the current loader in the autoload queue.
+ */
+ public function register($prepend = false) {
+ spl_autoload_register([$this, 'loadClass'], true, $prepend);
+ }
+}
+
+$loader = new ClassLoader();
+$loader->registerPath([
+ APP_PATH,
+ APP_PATH . DIRECTORY_SEPARATOR . 'Models',
+ LIB_PATH,
+ LIB_PATH . DIRECTORY_SEPARATOR . 'SimplePie',
+]);
+$loader->register();