refactoring
This commit is contained in:
@@ -30,7 +30,6 @@
|
||||
namespace Espo\Core\Utils;
|
||||
|
||||
use Espo\Core\{
|
||||
Exceptions\Error,
|
||||
Utils\Autoload\Loader,
|
||||
Utils\DataCache,
|
||||
Utils\File\Manager as FileManager,
|
||||
@@ -51,13 +50,21 @@ class Autoload
|
||||
];
|
||||
|
||||
protected $config;
|
||||
|
||||
protected $metadata;
|
||||
|
||||
protected $dataCache;
|
||||
|
||||
protected $fileManager;
|
||||
|
||||
protected $loader;
|
||||
|
||||
public function __construct(
|
||||
Config $config, Metadata $metadata, DataCache $dataCache, FileManager $fileManager, Loader $loader
|
||||
Config $config,
|
||||
Metadata $metadata,
|
||||
DataCache $dataCache,
|
||||
FileManager $fileManager,
|
||||
Loader $loader
|
||||
) {
|
||||
$this->config = $config;
|
||||
$this->metadata = $metadata;
|
||||
@@ -88,7 +95,7 @@ class Autoload
|
||||
$this->data = $this->loadData();
|
||||
|
||||
if ($useCache) {
|
||||
$result = $this->dataCache->store($this->cacheKey, $this->data);
|
||||
$this->dataCache->store($this->cacheKey, $this->data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,13 +122,7 @@ class Autoload
|
||||
|
||||
$content = $this->fileManager->getContents($filePath);
|
||||
|
||||
$arrayContent = Json::getArrayData($content);
|
||||
|
||||
if (empty($arrayContent)) {
|
||||
$GLOBALS['log']->error("Autoload: Empty file or syntax error in '{$filePath}'.");
|
||||
|
||||
return [];
|
||||
}
|
||||
$arrayContent = Json::decode($content, true);
|
||||
|
||||
return $this->normalizeData($arrayContent);
|
||||
}
|
||||
|
||||
@@ -400,8 +400,9 @@ class Manager
|
||||
throw new Error('FileManager: Failed to read file [' . $fullPath .'].');
|
||||
}
|
||||
|
||||
$savedDataArray = Json::getArrayData($fileContent);
|
||||
$newDataArray = Json::getArrayData($content);
|
||||
$savedDataArray = Json::decode($fileContent, true);
|
||||
|
||||
$newDataArray = Json::decode($content, true);
|
||||
|
||||
if (isset($removeOptions)) {
|
||||
$savedDataArray = Util::unsetInArray($savedDataArray, $removeOptions);
|
||||
@@ -462,7 +463,7 @@ class Manager
|
||||
return true;
|
||||
}
|
||||
|
||||
$currentDataArray = Json::getArrayData($currentData);
|
||||
$currentDataArray = Json::decode($currentData, true);
|
||||
|
||||
$unsettedData = Util::unsetInArray($currentDataArray, $unsets, true);
|
||||
|
||||
|
||||
@@ -170,7 +170,7 @@ class Unifier
|
||||
$unsets = Json::decode($fileContent);
|
||||
}
|
||||
else {
|
||||
$unsets = Json::getArrayData($fileContent);
|
||||
$unsets = Json::decode($fileContent, true);
|
||||
}
|
||||
|
||||
continue;
|
||||
|
||||
@@ -57,42 +57,4 @@ class Json
|
||||
{
|
||||
return json_decode($json, $associative, 512, JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the string is JSON
|
||||
*
|
||||
* @param string $json
|
||||
* @return bool
|
||||
*/
|
||||
public static function isJson(string $json): bool
|
||||
{
|
||||
try {
|
||||
self::decode($json);
|
||||
}
|
||||
catch (JsonException $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array data (if JSON convert to array).
|
||||
*
|
||||
* @param mixed $data.
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public static function getArrayData($data, ?array $returns = []): ?array
|
||||
{
|
||||
if (is_array($data)) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
if (self::isJson($data)) {
|
||||
return self::decode($data, true);
|
||||
}
|
||||
|
||||
return $returns;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,6 @@
|
||||
namespace Espo\Core\Utils;
|
||||
|
||||
use Espo\Core\{
|
||||
Exceptions\Error,
|
||||
Utils\Config,
|
||||
Utils\Metadata,
|
||||
Utils\File\Manager as FileManager,
|
||||
@@ -50,12 +49,19 @@ class Route
|
||||
];
|
||||
|
||||
private $config;
|
||||
|
||||
private $metadata;
|
||||
|
||||
private $fileManager;
|
||||
|
||||
private $dataCache;
|
||||
|
||||
public function __construct(Config $config, Metadata $metadata, FileManager $fileManager, DataCache $dataCache)
|
||||
{
|
||||
public function __construct(
|
||||
Config $config,
|
||||
Metadata $metadata,
|
||||
FileManager $fileManager,
|
||||
DataCache $dataCache
|
||||
) {
|
||||
$this->config = $config;
|
||||
$this->metadata = $metadata;
|
||||
$this->fileManager = $fileManager;
|
||||
@@ -122,13 +128,7 @@ class Route
|
||||
|
||||
$content = $this->fileManager->getContents($routeFile);
|
||||
|
||||
$data = Json::getArrayData($content);
|
||||
|
||||
if (empty($data)) {
|
||||
$GLOBALS['log']->warning("Route: No data or syntax error in '{$routeFile}'.");
|
||||
|
||||
return $currentData;
|
||||
}
|
||||
$data = Json::decode($content, true);
|
||||
|
||||
return $this->appendRoutesToData($currentData, $data);
|
||||
}
|
||||
@@ -209,7 +209,7 @@ class Route
|
||||
static protected function isRouteInList(array $newRoute, array $routeList): bool
|
||||
{
|
||||
foreach ($routeList as $route) {
|
||||
if (Util::isEquals($route, $newRoute)) {
|
||||
if (Util::areEqual($route, $newRoute)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,6 @@ class Util
|
||||
|
||||
protected static $reservedWordList = ['Case'];
|
||||
|
||||
|
||||
/**
|
||||
* Get a folder separator.
|
||||
*
|
||||
@@ -507,17 +506,18 @@ class Util
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if two variables are equals.
|
||||
* Check if two variables are equal.
|
||||
*
|
||||
* @param mixed $var1
|
||||
* @param mixed $var2
|
||||
* @param mixed $var1
|
||||
* @param mixed $var2
|
||||
* @return boolean
|
||||
*/
|
||||
public static function isEquals($var1, $var2)
|
||||
public static function areEqual($var1, $var2): bool
|
||||
{
|
||||
if (is_array($var1)) {
|
||||
static::ksortRecursive($var1);
|
||||
}
|
||||
|
||||
if (is_array($var2)) {
|
||||
static::ksortRecursive($var2);
|
||||
}
|
||||
|
||||
@@ -69,23 +69,4 @@ class JsonTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
$this->assertEquals('data/logs', Json::decode($test)->folder);
|
||||
}
|
||||
|
||||
public function testIsJSON()
|
||||
{
|
||||
$this->assertTrue(Json::isJSON('{"database":{"driver":"pdo_mysql","host":"localhost"},"devMode":true}'));
|
||||
|
||||
$this->assertTrue(Json::isJSON('[]'));
|
||||
|
||||
$this->assertTrue(Json::isJSON('{}'));
|
||||
|
||||
$this->assertTrue(Json::isJSON('true'));
|
||||
|
||||
$this->assertFalse(Json::isJSON('some string'));
|
||||
|
||||
$this->assertTrue(Json::isJSON(true));
|
||||
$this->assertEquals('true', json_encode(true));
|
||||
|
||||
$this->assertFalse(Json::isJSON(false));
|
||||
$this->assertEquals('false', json_encode(false));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user