added base FieldManager

This commit is contained in:
Taras Machyshyn
2014-03-28 11:49:06 +02:00
parent a19cb3608c
commit fc4f35cdd8
8 changed files with 249 additions and 64 deletions

View File

@@ -209,6 +209,13 @@ class Container
);
}
private function loadFieldManager()
{
return new \Espo\Core\Utils\FieldManager(
$this->get('metadata')
);
}
public function setUser($user)
{
$this->data['user'] = $user;

View File

@@ -0,0 +1,93 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
************************************************************************/
namespace Espo\Core\Utils;
class FieldManager
{
private $metadata;
protected $metadataType = 'entityDefs';
public function __construct(Metadata $metadata)
{
$this->metadata = $metadata;
}
protected function getMetadata()
{
return $this->metadata;
}
public function read($name, $scope)
{
return $this->getMetadata()->get($this->metadataType.'.'.$scope.'.fields.'.$name);
}
public function create($name, $fieldDef, $scope)
{
return $this->update($name, $fieldDef, $scope);
}
public function update($name, $fieldDef, $scope)
{
$defs = $this->normalizeDefs($name, $fieldDef);
return $this->setEntityDefs($defs, $scope);
}
public function delete($name, $scope)
{
$unsets = 'fields.'.$name;
return $this->getMetadata()->unsets($unsets, $this->metadataType, $scope);
}
/**
* Add all needed block for a field defenition
*
* @param string $fieldName
* @param array $fieldDef
* @return array
*/
protected function normalizeDefs($fieldName, array $fieldDef)
{
return array(
'fields' => array(
$fieldName => $fieldDef,
),
);
}
protected function setEntityDefs($defs, $scope)
{
$data = Json::encode($defs);
$result = $this->getMetadata()->set($data, $this->metadataType, $scope);
return $result;
}
}

View File

@@ -199,13 +199,13 @@ class Manager
*
* @return bool
*/
public function putContentsJSON($path, $data)
public function putContentsJson($path, $data)
{
if (!Utils\Json::isJSON($data)) {
$data = Utils\Json::encode($data);
$data = Utils\Json::encode($data, JSON_PRETTY_PRINT);
}
return $this->putContents($path, $data, JSON_PRETTY_PRINT);
return $this->putContents($path, $data);
}
/**
@@ -226,10 +226,10 @@ class Manager
$data= Utils\Util::merge($savedDataArray, $newDataArray);
if ($isJSON) {
$data= Utils\Json::encode($data);
$data= Utils\Json::encode($data, JSON_PRETTY_PRINT);
}
return $this->putContents($path, $data, JSON_PRETTY_PRINT);
return $this->putContents($path, $data);
}
/**
@@ -278,18 +278,23 @@ class Manager
*
* @param string | array $path
* @param array | string $unsets [description]
* @return [type] [description]
* @return bool
*/
public function unsetContents($path, $unsets)
public function unsetContents($path, $unsets, $isJSON = true)
{
$currentData = $this->getContents($path);
$currentDataArray= $this->getArrayData($currentData);
if (!is_array($currentDataArray)) {
if ($currentData == false) {
$GLOBALS['log']->notice('FileManager::unsetContents: File ['.$this->concatPaths($path).'] does not exist.');
return false;
}
$unsettedData = Utils\Util::unsetInArray($currentData, $unsets);
$currentDataArray = $this->getArrayData($currentData);
$unsettedData = Utils\Util::unsetInArray($currentDataArray, $unsets);
if ($isJSON) {
return $this->putContentsJson($path, $unsettedData);
}
return $this->putContents($path, $unsettedData);
}

View File

@@ -244,17 +244,35 @@ class Metadata
{
$path = $this->paths['customPath'];
if (file_exists($path)) {
$result = $this->getFileManager()->mergeContents(array($path, $type, $scope.'.json'), $data, true);
} else {
$result = $this->getFileManager()->putContentsJSON(array($path, $type, $scope.'.json'), $data);
}
$result = $this->getFileManager()->mergeContents(array($path, $type, $scope.'.json'), $data, true);
$this->init(true);
return $result;
}
/**
* Unset some fields and other stuff in metadat
*
* @param array | string $unsets Ex. 'fields.name'
* @param string $type Ex. 'entityDefs'
* @param string $scope
* @return bool
*/
public function unsets($unsets, $type, $scope)
{
$path = $this->paths['customPath'];
$result = $this->getFileManager()->unsetContents(array($path, $type, $scope.'.json'), $unsets, true);
if ($result == false) {
$GLOBALS['log']->warning('Metadata unsets available only for custom code.');
}
return $result;
}
public function getOrmMetadata($reload = false)
{
if (!empty($this->ormMeta) && !$reload) {

View File

@@ -0,0 +1,16 @@
{
"fields": {
"someName": {
"type": "varchar",
"maxLength": 40
},
"someName2": {
"type": "varchar",
"maxLength": 40
},
"some": {
"type": "varchar",
"maxLength": 40
}
}
}

View File

@@ -37,7 +37,7 @@ class ManagerTest extends \PHPUnit_Framework_TestCase
}
function testGetFileName()
public function testGetFileName()
{
$this->assertEquals('Donwload', $this->object->getFileName('Donwload.php'));
@@ -48,14 +48,14 @@ class ManagerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('Donwload', $this->object->getFileName('application/Espo/EntryPoints/Donwload.php'));
}
function testGetContents()
public function testGetContents()
{
$result = file_get_contents($this->filesPath.'/getContent/test.json');
$this->assertEquals($result, $this->object->getContents( array($this->filesPath, 'getContent/test.json') ));
}
function testPutContents()
public function testPutContents()
{
$testPath= $this->filesPath.'/setContent';
@@ -68,7 +68,7 @@ class ManagerTest extends \PHPUnit_Framework_TestCase
}
function testConcatPaths()
public function testConcatPaths()
{
$input = 'application/Espo/Resources/metadata/app/panel.json';
$result = 'application/Espo/Resources/metadata/app/panel.json';
@@ -105,7 +105,7 @@ class ManagerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($result, $this->reflection->invokeMethod('concatPaths', array($input)) );
}
function testGetDirName()
public function testGetDirName()
{
$input = 'data/logs/espo.log';
$result = 'logs';
@@ -133,7 +133,7 @@ class ManagerTest extends \PHPUnit_Framework_TestCase
}
function testGetDirNameFullPath()
public function testGetDirNameFullPath()
{
$input = 'data/logs/espo.log';
$result = 'data/logs';
@@ -160,6 +160,20 @@ class ManagerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($result, $this->object->getDirName($input));
}
public function testUnsetContents()
{
$testPath = $this->filesPath.'/unsets/test.json';
$initData = '{"fields":{"someName":{"type":"varchar","maxLength":40},"someName2":{"type":"varchar","maxLength":36}}}';
$this->object->putContents($testPath, $initData);
$unsets = 'fields.someName2';
$this->assertTrue($this->object->unsetContents($testPath, $unsets));
$result = '{"fields":{"someName":{"type":"varchar","maxLength":40}}}';
$this->assertJsonStringEqualsJsonFile($testPath, $result);
}
}

View File

@@ -356,6 +356,30 @@ class UtilTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($result, Util::unsetInArray($input, $unsets));
}
public function testUnsetInArrayByString()
{
$input = array(
'Account' => array(
'useCache' => true,
),
'Contact' => array(
'useCache' => true,
),
);
$unsets = 'Account.useCache';
$result = array(
'Account' => array(
),
'Contact' => array(
'useCache' => true,
),
);
$this->assertEquals($result, Util::unsetInArray($input, $unsets));
}
function testGetValueByKey()
{
$inputArray = array(

View File

@@ -0,0 +1,8 @@
{
"fields": {
"someName": {
"type": "varchar",
"maxLength": 40
}
}
}