From 58c8839902f7ddeef23a5eb662874a6cd308e504 Mon Sep 17 00:00:00 2001
From: Yassine Doghri <yassine@doghri.fr>
Date: Thu, 2 Sep 2021 10:01:20 +0000
Subject: [PATCH] refactor(componentrenderer): update locateView using a
 lookupModules property in config

---
 app/Config/ViewComponents.php                 | 22 +++++++++++++++++++
 .../ViewComponents/ComponentRenderer.php      | 13 ++++++-----
 .../ViewComponents/Config/ViewComponents.php  | 10 +++++++++
 3 files changed, 39 insertions(+), 6 deletions(-)
 create mode 100644 app/Config/ViewComponents.php

diff --git a/app/Config/ViewComponents.php b/app/Config/ViewComponents.php
new file mode 100644
index 0000000000..25afea847f
--- /dev/null
+++ b/app/Config/ViewComponents.php
@@ -0,0 +1,22 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Config;
+
+use ViewComponents\Config\ViewComponents as ViewComponentsConfig;
+
+class ViewComponents extends ViewComponentsConfig
+{
+    /**
+     * @var array<string, string>
+     */
+    public array $lookupModules = [
+        APP_NAMESPACE => APPPATH,
+        'Modules\Admin' => ROOTPATH . 'modules/Admin/',
+        'Modules\Auth' => ROOTPATH . 'modules/Auth/',
+        'Modules\Analytics' => ROOTPATH . 'modules/Analytics/',
+        'Modules\Install' => ROOTPATH . 'modules/Install/',
+        'Modules\Fediverse' => ROOTPATH . 'modules/Fediverse/',
+    ];
+}
diff --git a/app/Libraries/ViewComponents/ComponentRenderer.php b/app/Libraries/ViewComponents/ComponentRenderer.php
index c20d9d44bb..38a8e9ecc4 100644
--- a/app/Libraries/ViewComponents/ComponentRenderer.php
+++ b/app/Libraries/ViewComponents/ComponentRenderer.php
@@ -137,12 +137,13 @@ class ComponentRenderer
     private function locateView(string $name): string
     {
         // TODO: Is there a better way to locate components local to current module?
-        $modulesToDiscover = [APPPATH];
-        foreach (config('Autoload')->psr4 as $namespace => $path) {
-            if (str_starts_with($this->currentView, $namespace)) {
-                array_unshift($modulesToDiscover, $path);
-            }
-        }
+        $modulesToDiscover = [];
+        $lookupModules = $this->config->lookupModules;
+        $modulesToDiscover = array_filter($lookupModules, function ($namespace): bool {
+            return str_starts_with($this->currentView, $namespace);
+        }, ARRAY_FILTER_USE_KEY);
+        $modulesToDiscover = array_values($modulesToDiscover);
+        $modulesToDiscover[] = $this->config->defaultLookupPath;
 
         $namePath = str_replace('.', '/', $name);
 
diff --git a/app/Libraries/ViewComponents/Config/ViewComponents.php b/app/Libraries/ViewComponents/Config/ViewComponents.php
index efd459958c..56c041fb1e 100644
--- a/app/Libraries/ViewComponents/Config/ViewComponents.php
+++ b/app/Libraries/ViewComponents/Config/ViewComponents.php
@@ -11,4 +11,14 @@ class ViewComponents extends BaseConfig
     public string $classComponentsPath = 'View/Components';
 
     public string $viewFileComponentsPath = 'Views/components';
+
+    /**
+     * Modules to look into for local components. Associative array with the module namespace as key and the module path
+     * as value.
+     *
+     * @var array<string, string>
+     */
+    public array $lookupModules = [];
+
+    public string $defaultLookupPath = APPPATH;
 }
-- 
GitLab