mirror of
https://github.com/espocrm/espocrm.git
synced 2026-06-28 15:06:06 +00:00
added 'getPhpContents()' to FileManager
This commit is contained in:
@@ -94,7 +94,7 @@ class CronManager
|
||||
|
||||
protected function getLastRunTime()
|
||||
{
|
||||
$lastRunTime = $this->getFileManager()->getContents($this->lastRunTime);
|
||||
$lastRunTime = $this->getFileManager()->getPhpContents($this->lastRunTime);
|
||||
if (!is_int($lastRunTime)) {
|
||||
$lastRunTime = time() - (intval($this->getConfig()->get('cron.minExecutionTime')) + 60);
|
||||
}
|
||||
@@ -104,7 +104,7 @@ class CronManager
|
||||
|
||||
protected function setLastRunTime($time)
|
||||
{
|
||||
return $this->getFileManager()->putContentsPHP($this->lastRunTime, $time);
|
||||
return $this->getFileManager()->putPhpContents($this->lastRunTime, $time);
|
||||
}
|
||||
|
||||
protected function checkLastRunTime()
|
||||
|
||||
@@ -73,7 +73,7 @@ class HookManager
|
||||
protected function loadHooks()
|
||||
{
|
||||
if ($this->getConfig()->get('useCache') && file_exists($this->cacheFile)) {
|
||||
$this->data = $this->getFileManager()->getContents($this->cacheFile);
|
||||
$this->data = $this->getFileManager()->getPhpContents($this->cacheFile);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ class HookManager
|
||||
$this->data = array_merge_recursive($this->data, $this->getHookData($this->paths['customPath']));
|
||||
|
||||
if ($this->getConfig()->get('useCache')) {
|
||||
$this->getFileManager()->putContentsPHP($this->cacheFile, $this->data);
|
||||
$this->getFileManager()->putPhpContents($this->cacheFile, $this->data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ class ServiceFactory
|
||||
$config = $this->getContainer()->get('config');
|
||||
|
||||
if (file_exists($this->cacheFile) && $config->get('useCache')) {
|
||||
$this->data = $this->getFileManager()->getContents($this->cacheFile);
|
||||
$this->data = $this->getFileManager()->getPhpContents($this->cacheFile);
|
||||
} else {
|
||||
$this->data = $this->getClassNameHash($this->paths['corePath']);
|
||||
|
||||
@@ -65,7 +65,7 @@ class ServiceFactory
|
||||
$this->data = array_merge($this->data, $this->getClassNameHash($this->paths['customPath']));
|
||||
|
||||
if ($config->get('useCache')) {
|
||||
$result = $this->getFileManager()->putContentsPHP($this->cacheFile, $this->data);
|
||||
$result = $this->getFileManager()->putPhpContents($this->cacheFile, $this->data);
|
||||
if ($result == false) {
|
||||
throw new \Espo\Core\Exceptions\Error();
|
||||
}
|
||||
|
||||
@@ -82,14 +82,14 @@ class Autoload
|
||||
protected function init()
|
||||
{
|
||||
if (file_exists($this->cacheFile) && $this->getConfig()->get('useCache')) {
|
||||
$this->data = $this->getFileManager()->getContents($this->cacheFile);
|
||||
$this->data = $this->getFileManager()->getPhpContents($this->cacheFile);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->data = $this->unify();
|
||||
|
||||
if ($this->getConfig()->get('useCache')) {
|
||||
$result = $this->getFileManager()->putContentsPHP($this->cacheFile, $this->data);
|
||||
$result = $this->getFileManager()->putPhpContents($this->cacheFile, $this->data);
|
||||
if ($result == false) {
|
||||
throw new \Espo\Core\Exceptions\Error('Autoload: Cannot save unified autoload.');
|
||||
}
|
||||
|
||||
@@ -150,7 +150,8 @@ class Config
|
||||
|
||||
$removeData = empty($this->removeData) ? null : $this->removeData;
|
||||
|
||||
$result = $this->getFileManager()->mergeContentsPHP($this->configPath, $values, $removeData);
|
||||
$result = $this->getFileManager()->mergePhpContents($this->configPath, $values, $removeData);
|
||||
|
||||
if ($result) {
|
||||
$this->changedData = array();
|
||||
$this->removeData = array();
|
||||
@@ -162,7 +163,7 @@ class Config
|
||||
|
||||
public function getDefaults()
|
||||
{
|
||||
return $this->getFileManager()->getContents($this->defaultConfigPath);
|
||||
return $this->getFileManager()->getPhpContents($this->defaultConfigPath);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -178,9 +179,9 @@ class Config
|
||||
|
||||
$configPath = file_exists($this->configPath) ? $this->configPath : $this->defaultConfigPath;
|
||||
|
||||
$this->data = $this->getFileManager()->getContents($configPath);
|
||||
$this->data = $this->getFileManager()->getPhpContents($configPath);
|
||||
|
||||
$systemConfig = $this->getFileManager()->getContents($this->systemConfigPath);
|
||||
$systemConfig = $this->getFileManager()->getPhpContents($this->systemConfigPath);
|
||||
$this->data = Util::merge($systemConfig, $this->data);
|
||||
|
||||
return $this->data;
|
||||
|
||||
@@ -333,7 +333,7 @@ class Converter
|
||||
$fileList = $this->getFileManager()->getFileList($this->customTablePath, false, '\.php$', true);
|
||||
|
||||
foreach($fileList as $fileName) {
|
||||
$fileData = $this->getFileManager()->getContents( array($this->customTablePath, $fileName) );
|
||||
$fileData = $this->getFileManager()->getPhpContents( array($this->customTablePath, $fileName) );
|
||||
if (is_array($fileData)) {
|
||||
$customTables = Util::merge($customTables, $fileData);
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@ class ClassParser
|
||||
}
|
||||
|
||||
if ($cacheFile && file_exists($cacheFile) && $this->getConfig()->get('useCache')) {
|
||||
$data = $this->getFileManager()->getContents($cacheFile);
|
||||
$data = $this->getFileManager()->getPhpContents($cacheFile);
|
||||
} else {
|
||||
$data = $this->getClassNameHash($paths['corePath']);
|
||||
|
||||
@@ -104,7 +104,7 @@ class ClassParser
|
||||
}
|
||||
|
||||
if ($cacheFile && $this->getConfig()->get('useCache')) {
|
||||
$result = $this->getFileManager()->putContentsPHP($cacheFile, $data);
|
||||
$result = $this->getFileManager()->putPhpContents($cacheFile, $data);
|
||||
if ($result == false) {
|
||||
throw new \Espo\Core\Exceptions\Error();
|
||||
}
|
||||
|
||||
@@ -156,17 +156,31 @@ class Manager
|
||||
$fullPath = $this->concatPaths($path);
|
||||
|
||||
if (file_exists($fullPath)) {
|
||||
|
||||
if (strtolower(substr($fullPath, -4))=='.php') {
|
||||
return include($fullPath);
|
||||
if (isset($maxlen)) {
|
||||
return file_get_contents($fullPath, $useIncludePath, $context, $offset, $maxlen);
|
||||
} else {
|
||||
if (isset($maxlen)) {
|
||||
return file_get_contents($fullPath, $useIncludePath, $context, $offset, $maxlen);
|
||||
} else {
|
||||
return file_get_contents($fullPath, $useIncludePath, $context, $offset);
|
||||
}
|
||||
return file_get_contents($fullPath, $useIncludePath, $context, $offset);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get PHP array from PHP file
|
||||
*
|
||||
* @param string | array $path
|
||||
* @return array | bool
|
||||
*/
|
||||
public function getPhpContents($path)
|
||||
{
|
||||
$fullPath = $this->concatPaths($path);
|
||||
|
||||
if (file_exists($fullPath) && strtolower(substr($fullPath, -4)) == '.php') {
|
||||
$phpContents = include($fullPath);
|
||||
if (is_array($phpContents)) {
|
||||
return $phpContents;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -206,7 +220,7 @@ class Manager
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function putContentsPHP($path, $data)
|
||||
public function putPhpContents($path, $data)
|
||||
{
|
||||
return $this->putContents($path, $this->getPHPFormat($data), LOCK_EX);
|
||||
}
|
||||
@@ -235,19 +249,23 @@ class Manager
|
||||
*
|
||||
* @param string | array $path
|
||||
* @param string $content JSON string
|
||||
* @param bool $isJSON
|
||||
* @param bool $isReturnJson
|
||||
* @param string | array $removeOptions - List of unset keys from content
|
||||
* @param bool $isReturn - Is result to be returned or stored
|
||||
* @param bool $isPhp - Is merge php files
|
||||
*
|
||||
* @return bool | array
|
||||
*/
|
||||
public function mergeContents($path, $content, $isJSON = false, $removeOptions = null, $isReturn = false)
|
||||
public function mergeContents($path, $content, $isReturnJson = false, $removeOptions = null, $isPhp = false)
|
||||
{
|
||||
$fileContent = $this->getContents($path);
|
||||
if ($isPhp) {
|
||||
$fileContent = $this->getPhpContents($path);
|
||||
} else {
|
||||
$fileContent = $this->getContents($path);
|
||||
}
|
||||
|
||||
$fullPath = $this->concatPaths($path);
|
||||
if (file_exists($fullPath) && ($fileContent === false || empty($fileContent))) {
|
||||
throw new Error('Failed to read file [' . $fullPath .'].');
|
||||
throw new Error('FileManager: Failed to read file [' . $fullPath .'].');
|
||||
}
|
||||
|
||||
$savedDataArray = Utils\Json::getArrayData($fileContent);
|
||||
@@ -259,12 +277,13 @@ class Manager
|
||||
}
|
||||
|
||||
$data = Utils\Util::merge($savedDataArray, $newDataArray);
|
||||
if ($isJSON) {
|
||||
|
||||
if ($isReturnJson) {
|
||||
$data = Utils\Json::encode($data, JSON_PRETTY_PRINT);
|
||||
}
|
||||
|
||||
if ($isReturn) {
|
||||
return $data;
|
||||
if ($isPhp) {
|
||||
return $this->putPhpContents($path, $data);
|
||||
}
|
||||
|
||||
return $this->putContents($path, $data);
|
||||
@@ -278,11 +297,9 @@ class Manager
|
||||
* @param string | array $removeOptions - List of unset keys from content
|
||||
* @return bool
|
||||
*/
|
||||
public function mergeContentsPHP($path, $content, $removeOptions = null)
|
||||
public function mergePhpContents($path, $content, $removeOptions = null)
|
||||
{
|
||||
$data = $this->mergeContents($path, $content, false, $removeOptions, true);
|
||||
|
||||
return $this->putContentsPHP($path, $data);
|
||||
return $this->mergeContents($path, $content, false, $removeOptions, true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -319,7 +319,7 @@ class Language
|
||||
|
||||
if ($this->getConfig()->get('useCache')) {
|
||||
$i18nCacheFile = str_replace('{*}', $i18nName, $this->cacheFile);
|
||||
$result &= $this->getFileManager()->putContentsPHP($i18nCacheFile, $i18nData);
|
||||
$result &= $this->getFileManager()->putPhpContents($i18nCacheFile, $i18nData);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -330,7 +330,7 @@ class Language
|
||||
|
||||
$currentLanguage = $this->getLanguage();
|
||||
if (empty($this->data[$currentLanguage])) {
|
||||
$this->data[$currentLanguage] = $this->getFileManager()->getContents($this->getLangCacheFile());
|
||||
$this->data[$currentLanguage] = $this->getFileManager()->getPhpContents($this->getLangCacheFile());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,13 +124,13 @@ class Metadata
|
||||
}
|
||||
|
||||
if (file_exists($this->cacheFile) && !$reload) {
|
||||
$this->meta = $this->getFileManager()->getContents($this->cacheFile);
|
||||
$this->meta = $this->getFileManager()->getPhpContents($this->cacheFile);
|
||||
} else {
|
||||
$this->meta = $this->getUnifier()->unify($this->name, $this->paths, true);
|
||||
$this->meta = $this->setLanguageFromConfig($this->meta);
|
||||
|
||||
if ($this->getConfig()->get('useCache')) {
|
||||
$isSaved = $this->getFileManager()->putContentsPHP($this->cacheFile, $this->meta);
|
||||
$isSaved = $this->getFileManager()->putPhpContents($this->cacheFile, $this->meta);
|
||||
if ($isSaved === false) {
|
||||
$GLOBALS['log']->emergency('Metadata:init() - metadata has not been saved to a cache file');
|
||||
}
|
||||
@@ -270,7 +270,7 @@ class Metadata
|
||||
}
|
||||
|
||||
if (empty($this->ormMeta)) {
|
||||
$this->ormMeta = $this->getFileManager()->getContents($this->ormCacheFile);
|
||||
$this->ormMeta = $this->getFileManager()->getPhpContents($this->ormCacheFile);
|
||||
}
|
||||
|
||||
return $this->ormMeta;
|
||||
@@ -279,7 +279,7 @@ class Metadata
|
||||
public function setOrmMetadata(array $ormMeta)
|
||||
{
|
||||
if ($this->getConfig()->get('useCache')) {
|
||||
$result = $this->getFileManager()->putContentsPHP($this->ormCacheFile, $ormMeta);
|
||||
$result = $this->getFileManager()->putPhpContents($this->ormCacheFile, $ormMeta);
|
||||
if ($result == false) {
|
||||
throw new \Espo\Core\Exceptions\Error('Metadata::setOrmMetadata() - Cannot save ormMetadata to a file');
|
||||
}
|
||||
|
||||
@@ -84,12 +84,12 @@ class Module
|
||||
protected function init()
|
||||
{
|
||||
if (file_exists($this->cacheFile) && $this->getConfig()->get('useCache')) {
|
||||
$this->data = $this->getFileManager()->getContents($this->cacheFile);
|
||||
$this->data = $this->getFileManager()->getPhpContents($this->cacheFile);
|
||||
} else {
|
||||
$this->data = $this->getUnifier()->unify($this->paths, true);
|
||||
|
||||
if ($this->getConfig()->get('useCache')) {
|
||||
$result = $this->getFileManager()->putContentsPHP($this->cacheFile, $this->data);
|
||||
$result = $this->getFileManager()->putPhpContents($this->cacheFile, $this->data);
|
||||
if ($result == false) {
|
||||
throw new \Espo\Core\Exceptions\Error('Module - Cannot save unified modules.');
|
||||
}
|
||||
|
||||
@@ -95,12 +95,12 @@ class Route
|
||||
protected function init()
|
||||
{
|
||||
if (file_exists($this->cacheFile) && $this->getConfig()->get('useCache')) {
|
||||
$this->data = $this->getFileManager()->getContents($this->cacheFile);
|
||||
$this->data = $this->getFileManager()->getPhpContents($this->cacheFile);
|
||||
} else {
|
||||
$this->data = $this->unify();
|
||||
|
||||
if ($this->getConfig()->get('useCache')) {
|
||||
$result = $this->getFileManager()->putContentsPHP($this->cacheFile, $this->data);
|
||||
$result = $this->getFileManager()->putPhpContents($this->cacheFile, $this->data);
|
||||
if ($result == false) {
|
||||
throw new \Espo\Core\Exceptions\Error('Route - Cannot save unified routes');
|
||||
}
|
||||
@@ -127,7 +127,7 @@ class Route
|
||||
protected function getAddData($currData, $routeFile)
|
||||
{
|
||||
if (file_exists($routeFile)) {
|
||||
$content= $this->getFileManager()->getContents($routeFile);
|
||||
$content = $this->getFileManager()->getContents($routeFile);
|
||||
$arrayContent = Json::getArrayData($content);
|
||||
if (empty($arrayContent)) {
|
||||
$GLOBALS['log']->error('Route::unify() - Empty file or syntax error - ['.$routeFile.']');
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<?php
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
@@ -28,24 +28,24 @@ use tests\ReflectionHelper;
|
||||
class CronManagerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $object;
|
||||
|
||||
|
||||
protected $objects;
|
||||
|
||||
protected $filesPath= 'tests/testData/EntryPoints';
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
{
|
||||
$this->objects['container'] = $this->getMockBuilder('\Espo\Core\Container')->disableOriginalConstructor()->getMock();
|
||||
|
||||
$this->objects['serviceFactory'] = $this->getMockBuilder('\Espo\Core\ServiceFactory')->disableOriginalConstructor()->getMock();
|
||||
$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['fileManager'] = $this->getMockBuilder('\Espo\Core\Utils\File\Manager')->disableOriginalConstructor()->getMock();
|
||||
|
||||
|
||||
$map = array(
|
||||
array('config', $this->objects['config']),
|
||||
array('fileManager', $this->objects['fileManager']),
|
||||
array('serviceFactory', $this->objects['serviceFactory']),
|
||||
array('serviceFactory', $this->objects['serviceFactory']),
|
||||
);
|
||||
|
||||
$this->objects['container']
|
||||
@@ -55,7 +55,7 @@ class CronManagerTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$this->object = new \Espo\Core\CronManager( $this->objects['container'] );
|
||||
|
||||
$this->reflection = new ReflectionHelper($this->object);
|
||||
$this->reflection = new ReflectionHelper($this->object);
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
@@ -68,50 +68,50 @@ class CronManagerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$this->objects['fileManager']
|
||||
->expects($this->once())
|
||||
->method('getContents')
|
||||
->method('getPhpContents')
|
||||
->will($this->returnValue(false));
|
||||
|
||||
$this->objects['config']
|
||||
->expects($this->exactly(2))
|
||||
->method('get')
|
||||
->will($this->returnValue(50));
|
||||
|
||||
$this->assertTrue( $this->reflection->invokeMethod('checkLastRunTime', array()) );
|
||||
}
|
||||
|
||||
$this->assertTrue( $this->reflection->invokeMethod('checkLastRunTime', array()) );
|
||||
}
|
||||
|
||||
|
||||
function testCheckLastRunTime()
|
||||
{
|
||||
$this->objects['fileManager']
|
||||
->expects($this->once())
|
||||
->method('getContents')
|
||||
->method('getPhpContents')
|
||||
->will($this->returnValue(time()-60));
|
||||
|
||||
$this->objects['config']
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->will($this->returnValue(50));
|
||||
|
||||
$this->assertTrue( $this->reflection->invokeMethod('checkLastRunTime', array()) );
|
||||
}
|
||||
|
||||
$this->assertTrue( $this->reflection->invokeMethod('checkLastRunTime', array()) );
|
||||
}
|
||||
|
||||
|
||||
function testCheckLastRunTimeTooFrequency()
|
||||
{
|
||||
$this->objects['fileManager']
|
||||
->expects($this->once())
|
||||
->method('getContents')
|
||||
->method('getPhpContents')
|
||||
->will($this->returnValue(time()-49));
|
||||
|
||||
$this->objects['config']
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->will($this->returnValue(50));
|
||||
|
||||
$this->assertFalse( $this->reflection->invokeMethod('checkLastRunTime', array()) );
|
||||
}
|
||||
|
||||
|
||||
$this->assertFalse( $this->reflection->invokeMethod('checkLastRunTime', array()) );
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<?php
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
@@ -150,7 +150,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testSystemConfigMerge()
|
||||
{
|
||||
$configDataWithoutSystem = $this->objects['fileManager']->getContents($this->configPath);
|
||||
$configDataWithoutSystem = $this->objects['fileManager']->getPhpContents($this->configPath);
|
||||
$this->assertArrayNotHasKey('systemItems', $configDataWithoutSystem);
|
||||
$this->assertArrayNotHasKey('adminItems', $configDataWithoutSystem);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user