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
29 changes: 5 additions & 24 deletions src/Environment/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +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.5
* @since 2.9.6
*/

namespace Quantum\Environment;

use Quantum\Libraries\Storage\Factories\FileSystemFactory;
use Quantum\Environment\Exceptions\EnvException;
use Quantum\Libraries\Storage\FileSystem;
use Quantum\Di\Exceptions\DiException;
use Quantum\Exceptions\BaseException;
use Quantum\Loader\Loader;
Expand All @@ -34,12 +32,6 @@
class Environment
{

/**
* FileSystem instance
* @var FileSystem
*/
private $fs;

/**
* Environment file
* @var string
Expand All @@ -65,14 +57,6 @@ class Environment
*/
private static $instance = null;

/**
* @throws BaseException
*/
private function __construct()
{
$this->fs = FileSystemFactory::get();
}

/**
* GetInstance
* @return Environment
Expand Down Expand Up @@ -197,14 +181,11 @@ public function updateRow(string $key, ?string $value)
$envFilePath = App::getBaseDir() . DS . $this->envFile;

if ($row) {
$this->fs->put($envFilePath, preg_replace(
'/^' . $row . '/m',
$key . "=" . $value,
$this->fs->get($envFilePath)
)
);
$envFileContent = file_get_contents($envFilePath);
$envFileContent = preg_replace('/^' . preg_quote($row, '/') . '/m', $key . "=" . $value, $envFileContent);
file_put_contents($envFilePath, $envFileContent);
} else {
$this->fs->append($envFilePath, PHP_EOL . $key . "=" . $value . PHP_EOL);
file_put_contents($envFilePath, PHP_EOL . $key . "=" . $value . PHP_EOL, FILE_APPEND);
}

$this->envContent = Dotenv::createMutable(App::getBaseDir(), $this->envFile)->load();
Expand Down
10 changes: 9 additions & 1 deletion src/Libraries/Storage/Exceptions/FileSystemException.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.5
* @since 2.9.6
*/

namespace Quantum\Libraries\Storage\Exceptions;
Expand Down Expand Up @@ -48,4 +48,12 @@ public static function fileAlreadyExists(): FileSystemException
{
return new static(t('exception.file_already_exists'), E_WARNING);
}

/**
* @return FileSystemException
*/
public static function incorrectTokenService(): FileSystemException
{
return new static(t('exception.incorrect_auth_service'), E_WARNING);
}
}
112 changes: 98 additions & 14 deletions src/Libraries/Storage/Factories/FileSystemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,28 @@
* @author Arman Ag. <arman.ag@softberg.org>
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
* @link http://quantum.softberg.org/
* @since 2.9.5
* @since 2.9.6
*/

namespace Quantum\Libraries\Storage\Factories;

use Quantum\Libraries\Storage\Adapters\GoogleDrive\GoogleDriveFileSystemAdapter;
use Quantum\Libraries\Storage\Adapters\Dropbox\DropboxFileSystemAdapter;
use Quantum\Libraries\Storage\Adapters\Local\LocalFileSystemAdapter;
use Quantum\Libraries\Storage\Adapters\GoogleDrive\GoogleDriveApp;
use Quantum\Libraries\Storage\Contracts\TokenServiceInterface;
use Quantum\Libraries\Storage\Exceptions\FileSystemException;
use Quantum\Libraries\Storage\Adapters\Dropbox\DropboxApp;
use Quantum\Libraries\Storage\Contracts\CloudAppInterface;
use Quantum\Libraries\Config\Exceptions\ConfigException;
use Quantum\Libraries\HttpClient\HttpClient;
use Quantum\Libraries\Storage\FileSystem;
use Quantum\Exceptions\ServiceException;
use Quantum\Di\Exceptions\DiException;
use Quantum\Exceptions\BaseException;
use Quantum\Factory\ServiceFactory;
use Quantum\Loader\Setup;
use ReflectionException;

/**
* Class FileSystemFactory
Expand All @@ -38,40 +48,114 @@ class FileSystemFactory
FileSystem::GDRIVE => GoogleDriveFileSystemAdapter::class,
];

/**
* Supported apps
*/
const APPS = [
FileSystem::DROPBOX => DropboxApp::class,
FileSystem::GDRIVE => GoogleDriveApp::class,
];

/**
* @var array
*/
private static $instances = [];

