From f5409301557e6e8e92c43701916501a4af698a5a Mon Sep 17 00:00:00 2001 From: Dag Date: Wed, 13 Apr 2022 20:57:33 +0200 Subject: [PATCH 1/2] refactor: add autoloader --- lib/ActionFactory.php | 2 -- lib/BridgeFactory.php | 2 -- lib/CacheFactory.php | 2 -- lib/FormatFactory.php | 2 -- lib/rssbridge.php | 16 ++++++++++++++++ tests/ActionImplementationTest.php | 1 - tests/BridgeImplementationTest.php | 1 - tests/CacheImplementationTest.php | 1 - tests/FormatImplementationTest.php | 1 - 9 files changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/ActionFactory.php b/lib/ActionFactory.php index 46e28c5e111..1a0520b5f98 100644 --- a/lib/ActionFactory.php +++ b/lib/ActionFactory.php @@ -27,8 +27,6 @@ public function create($name) { throw new \Exception('Action ' . $name . ' does not exist!'); } - require_once $filePath; - $class = $this->buildClassName($name); if((new \ReflectionClass($class))->isInstantiable()) { diff --git a/lib/BridgeFactory.php b/lib/BridgeFactory.php index fea254f1cb3..5281fa62013 100644 --- a/lib/BridgeFactory.php +++ b/lib/BridgeFactory.php @@ -68,8 +68,6 @@ public function create($name){ throw new \Exception('Bridge file ' . $filePath . ' does not exist!'); } - require_once $filePath; - if((new \ReflectionClass($name))->isInstantiable()) { return new $name(); } diff --git a/lib/CacheFactory.php b/lib/CacheFactory.php index 9ce5c19bddf..441efba185e 100644 --- a/lib/CacheFactory.php +++ b/lib/CacheFactory.php @@ -54,8 +54,6 @@ public function create($name){ throw new \Exception('Cache file ' . $filePath . ' does not exist!'); } - require_once $filePath; - if((new \ReflectionClass($name))->isInstantiable()) { return new $name(); } diff --git a/lib/FormatFactory.php b/lib/FormatFactory.php index e2bba2fc577..c5024e159bc 100644 --- a/lib/FormatFactory.php +++ b/lib/FormatFactory.php @@ -59,8 +59,6 @@ public function create($name){ throw new \Exception('Format file ' . $filePath . ' does not exist!'); } - require_once $pathFormat; - if((new \ReflectionClass($name))->isInstantiable()) { return new $name(); } diff --git a/lib/rssbridge.php b/lib/rssbridge.php index 47a92e73cda..b4baf62aeba 100644 --- a/lib/rssbridge.php +++ b/lib/rssbridge.php @@ -87,3 +87,19 @@ require_once PATH_LIB_VENDOR . 'parsedown/Parsedown.php'; require_once PATH_LIB_VENDOR . 'php-urljoin/src/urljoin.php'; require_once PATH_LIB_VENDOR . 'simplehtmldom/simple_html_dom.php'; + +spl_autoload_register(function ($className) { + $folders = [ + __DIR__ . '/../actions/', + __DIR__ . '/../bridges/', + __DIR__ . '/../caches/', + __DIR__ . '/../formats/', + __DIR__ . '/../lib/', + ]; + foreach ($folders as $folder) { + $file = $folder . $className . '.php'; + if (is_file($file)) { + require $file; + } + } +}); diff --git a/tests/ActionImplementationTest.php b/tests/ActionImplementationTest.php index 554432f3b99..caf10c3ead3 100644 --- a/tests/ActionImplementationTest.php +++ b/tests/ActionImplementationTest.php @@ -51,7 +51,6 @@ public function dataActionsProvider() { } private function setAction($path) { - require_once $path; $this->class = basename($path, '.php'); $this->assertTrue(class_exists($this->class), 'class ' . $this->class . ' doesn\'t exist'); $this->obj = new $this->class(); diff --git a/tests/BridgeImplementationTest.php b/tests/BridgeImplementationTest.php index e634aacf279..304f8d2307b 100644 --- a/tests/BridgeImplementationTest.php +++ b/tests/BridgeImplementationTest.php @@ -213,7 +213,6 @@ public function dataBridgesProvider() { } private function setBridge($path) { - require_once $path; $this->class = basename($path, '.php'); $this->assertTrue(class_exists($this->class), 'class ' . $this->class . ' doesn\'t exist'); $this->obj = new $this->class(); diff --git a/tests/CacheImplementationTest.php b/tests/CacheImplementationTest.php index 25189134eab..92d26f4797b 100644 --- a/tests/CacheImplementationTest.php +++ b/tests/CacheImplementationTest.php @@ -35,7 +35,6 @@ public function dataCachesProvider() { } private function setCache($path) { - require_once $path; $this->class = basename($path, '.php'); $this->assertTrue(class_exists($this->class), 'class ' . $this->class . ' doesn\'t exist'); } diff --git a/tests/FormatImplementationTest.php b/tests/FormatImplementationTest.php index f2df6350ced..24c11b86e41 100644 --- a/tests/FormatImplementationTest.php +++ b/tests/FormatImplementationTest.php @@ -36,7 +36,6 @@ public function dataFormatsProvider() { } private function setFormat($path) { - require_once $path; $this->class = basename($path, '.php'); $this->assertTrue(class_exists($this->class), 'class ' . $this->class . ' doesn\'t exist'); $this->obj = new $this->class(); From b8447d23d396de1d612b0ffdab99dd0b605126fe Mon Sep 17 00:00:00 2001 From: Dag Date: Wed, 13 Apr 2022 21:07:41 +0200 Subject: [PATCH 2/2] lint --- lib/rssbridge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rssbridge.php b/lib/rssbridge.php index 830550e84d5..eccd97bfe86 100644 --- a/lib/rssbridge.php +++ b/lib/rssbridge.php @@ -106,4 +106,4 @@ Configuration::verifyInstallation(); Configuration::loadConfiguration(); -Authentication::showPromptIfNeeded(); \ No newline at end of file +Authentication::showPromptIfNeeded();