Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/Libraries/Storage/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author Arman Ag. <arman.ag@softberg.org>
* @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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
}
}
Expand Down
71 changes: 33 additions & 38 deletions src/Loader/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@
* @author Arman Ag. <arman.ag@softberg.org>
* @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;

/**
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
}
13 changes: 12 additions & 1 deletion tests/Unit/Loader/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Expand Down