From 584328c85b313f40463fd6dd54b108b04d67c2e9 Mon Sep 17 00:00:00 2001 From: Arman <407448+armanist@users.noreply.github.com> Date: Fri, 19 Dec 2025 16:15:17 +0400 Subject: [PATCH] Adding new `fileExists()` method do Loader and handling UploadedFile config load gracefully --- src/Libraries/Storage/UploadedFile.php | 30 +++++++---- src/Loader/Loader.php | 71 ++++++++++++-------------- tests/Unit/Loader/LoaderTest.php | 13 ++++- 3 files changed, 66 insertions(+), 48 deletions(-) diff --git a/src/Libraries/Storage/UploadedFile.php b/src/Libraries/Storage/UploadedFile.php index 2038ca05f..919be58df 100644 --- a/src/Libraries/Storage/UploadedFile.php +++ b/src/Libraries/Storage/UploadedFile.php @@ -9,7 +9,7 @@ * @author Arman Ag. * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.8 + * @since 2.9.9 */ namespace Quantum\Libraries\Storage; @@ -23,10 +23,12 @@ use Quantum\Config\Exceptions\ConfigException; use Quantum\App\Exceptions\BaseException; use Quantum\Di\Exceptions\DiException; -use Quantum\Loader\Setup; use Gumlet\ImageResizeException; +use Quantum\Loader\Loader; +use Quantum\Loader\Setup; use ReflectionException; use Gumlet\ImageResize; +use Quantum\Di\Di; use SplFileInfo; use finfo; @@ -406,22 +408,32 @@ protected function allowed(string $extension, string $mimeType): bool /** * Loads allowed mime types from config (shared/config/uploads.php) if present. - * @return void + * @throws ConfigException + * @throws DiException * @throws FileUploadException + * @throws ReflectionException */ protected function loadAllowedMimeTypesFromConfig(): void { - if (!config()->has('uploads')) { - config()->import(new Setup('config', 'uploads')); - } + if (!config()->has('uploads')) { + $loader = Di::get(Loader::class); + $setup = new Setup('config', 'uploads'); + $loader->setup($setup); + + if (!$loader->fileExists()) { + return; + } + + config()->import($setup); + } - $allowedMimeTypesMap = config()->get('uploads.allowed_mime_types'); + $allowedMimeTypesMap = config()->get('uploads.allowed_mime_types') ?? []; - if ($allowedMimeTypesMap !== null && !is_array($allowedMimeTypesMap)) { + if (!is_array($allowedMimeTypesMap)) { throw FileUploadException::incorrectMimeTypesConfig('uploads'); } - if (is_array($allowedMimeTypesMap)) { + if ($allowedMimeTypesMap) { $this->setAllowedMimeTypesMap($allowedMimeTypesMap); } } diff --git a/src/Loader/Loader.php b/src/Loader/Loader.php index 55953e48b..a7b2ad151 100644 --- a/src/Loader/Loader.php +++ b/src/Loader/Loader.php @@ -9,15 +9,12 @@ * @author Arman Ag. * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.7 + * @since 2.9.9 */ namespace Quantum\Loader; use Quantum\Loader\Exceptions\LoaderException; -use Quantum\App\Exceptions\BaseException; -use ReflectionException; -use ReflectionClass; use Quantum\App\App; /** @@ -107,23 +104,33 @@ public function load() } /** - * Gets the file path - * @return string - * @throws LoaderException + * Checks if the setup points to valid file + * @return bool */ - public function getFilePath(): string + public function fileExists(): bool { - $filePath = ''; + $filePath = $this->resolveFilePath(); - if ($this->module) { - $filePath = modules_dir() . DS . $this->module . DS; + if (file_exists($filePath)) { + return true; } - if ($this->pathPrefix) { - $filePath .= $this->pathPrefix . DS; + if ($this->hierarchical) { + $filePath = App::getBaseDir() . DS . 'shared' . DS . strtolower($this->pathPrefix) . DS . $this->fileName . '.php'; + return file_exists($filePath); } - $filePath .= $this->fileName . '.php'; + return false; + } + + /** + * Gets the file path + * @return string + * @throws LoaderException + */ + public function getFilePath(): string + { + $filePath = $this->resolveFilePath(); if (!file_exists($filePath)) { if ($this->hierarchical) { @@ -141,34 +148,22 @@ public function getFilePath(): string } /** - * Loads namespaced class from the file - * @param string $filePath - * @param callable $notFoundException - * @param callable $notDefinedException - * @param array $constructorArgs - * @return mixed - * @throws BaseException - * @throws ReflectionException + * @return string */ - public function loadClassFromFile( - string $filePath, - callable $notFoundException, - callable $notDefinedException, - array $constructorArgs = [] - ) { - if (!file_exists($filePath)) { - throw $notFoundException(); - } + private function resolveFilePath(): string + { + $filePath = ''; - require_once $filePath; + if ($this->module) { + $filePath = modules_dir() . DS . $this->module . DS; + } - foreach (get_declared_classes() as $className) { - $reflectionClass = new ReflectionClass($className); - if ($reflectionClass->getFileName() === $filePath) { - return new $className(...$constructorArgs); - } + if ($this->pathPrefix) { + $filePath .= $this->pathPrefix . DS; } - throw $notDefinedException(); + $filePath .= $this->fileName . '.php'; + + return $filePath; } } \ No newline at end of file diff --git a/tests/Unit/Loader/LoaderTest.php b/tests/Unit/Loader/LoaderTest.php index 1af46eb36..a87bbb474 100644 --- a/tests/Unit/Loader/LoaderTest.php +++ b/tests/Unit/Loader/LoaderTest.php @@ -18,7 +18,18 @@ public function setUp(): void $this->loader = new Loader(); } - public function testSetupAndLoad() + public function testLoaderFileExists() + { + $this->loader->setup(new Setup('config', 'app')); + + $this->assertTrue($this->loader->fileExists()); + + $this->loader->setup(new Setup('config', 'unknown')); + + $this->assertFalse($this->loader->fileExists()); + } + + public function testLoaderSetupAndLoad() { $this->loader->setup(new Setup('config', 'app'));