refactoring

This commit is contained in:
Yuri Kuznetsov
2021-04-24 10:11:51 +03:00
parent a0ec26a755
commit 82c5e88c4a
3 changed files with 41 additions and 52 deletions
+7 -7
View File
@@ -78,6 +78,9 @@ class Layout
$this->user = $user;
}
/**
* @return array|object
*/
public function getOriginal(string $scope, string $name, ?string $setId = null)
{
$result = null;
@@ -91,8 +94,9 @@ class Layout
}
if (!$result) {
$result = $this->layout->get($scope, $name);
$result = json_decode($result);
$result = json_decode(
$this->layout->get($scope, $name)
);
}
if ($result === false) {
@@ -264,11 +268,7 @@ class Layout
$layoutManager->set($data, $scope, $name);
$result = $layoutManager->save();
if ($result === false) {
throw new Error("Error while saving layout.");
}
$layoutManager->save();
$this->dataManager->updateCacheTimestamp();
@@ -32,10 +32,13 @@ namespace Espo\Tools\LayoutManager;
use Espo\Core\{
Utils\File\Manager as FileManager,
Utils\Layout,
Utils\Util,
Utils\Json,
Exceptions\Error,
};
use const JSON_PRETTY_PRINT;
use const JSON_UNESCAPED_UNICODE;
class LayoutManager
{
protected $fileManager;
@@ -50,11 +53,6 @@ class LayoutManager
$this->layout = $layout;
}
protected function sanitizeInput($name)
{
return preg_replace("([\.]{2,})", '', $name);
}
/**
* Get layout in string format.
*/
@@ -65,20 +63,22 @@ class LayoutManager
/**
* Set layout data.
*
* @param mixed $data
*/
public function set($data, string $scope, string $name)
public function set($data, string $scope, string $name): void
{
$scope = $this->sanitizeInput($scope);
$name = $this->sanitizeInput($name);
if (empty($scope) || empty($name)) {
return false;
throw new Error("Error while setting layout.");
}
$this->changedData[$scope][$name] = $data;
}
public function resetToDefault(string $scope, string $name)
public function resetToDefault(string $scope, string $name): ?string
{
$scope = $this->sanitizeInput($scope);
$name = $this->sanitizeInput($name);
@@ -98,62 +98,53 @@ class LayoutManager
/**
* Save changes.
*
* @throws Error
*/
public function save()
public function save(): void
{
$result = true;
if (!empty($this->changedData)) {
foreach ($this->changedData as $scope => $rowData) {
foreach ($rowData as $layoutName => $layoutData) {
if (empty($scope) || empty($layoutName)) {
continue;
}
$layoutPath = $this->getDirPath($scope, true);
$data = Json::encode($layoutData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
if (empty($this->changedData)) {
return;
}
$result &= $this->fileManager->putContents(array($layoutPath, $layoutName.'.json'), $data);
foreach ($this->changedData as $scope => $rowData) {
foreach ($rowData as $layoutName => $layoutData) {
if (empty($scope) || empty($layoutName)) {
continue;
}
$layoutPath = $this->getDirPath($scope, true);
$data = Json::encode($layoutData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
$result &= $this->fileManager->putContents([$layoutPath, $layoutName.'.json'], $data);
}
}
if ($result == true) {
$this->clearChanges();
if (!$result) {
throw new Error("Error while saving layout.");
}
return (bool) $result;
$this->clearChanges();
}
/**
* Clear unsaved changes.
*/
public function clearChanges()
public function clearChanges(): void
{
$this->changedData = [];
}
/**
* Merge layout data.
* Ex. $scope= Account, $name= detail then will be created a file layoutFolder/Account/detail.json
*/
public function merge($data, string $scope, string $name)
{
$scope = $this->sanitizeInput($scope);
$name = $this->sanitizeInput($name);
$prevData = $this->get($scope, $name);
$prevDataArray = Json::getArrayData($prevData);
$dataArray = Json::getArrayData($data);
$data = Util::merge($prevDataArray, $dataArray);
$data = Json::encode($data);
return $this->set($data, $scope, $name);
}
protected function getDirPath(string $entityType, bool $isCustom = false): string
{
return $this->layout->getDirPath($entityType, $isCustom);
}
protected function sanitizeInput(string $name): string
{
return preg_replace("([\.]{2,})", '', $name);
}
}
-2
View File
@@ -37,12 +37,10 @@ class JsonTest extends \PHPUnit\Framework\TestCase
{
protected function setUp(): void
{
$GLOBALS['log'] = $this->getMockBuilder('\Espo\Core\Utils\Log')->disableOriginalConstructor()->getMock();
}
protected function tearDown() : void
{
$this->object = NULL;
}
public function testDecodeBad1()