diff --git a/application/Espo/Core/CronManager.php b/application/Espo/Core/CronManager.php index e548ca904f..90001a475c 100644 --- a/application/Espo/Core/CronManager.php +++ b/application/Espo/Core/CronManager.php @@ -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() diff --git a/application/Espo/Core/HookManager.php b/application/Espo/Core/HookManager.php index 65da1d04b2..99c89204af 100644 --- a/application/Espo/Core/HookManager.php +++ b/application/Espo/Core/HookManager.php @@ -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); } } diff --git a/application/Espo/Core/ServiceFactory.php b/application/Espo/Core/ServiceFactory.php index eb59be5a2c..8a5ce8d9fb 100644 --- a/application/Espo/Core/ServiceFactory.php +++ b/application/Espo/Core/ServiceFactory.php @@ -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(); } diff --git a/application/Espo/Core/Utils/Autoload.php b/application/Espo/Core/Utils/Autoload.php index 7e229c8f24..7351e5fac3 100644 --- a/application/Espo/Core/Utils/Autoload.php +++ b/application/Espo/Core/Utils/Autoload.php @@ -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.'); } diff --git a/application/Espo/Core/Utils/Config.php b/application/Espo/Core/Utils/Config.php index bcbc8e74f5..01059829c4 100644 --- a/application/Espo/Core/Utils/Config.php +++ b/application/Espo/Core/Utils/Config.php @@ -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; diff --git a/application/Espo/Core/Utils/Database/Schema/Converter.php b/application/Espo/Core/Utils/Database/Schema/Converter.php index ed5ce667bb..04ae39eeea 100644 --- a/application/Espo/Core/Utils/Database/Schema/Converter.php +++ b/application/Espo/Core/Utils/Database/Schema/Converter.php @@ -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); } diff --git a/application/Espo/Core/Utils/File/ClassParser.php b/application/Espo/Core/Utils/File/ClassParser.php index ab7ea72f8f..17c78f4d67 100644 --- a/application/Espo/Core/Utils/File/ClassParser.php +++ b/application/Espo/Core/Utils/File/ClassParser.php @@ -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(); } diff --git a/application/Espo/Core/Utils/File/Manager.php b/application/Espo/Core/Utils/File/Manager.php index ac31bb69e9..142f494d7c 100644 --- a/application/Espo/Core/Utils/File/Manager.php +++ b/application/Espo/Core/Utils/File/Manager.php @@ -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); } /** diff --git a/application/Espo/Core/Utils/Language.php b/application/Espo/Core/Utils/Language.php index 23560e8ab6..1c5bd51cc1 100644 --- a/application/Espo/Core/Utils/Language.php +++ b/application/Espo/Core/Utils/Language.php @@ -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()); } } } diff --git a/application/Espo/Core/Utils/Metadata.php b/application/Espo/Core/Utils/Metadata.php index 3c8f7699ea..d16c86f156 100644 --- a/application/Espo/Core/Utils/Metadata.php +++ b/application/Espo/Core/Utils/Metadata.php @@ -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'); } diff --git a/application/Espo/Core/Utils/Module.php b/application/Espo/Core/Utils/Module.php index b9420d5e29..24231e52fb 100644 --- a/application/Espo/Core/Utils/Module.php +++ b/application/Espo/Core/Utils/Module.php @@ -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.'); } diff --git a/application/Espo/Core/Utils/Route.php b/application/Espo/Core/Utils/Route.php index a563750155..3296c3894f 100644 --- a/application/Espo/Core/Utils/Route.php +++ b/application/Espo/Core/Utils/Route.php @@ -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.']'); diff --git a/tests/Espo/Core/CronManagerTest.php b/tests/Espo/Core/CronManagerTest.php index 95a2f574cc..8af18f272e 100644 --- a/tests/Espo/Core/CronManagerTest.php +++ b/tests/Espo/Core/CronManagerTest.php @@ -1,4 +1,4 @@ -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()) ); + } + + } diff --git a/tests/Espo/Core/Utils/ConfigTest.php b/tests/Espo/Core/Utils/ConfigTest.php index 8721ceea04..6e827a4815 100644 --- a/tests/Espo/Core/Utils/ConfigTest.php +++ b/tests/Espo/Core/Utils/ConfigTest.php @@ -1,4 +1,4 @@ -objects['fileManager']->getContents($this->configPath); + $configDataWithoutSystem = $this->objects['fileManager']->getPhpContents($this->configPath); $this->assertArrayNotHasKey('systemItems', $configDataWithoutSystem); $this->assertArrayNotHasKey('adminItems', $configDataWithoutSystem);