/**
* @param string $type
* @param CloudAppInterface|null $cloudApp
* @param string|null $adapter
* @return FileSystem
* @throws BaseException
* @throws DiException
* @throws ReflectionException
* @throws ConfigException
*/
public static function get(string $type = FileSystem::LOCAL, ?CloudAppInterface $cloudApp = null): FileSystem
public static function get(?string $adapter = null): FileSystem
{
if (!isset(self::$instances[$type])) {
self::$instances[$type] = self::createInstance($type, $cloudApp);
if (!config()->has('fs')) {
config()->import(new Setup('Config', 'fs'));
}

$adapter = $adapter ?? config()->get('fs.default');

$adapterClass = self::getAdapterClass($adapter);

if (!isset(self::$instances[$adapter])) {
self::$instances[$adapter] = self::createInstance($adapterClass, $adapter);
}

return self::$instances[$type];
return self::$instances[$adapter];
}

/**
* @param string $type
* @param CloudAppInterface|null $cloudApp
* @param string $adapterClass
* @param string $adapter
* @return FileSystem
* @throws DiException
* @throws FileSystemException
* @throws ReflectionException
* @throws ServiceException
*/
private static function createInstance(string $adapterClass, string $adapter): FileSystem
{
return new FileSystem(new $adapterClass(
self::createCloudApp($adapter)
));
}

/**
* @param string $adapter
* @return string
* @throws BaseException
*/
private static function createInstance(string $type, ?CloudAppInterface $cloudApp = null): FileSystem
private static function getAdapterClass(string $adapter): string
{
if (!array_key_exists($adapter, self::ADAPTERS)) {
throw FileSystemException::adapterNotSupported($adapter);
}

return self::ADAPTERS[$adapter];
}

/**
* @param string $adapter
* @return CloudAppInterface|null
* @throws DiException
* @throws FileSystemException
* @throws ReflectionException
* @throws ServiceException
*/
private static function createCloudApp(string $adapter): ?CloudAppInterface
{
if (!isset(self::ADAPTERS[$type])) {
throw FileSystemException::adapterNotSupported($type);
if ($adapter === FileSystem::LOCAL || !isset(self::APPS[$adapter])) {
return null;
}

$adapterClass = self::ADAPTERS[$type];
$cloudAppClass = self::APPS[$adapter];

return new $cloudAppClass(
config()->get('fs.' . $adapter . '.params.app_key'),
config()->get('fs.' . $adapter . '.params.app_secret'),
self::createTokenService($adapter),
new HttpClient()
);
}

/**
* @param string $adapter
* @return TokenServiceInterface
* @throws FileSystemException
* @throws DiException
* @throws ServiceException
* @throws ReflectionException
*/
private static function createTokenService(string $adapter): TokenServiceInterface
{
$tokenService = ServiceFactory::create(config()->get('fs.' . $adapter . '.service'));

if (!$tokenService instanceof TokenServiceInterface) {
throw FileSystemException::incorrectTokenService();
}

return new FileSystem(new $adapterClass($cloudApp));
return $tokenService;
}
}
37 changes: 37 additions & 0 deletions src/Libraries/Storage/Helpers/fs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* Quantum PHP Framework
*
* An open source software development framework for PHP
*
* @package Quantum
* @author Arman Ag. <arman.ag@softberg.org>
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
* @link http://quantum.softberg.org/
* @since 2.9.6
*/

use Quantum\Libraries\Storage\Factories\FileSystemFactory;
use Quantum\Libraries\Config\Exceptions\ConfigException;
use Quantum\Libraries\Auth\Exceptions\AuthException;
use Quantum\Libraries\Storage\FileSystem;
use Quantum\Exceptions\ServiceException;
use Quantum\Di\Exceptions\DiException;
use Quantum\Exceptions\BaseException;

/**
* Gets the FileSystem handler
* @param string|null $adapter
* @return FileSystem
* @throws AuthException
* @throws BaseException
* @throws ConfigException
* @throws DiException
* @throws ReflectionException
* @throws ServiceException
*/
function fs(?string $adapter = null): FileSystem
{
return FileSystemFactory::get($adapter);
}
7 changes: 2 additions & 5 deletions src/Loader/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
* @author Arman Ag. <arman.ag@softberg.org>
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
* @link http://quantum.softberg.org/
* @since 2.9.5
* @since 2.9.6
*/

namespace Quantum\Loader;

use Quantum\Libraries\Storage\Factories\FileSystemFactory;
use Quantum\Loader\Exceptions\LoaderException;
use Quantum\Exceptions\BaseException;
use ReflectionException;
Expand Down Expand Up @@ -157,9 +156,7 @@ public function loadClassFromFile(
callable $notDefinedException,
array $constructorArgs = []
) {
$fs = FileSystemFactory::get();

if (!$fs->exists($filePath)) {
if (!file_exists($filePath)) {
throw $notFoundException();
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/AppTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ abstract class AppTestCase extends TestCase

public function setUp(): void
{
$this->fs = FileSystemFactory::get();

AppFactory::create(App::WEB, PROJECT_ROOT);

Config::getInstance()->flush();
Expand All @@ -30,6 +28,8 @@ public function setUp(): void

Config::getInstance()
->load(new Setup('config', 'config'));

$this->fs = FileSystemFactory::get();
}

protected function setPrivateProperty($object, $property, $value): void
Expand Down
Loading