diff --git a/src/Classes/AdminDirScanner.php b/src/Classes/AdminDirScanner.php new file mode 100644 index 00000000..95c1bddc --- /dev/null +++ b/src/Classes/AdminDirScanner.php @@ -0,0 +1,84 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace RobinTheHood\ModifiedModuleLoaderClient; + +use RobinTheHood\ModifiedModuleLoaderClient\Helpers\FileHelper; + +class AdminDirScanner +{ + /** + * @return string[] + */ + public function getAll(string $shopRootPath): array + { + $adminDirPaths = []; + $directoryPaths = FileHelper::scanDir($shopRootPath, FileHelper::DIRS_ONLY, false); + foreach ($directoryPaths as $directoryPath) { + if ($this->isAdminDirPath($directoryPath)) { + $adminDirPaths[] = $directoryPath; + } + } + return $adminDirPaths; + } + + private function isAdminDirPath(string $filePath): bool + { + if (!file_exists($filePath)) { + return false; + } + + if (!is_dir($filePath)) { + return false; + } + + $fileName = basename($filePath); + + if (!$this->isAdminDirName($fileName)) { + return false; + } + + if (!$this->hasAdminDirFiles($filePath)) { + return false; + } + + return true; + } + + private function isAdminDirName(string $fileName): bool + { + if ($fileName === 'admin') { + return true; + } + + if (strpos(strtolower($fileName), 'admin_') === 0) { + return true; + } + + return false; + } + + private function hasAdminDirFiles(string $filePath): bool + { + // List of Files, that the admin dire + $files = [ + 'check_update.php' + ]; + + if (!FileHelper::containsAllFiles($files, $filePath)) { + return false; + } + + return true; + } +} diff --git a/src/Classes/ShopInfo.php b/src/Classes/ShopInfo.php index 34943473..c4a8968a 100644 --- a/src/Classes/ShopInfo.php +++ b/src/Classes/ShopInfo.php @@ -13,6 +13,7 @@ namespace RobinTheHood\ModifiedModuleLoaderClient; +use Exception; use RobinTheHood\ModifiedModuleLoaderClient\App; use RobinTheHood\ModifiedModuleLoaderClient\Helpers\FileHelper; @@ -99,30 +100,17 @@ public static function getAdminDir(): string */ public static function scanForAdminDir(): string { - $resultDirectory = 'admin'; // Set default + $adminDirScanner = new AdminDirScanner(); + $adminDirPaths = $adminDirScanner->getAll(App::getShopRoot()); - $knownAdminPath = App::getShopRoot() . '/admin'; - - if (\file_exists($knownAdminPath) && \is_dir($knownAdminPath)) { - return $resultDirectory; + if (count($adminDirPaths) <= 0) { + throw new Exception("No valid admin directory found in " . App::getShopRoot()); } - $directorys = FileHelper::scanDir(App::getShopRoot(), FileHelper::DIRS_ONLY, false); - - // List of Files, that the admin dire - $files = [ - 'check_update.php' - ]; - - foreach ($directorys as $directory) { - if (!FileHelper::containsAllFiles($files, $directory)) { - continue; - } - - $resultDirectory = $directory; - break; + if (count($adminDirPaths) >= 2) { + throw new Exception("More than one valid admin directory found in " . App::getShopRoot()); } - return basename($resultDirectory); + return basename($adminDirPaths[0]); } }