changes: config, metadata

This commit is contained in:
Taras Machyshyn
2014-02-17 18:42:31 +02:00
parent fcce1d03cc
commit 1c08658653
8 changed files with 159 additions and 225 deletions

View File

@@ -133,30 +133,17 @@ class Container
{
return new \Espo\Core\Utils\Metadata(
$this->get('config'),
$this->get('fileManager'),
$this->get('uniteFiles')
$this->get('fileManager')
);
}
private function loadLayout()
{
return new \Espo\Core\Utils\Layout(
$this->get('config'),
return new \Espo\Core\Utils\Layout(
$this->get('fileManager'),
$this->get('metadata')
);
}
private function loadUniteFiles()
{
return new \Espo\Core\Utils\File\UniteFiles(
$this->get('fileManager'),
array(
'unsetFileName' => $this->get('config')->get('unsetFileName'),
'defaultsPath' => $this->get('config')->get('defaultsPath'),
)
);
}
private function loadAcl()
{

View File

@@ -70,15 +70,19 @@ class ClassParser
if ($cacheFile && file_exists($cacheFile) && $this->getConfig()->get('useCache')) {
$data = $this->getFileManager()->getContents($cacheFile);
} else {
$data = $this->getClassNameHash( $this->getInitPaths($paths) );
$data = $this->getClassNameHash($paths['corePath']);
if (isset($paths['modulePath'])) {
foreach ($this->getMetadata()->getModuleList() as $moduleName) {
$path = str_replace('{*}', $moduleName, $paths['modulePath']);
$data = array_merge($data, $this->getClassNameHash(array($path)));
$data = array_merge($data, $this->getClassNameHash($path));
}
}
if (isset($paths['customPath'])) {
$data = array_merge($data, $this->getClassNameHash($paths['customPath']));
}
if ($cacheFile && $this->getConfig()->get('useCache')) {
$result = $this->getFileManager()->putContentsPHP($cacheFile, $data);
@@ -92,8 +96,12 @@ class ClassParser
}
protected function getClassNameHash(array $dirs)
protected function getClassNameHash($dirs)
{
if (is_string($dirs)) {
$dirs = (array) $dirs;
}
$data = array();
foreach ($dirs as $dir) {
if (file_exists($dir)) {
@@ -114,29 +122,8 @@ class ClassParser
}
}
}
return $data;
}
/**
* Get init paths like corePath and/or customPath
* @param array $paths
* @return array
*/
protected function getInitPaths(array $paths)
{
$allowedInitPath = array(
'corePath',
'customPath',
);
$initPaths = array();
foreach ($allowedInitPath as $pathName) {
if (isset($paths[$pathName])) {
$initPaths[] = $paths[$pathName];
}
}
return $initPaths;
}
}

View File

@@ -7,12 +7,15 @@ use Espo\Core\Utils;
class UniteFiles
{
private $fileManager;
private $params;
public function __construct(\Espo\Core\Utils\File\Manager $fileManager, array $params)
protected $params = array(
'unsetFileName' => 'unset.json',
'defaultsPath' => 'application/Espo/Core/defaults',
);
public function __construct(\Espo\Core\Utils\File\Manager $fileManager)
{
$this->fileManager = $fileManager;
$this->params = $params;
$this->fileManager = $fileManager;
}
@@ -21,23 +24,16 @@ class UniteFiles
return $this->fileManager;
}
protected function getParams()
{
return $this->params;
}
/**
* Unite file content to the file
*
* @param string $configParams - array('name', 'cachePath', 'corePath', 'customPath')
* @param bool $recursively - Note: only for first level of sub directory, other levels of sub directories will be ignored
*
* @return array
*/
public function uniteFiles($configParams, $recursively=false)
* Unite file content to the file
*
* @param string $configParams - array('name', 'cachePath', 'corePath', 'customPath')
* @param bool $recursively - Note: only for first level of sub directory, other levels of sub directories will be ignored
*
* @return array
*/
public function uniteFiles($configParams, $recursively = false)
{
//EXAMPLE OF IMPLEMENTATION IN METADATA CLASS
/*if (empty($configParams) || empty($configParams['name']) || empty($configParams['cachePath']) || empty($configParams['corePath'])) {
@@ -71,23 +67,21 @@ class UniteFiles
}
/**
* Unite file content to the file for one directory [NOW ONLY FOR METADATA, NEED TO CHECK FOR LAYOUTS AND OTHERS]
*
* @param string $dirPath
* @param string $type - name of type array("metadata", "layouts"), ex. metadataConfig['name']
* @param bool $recursively - Note: only for first level of sub directory, other levels of sub directories will be ignored
* @param string $moduleName - name of module if exists
*
* @return string - content of the files
*/
public function uniteFilesSingle($dirPath, $type, $recursively=false, $moduleName= '')
* Unite file content to the file for one directory [NOW ONLY FOR METADATA, NEED TO CHECK FOR LAYOUTS AND OTHERS]
*
* @param string $dirPath
* @param string $type - name of type array("metadata", "layouts"), ex. $this->name
* @param bool $recursively - Note: only for first level of sub directory, other levels of sub directories will be ignored
* @param string $moduleName - name of module if exists
*
* @return string - content of the files
*/
public function uniteFilesSingle($dirPath, $type, $recursively = false, $moduleName = '')
{
if (empty($dirPath) || !file_exists($dirPath)) {
return false;
}
$params = $this->getParams();
$unsetFileName = $params['unsetFileName'];
//$unsetFileName = $this->getConfig('unsetFileName');
$unsetFileName = $this->params['unsetFileName'];
//get matadata files
$fileList = $this->getFileManager()->getFileList($dirPath, $recursively, '\.json$');
@@ -132,13 +126,13 @@ class UniteFiles
}
/**
* Helpful method for get content from files for unite Files
*
* @param string | array $paths
* @param string | array() $defaults - It can be a string like ["metadata","layouts"] OR an array with default values
*
* @return array
*/
* Helpful method for get content from files for unite Files
*
* @param string | array $paths
* @param string | array() $defaults - It can be a string like ["metadata","layouts"] OR an array with default values
*
* @return array
*/
public function uniteFilesGetContent($paths, $defaults)
{
$fileContent= $this->getFileManager()->getContents($paths);
@@ -166,17 +160,16 @@ class UniteFiles
}
/**
* Load default values for selected type [metadata, layouts]
*
* @param string $name
* @param string $type - [metadata, layouts]
*
* @return array
*/
* Load default values for selected type [metadata, layouts]
*
* @param string $name
* @param string $type - [metadata, layouts]
*
* @return array
*/
function loadDefaultValues($name, $type='metadata')
{
$params = $this->getParams();
$defaultPath= $params['defaultsPath'];
$defaultPath= $this->params['defaultsPath'];
$defaultValue= $this->getFileManager()->getContents( array($defaultPath, $type, $name.'.json') );
if ($defaultValue!==false) {

View File

@@ -4,44 +4,35 @@ namespace Espo\Core\Utils;
class Layout
{
private $config;
private $fileManager;
private $metadata;
/**
* @var string - uses for loading default values
*/
private $name = 'layout';
private $name = 'layout';
protected $params = array(
'defaultsPath' => 'application/Espo/Core/defaults',
);
/**
* @var array - path to layout files
*/
private $paths = array(
'corePath' => 'application/Espo/Resources/layouts',
'modulePath' => 'application/Espo/Modules/{*}/Resources/layouts',
);
'modulePath' => 'application/Espo/Modules/{*}/Resources/layouts',
'customPath' => 'application/Espo/Custom/Resources/layouts',
);
/**
* @var array - path to layout files in custom folder
*/
private $customPaths = array(
'corePath' => 'application/Espo/Custom/Resources/layouts',
'modulePath' => 'application/Espo/Custom/Modules/{*}/Resources/layouts',
);
public function __construct(\Espo\Core\Utils\Config $config, \Espo\Core\Utils\File\Manager $fileManager, \Espo\Core\Utils\Metadata $metadata)
public function __construct(\Espo\Core\Utils\File\Manager $fileManager, \Espo\Core\Utils\Metadata $metadata)
{
$this->config = $config;
$this->fileManager = $fileManager;
$this->metadata = $metadata;
}
protected function getConfig()
{
return $this->config;
}
protected function getFileManager()
{
return $this->fileManager;
@@ -61,7 +52,7 @@ class Layout
*
* @return json
*/
function get($controller, $name)
public function get($controller, $name)
{
$fileFullPath = Util::concatPath($this->getLayoutPath($controller, true), $name.'.json');
if (!file_exists($fileFullPath)) {
@@ -70,7 +61,7 @@ class Layout
if (!file_exists($fileFullPath)) {
//load defaults
$defaultPath = $this->getConfig()->get('defaultsPath');
$defaultPath = $this->params['defaultsPath'];
$fileFullPath = Util::concatPath( Util::concatPath($defaultPath, $this->name), $name.'.json' );
//END: load defaults
@@ -93,7 +84,7 @@ class Layout
*
* @return bool
*/
function set($data, $controller, $name)
public function set($data, $controller, $name)
{
if (empty($controller) || empty($name)) {
return false;
@@ -119,7 +110,7 @@ class Layout
*
* @return bool
*/
function merge($data, $controller, $name)
public function merge($data, $controller, $name)
{
$prevData = $this->get($controller, $name);
@@ -140,16 +131,19 @@ class Layout
*
* @return string
*/
public function getLayoutPath($entityName, $isCustom = false)
{
$paths = $isCustom ? $this->customPaths : $this->paths;
$moduleName = $this->getMetadata()->getScopeModuleName($entityName);
protected function getLayoutPath($entityName, $isCustom = false)
{
$path = $this->paths['customPath'];
if (!$isCustom) {
$moduleName = $this->getMetadata()->getScopeModuleName($entityName);
$path = $paths['corePath'];
if ($moduleName !== false) {
$path = str_replace('{*}', $moduleName, $paths['modulePath']);
}
$path = $this->paths['corePath'];
if ($moduleName !== false) {
$path = str_replace('{*}', $moduleName, $this->paths['modulePath']);
}
}
$path = Util::concatPath($path, $entityName);
return $path;

View File

@@ -4,11 +4,7 @@ namespace Espo\Core\Utils;
class Metadata
{
protected $metadataConfig;
protected $meta;
private $espoMetadata;
protected $meta;
protected $scopes = array();
@@ -16,16 +12,37 @@ class Metadata
private $uniteFiles;
private $fileManager;
private $converter;
/**
* @var string - uses for loading default values
*/
private $name = 'metadata';
private $cacheFile = 'data/cache/application/metadata.php';
private $paths = array(
'corePath' => 'application/Espo/Resources/metadata',
'modulePath' => 'application/Espo/Modules/{*}/Resources/metadata',
'customPath' => 'application/Espo/Custom/Resources/metadata',
);
protected $ormMeta = null;
private $ormCacheFile = 'data/cache/application/ormMetadata.php';
private $moduleList = null;
public function __construct(\Espo\Core\Utils\Config $config, \Espo\Core\Utils\File\Manager $fileManager, \Espo\Core\Utils\File\UniteFiles $uniteFiles)
public function __construct(\Espo\Core\Utils\Config $config, \Espo\Core\Utils\File\Manager $fileManager)
{
$this->config = $config;
$this->uniteFiles = $uniteFiles;
$this->fileManager = $fileManager;
$this->converter = new \Espo\Core\Utils\Database\Converter($this, $this->fileManager);
$this->uniteFiles = new \Espo\Core\Utils\File\UniteFiles($this->fileManager);
$this->converter = new \Espo\Core\Utils\Database\Converter($this, $this->fileManager);
$this->init(!$this->isCached());
}
@@ -57,9 +74,8 @@ class Metadata
if (!$this->getConfig()->get('useCache')) {
return false;
}
$metaConfig = $this->getMetaConfig();
if (file_exists($metaConfig['metadataCacheFile'])) {
if (file_exists($this->cacheFile)) {
return true;
}
@@ -77,9 +93,8 @@ class Metadata
$this->meta = $data;
if ($reload) {
//save medatada to a cache file
$metaConfig = $this->getMetaConfig();
$isSaved = $this->getFileManager()->putContentsPHP($metaConfig['metadataCacheFile'], $data);
//save medatada to a cache file
$isSaved = $this->getFileManager()->putContentsPHP($this->cacheFile, $data);
if ($isSaved === false) {
$GLOBALS['log']->add('FATAL', 'Metadata:init() - metadata has not been saved to a cache file');
}
@@ -148,18 +163,16 @@ class Metadata
*/
public function getMetadataOnly($isJSON = true, $reload = false)
{
$metaConfig = $this->getMetaConfig();
$data = false;
if (!file_exists($metaConfig['metadataCacheFile']) || $reload) {
$data = $this->uniteFiles($metaConfig, true);
if (!file_exists($this->cacheFile) || $reload) {
$data = $this->uniteFiles();
if ($data === false) {
$GLOBALS['log']->add('FATAL', 'Metadata:getMetadata() - metadata unite file cannot be created');
}
}
else if (file_exists($metaConfig['metadataCacheFile'])) {
$data = $this->getFileManager()->getContents($metaConfig['metadataCacheFile']);
else if (file_exists($this->cacheFile)) {
$data = $this->getFileManager()->getContents($this->cacheFile);
}
if ($isJSON) {
@@ -183,13 +196,11 @@ class Metadata
*/
public function set($data, $type, $scope)
{
$metaConfig = $this->getMetaConfig();
$fullPath = $metaConfig['corePath'];
$fullPath = $this->paths['corePath'];
$moduleName = $this->getScopeModuleName($scope);
if ($moduleName !== false) {
$fullPath = str_replace('{*}', $moduleName, $metaConfig['customPath']);
$fullPath = str_replace('{*}', $moduleName, $this->paths['modulePath']);
}
$fullPath = Util::concatPath($fullPath, $type);
@@ -209,34 +220,28 @@ class Metadata
public function getOrmMetadata()
{
if (!empty($this->espoMetadata)) {
return $this->espoMetadata;
if (!empty($this->ormMeta)) {
return $this->ormMeta;
}
$metaConfig = $this->getMetaConfig();
$espoMetadataFile = Util::concatPath($metaConfig['cachePath'], 'ormMetadata.php');
if (!file_exists($espoMetadataFile) || !$this->getConfig()->get('useCache')) {
if (!file_exists($this->ormCacheFile) || !$this->getConfig()->get('useCache')) {
$this->getConverter()->process();
}
$this->espoMetadata = $this->getFileManager()->getContents($espoMetadataFile);
$this->ormMeta = $this->getFileManager()->getContents($this->ormCacheFile);
return $this->espoMetadata;
return $this->ormMeta;
}
public function setOrmMetadata(array $espoMetadata)
public function setOrmMetadata(array $ormMeta)
{
$metaConfig = $this->getMetaConfig();
$result = $this->getFileManager()->putContentsPHP(array($metaConfig['cachePath'], 'ormMetadata.php'), $espoMetadata);
$result = $this->getFileManager()->putContentsPHP($this->ormCacheFile, $ormMeta);
if ($result == false) {
$GLOBALS['log']->add('EXCEPTION', 'Metadata::setOrmMetadata() - Cannot save ormMetadata to a file');
throw new \Espo\Core\Exceptions\Error();
}
$this->espoMetadata = $espoMetadata;
$this->ormMeta = $ormMeta;
return $result;
}
@@ -245,30 +250,28 @@ class Metadata
/**
* Unite file content to the file
*
* @param string $configParams - array("name", "cachePath", "corePath", "customPath")
* @param bool $recursively - Note: only for first level of sub directory, other levels of sub directories will be ignored
*
* @return array
*/
function uniteFiles($configParams, $recursively = false)
protected function uniteFiles($recursively = true)
{
if (empty($configParams) || empty($configParams['name']) || empty($configParams['cachePath']) || empty($configParams['corePath'])) {
return false;
}
$content = $this->getUniteFiles()->uniteFilesSingle($this->paths['corePath'], $this->name, $recursively);
//merge matadata files
$content= $this->getUniteFiles()->uniteFilesSingle($configParams['corePath'], $configParams['name'], $recursively);
if (!empty($this->paths['modulePath'])) {
$customDir = strstr($this->paths['modulePath'], '{*}', true);
$dirList = $this->getFileManager()->getFileList($customDir, false, '', 'dir');
if (!empty($configParams['customPath'])) {
$customDir= strstr($configParams['customPath'], '{*}', true);
$dirList= $this->getFileManager()->getFileList($customDir, false, '', 'dir');
foreach($dirList as $dirName) {
$curPath= str_replace('{*}', $dirName, $configParams['customPath']);
$content= Util::merge($content, $this->getUniteFiles()->uniteFilesSingle($curPath, $configParams['name'], $recursively, $dirName));
foreach ($dirList as $dirName) {
$curPath = str_replace('{*}', $dirName, $this->paths['modulePath']);
$content = Util::merge($content, $this->getUniteFiles()->uniteFilesSingle($curPath, $this->name, $recursively, $dirName));
}
}
//END: merge matadata files
//todo check customPaths
if (!empty($this->paths['customPath'])) {
$content = Util::merge($content, $this->getUniteFiles()->uniteFilesSingle($this->paths['customPath'], $this->name, $recursively));
}
return $content;
}
@@ -409,23 +412,6 @@ class Metadata
return false;
}
/**
* Get settings for Metadata
*
* @return array
*/
public function getMetaConfig()
{
if (isset($this->metadataConfig) && is_array($this->metadataConfig)) {
return $this->metadataConfig;
}
$this->metadataConfig = $this->getConfig()->get('metadataConfig');
$this->metadataConfig['metadataCacheFile'] = Util::concatPath($this->metadataConfig['cachePath'], $this->metadataConfig['name']).'.php';
return $this->metadataConfig;
}
}

View File

@@ -5,20 +5,10 @@ return array (
'customDir' => 'application/Espo/Custom',
'cachePath' => 'data/cache',
'defaultsPath' => 'application/Espo/Core/defaults',
'unsetFileName' => 'unset.json',
'espoModulePath' => 'Espo/Modules/{*}',
'espoCustomPath' => 'Espo/Custom',
'metadataConfig' =>
array (
'name' => 'metadata',
'cachePath' => 'data/cache/application',
'corePath' => 'application/Espo/Resources/metadata',
'customPath' => 'application/Espo/Modules/{*}/Resources/metadata',
),
'languageConfig' =>
array (
'name' => '{lang}',
@@ -63,19 +53,13 @@ return array (
'adminItems',
'configPath',
'cachePath',
'metadataConfig',
'languageConfig',
'database',
'customPath',
'defaultsPath',
'unsetFileName',
'configPathFull',
'configCustomPathFull',
'crud',
'customDir',
'espoModulePath',
'espoCustomPath',
'scopeModuleMap',
),
'adminItems' =>
array (
@@ -91,6 +75,8 @@ return array (
'GBP' => 1.67,
),
),
'defaultLanguage' => 'enUs',
);
?>

View File

@@ -2,6 +2,8 @@
namespace tests\Espo\Core\Utils;
use tests\ReflectionHelper;
class LayoutTest extends \PHPUnit_Framework_TestCase
{
@@ -9,15 +11,21 @@ class LayoutTest extends \PHPUnit_Framework_TestCase
protected $objects;
protected $reflection;
protected $filesPath= 'tests/testData/FileManager';
protected function setUp()
{
$this->objects['config'] = $this->getMockBuilder('\\Espo\\Core\\Utils\\Config')->disableOriginalConstructor()->getMock();
$this->objects['fileManager'] = $this->getMockBuilder('\\Espo\\Core\\Utils\\File\\Manager')->disableOriginalConstructor()->getMock();
$this->objects['metadata'] = $this->getMockBuilder('\\Espo\\Core\\Utils\\Metadata')->disableOriginalConstructor()->getMock();
$this->object = new \Espo\Core\Utils\Layout($this->objects['config'], $this->objects['fileManager'], $this->objects['metadata']);
$this->object = new \Espo\Core\Utils\Layout($this->objects['fileManager'], $this->objects['metadata']);
$this->reflection = new ReflectionHelper($this->object);
$this->reflection->setProperty('params', array(
'application/Espo/Core/defaults',
) );
}
protected function tearDown()
@@ -29,24 +37,24 @@ class LayoutTest extends \PHPUnit_Framework_TestCase
function testGetLayoutPathCore()
{
$this->objects['metadata']
->expects($this->exactly(2))
->expects($this->exactly(1))
->method('getScopeModuleName')
->will($this->returnValue(false));
$this->assertEquals('application/Espo/Resources/layouts/User', $this->object->getLayoutPath('User'));
$this->assertEquals('application/Espo/Custom/Resources/layouts/User', $this->object->getLayoutPath('User', true));
$this->assertEquals('application/Espo/Resources/layouts/User', $this->reflection->invokeMethod('getLayoutPath', array('User')) );
$this->assertEquals('application/Espo/Custom/Resources/layouts/User', $this->reflection->invokeMethod('getLayoutPath', array('User', true)) );
}
function testGetLayoutPathModule()
{
$this->objects['metadata']
->expects($this->exactly(2))
->expects($this->exactly(1))
->method('getScopeModuleName')
->will($this->returnValue('Crm'));
$this->assertEquals('application/Espo/Modules/Crm/Resources/layouts/Call', $this->object->getLayoutPath('Call'));
$this->assertEquals('application/Espo/Custom/Modules/Crm/Resources/layouts/Call', $this->object->getLayoutPath('Call', true));
$this->assertEquals('application/Espo/Modules/Crm/Resources/layouts/Call', $this->reflection->invokeMethod('getLayoutPath', array('Call')) );
$this->assertEquals('application/Espo/Custom/Resources/layouts/Call', $this->reflection->invokeMethod('getLayoutPath', array('Call', true)) );
}
function testGet()
@@ -54,14 +62,9 @@ class LayoutTest extends \PHPUnit_Framework_TestCase
$result = '[{"label":"Overview","rows":[[{"name":"userName"},{"name":"isAdmin"}],[{"name":"name"},{"name":"title"}],[{"name":"defaultTeam"}],[{"name":"emailAddress"},{"name":"phone"}]]}]';
$this->objects['metadata']
->expects($this->exactly(2))
->expects($this->exactly(1))
->method('getScopeModuleName')
->will($this->returnValue(false));
$this->objects['config']
->expects($this->never())
->method('get')
->will($this->returnValue('application/Espo/Core/defaults'));
->will($this->returnValue(false));
$this->objects['fileManager']
->expects($this->exactly(1))

View File

@@ -32,8 +32,6 @@ return array (
'adminItems',
'configPath',
'cachePath',
'metadataConfig',
'languageConfig',
'database',
'customPath',
'defaultsPath',