From fcce1d03ccbbd7b6f3193349febf129305a6718d Mon Sep 17 00:00:00 2001 From: Taras Machyshyn Date: Fri, 14 Feb 2014 18:22:13 +0200 Subject: [PATCH] major fileManager changes --- application/Espo/Core/Acl.php | 2 +- application/Espo/Core/CronManager.php | 4 +- application/Espo/Core/HookManager.php | 4 +- application/Espo/Core/ServiceFactory.php | 4 +- application/Espo/Core/Utils/Config.php | 8 +- .../Core/Utils/Database/Schema/Converter.php | 2 +- .../Espo/Core/Utils/File/ClassParser.php | 4 +- application/Espo/Core/Utils/File/Manager.php | 164 +++++++++--------- .../Espo/Core/Utils/File/UniteFiles.php | 20 +-- application/Espo/Core/Utils/Layout.php | 4 +- application/Espo/Core/Utils/Log.php | 6 +- application/Espo/Core/Utils/Metadata.php | 10 +- application/Espo/Core/Utils/Route.php | 6 +- application/Espo/Core/Utils/Util.php | 2 +- .../Modules/Crm/Services/InboundEmail.php | 2 +- application/Espo/Repositories/Preferences.php | 4 +- application/Espo/Services/Attachment.php | 2 +- application/Espo/Services/EmailTemplate.php | 4 +- application/Espo/Services/Record.php | 2 +- tests/Espo/Core/CronManagerTest.php | 6 +- tests/Espo/Core/Utils/File/ManagerTest.php | 63 +++++++ tests/Espo/Core/Utils/LayoutTest.php | 2 +- .../testData/FileManager/setContent/test.json | 2 +- 23 files changed, 193 insertions(+), 134 deletions(-) diff --git a/application/Espo/Core/Acl.php b/application/Espo/Core/Acl.php index d893967953..349aee6644 100644 --- a/application/Espo/Core/Acl.php +++ b/application/Espo/Core/Acl.php @@ -223,7 +223,7 @@ class Acl private function buildCache() { $contents = '<' . '?'. 'php return ' . var_export($this->data, true) . ';'; - $this->fileManager()->setContent($this->cacheFile, $contents); + $this->fileManager()->putContents($contents, $this->cacheFile); } } diff --git a/application/Espo/Core/CronManager.php b/application/Espo/Core/CronManager.php index 98e96b1501..6812a6aa44 100644 --- a/application/Espo/Core/CronManager.php +++ b/application/Espo/Core/CronManager.php @@ -69,7 +69,7 @@ class CronManager protected function getLastRunTime() { - $lastRunTime = $this->getFileManager()->getContent($this->lastRunTime); + $lastRunTime = $this->getFileManager()->getContents($this->lastRunTime); if (!is_int($lastRunTime)) { $lastRunTime = time() - (intval($this->getConfig()->get('cron.minExecutionTime')) + 60); } @@ -79,7 +79,7 @@ class CronManager protected function setLastRunTime($time) { - return $this->getFileManager()->setContentPHP($time, $this->lastRunTime); + return $this->getFileManager()->putContentsPHP($this->lastRunTime, $time); } protected function checkLastRunTime() diff --git a/application/Espo/Core/HookManager.php b/application/Espo/Core/HookManager.php index e5fce8b310..751062bacb 100644 --- a/application/Espo/Core/HookManager.php +++ b/application/Espo/Core/HookManager.php @@ -46,7 +46,7 @@ class HookManager protected function loadHooks() { if ($this->getConfig()->get('useCache') && file_exists($this->cacheFile)) { - $this->data = $this->getFileManager()->getContent($this->cacheFile); + $this->data = $this->getFileManager()->getContents($this->cacheFile); return; } @@ -58,7 +58,7 @@ class HookManager } if ($this->getConfig()->get('useCache')) { - $this->getFileManager()->setContentPHP($this->data, $this->cacheFile); + $this->getFileManager()->putContentsPHP($this->cacheFile, $this->data); } } diff --git a/application/Espo/Core/ServiceFactory.php b/application/Espo/Core/ServiceFactory.php index b0bff268bb..92011b2934 100644 --- a/application/Espo/Core/ServiceFactory.php +++ b/application/Espo/Core/ServiceFactory.php @@ -33,7 +33,7 @@ class ServiceFactory $config = $this->getContainer()->get('config'); if (file_exists($this->cacheFile) && $config->get('useCache')) { - $this->data = $this->getFileManager()->getContent($this->cacheFile); + $this->data = $this->getFileManager()->getContents($this->cacheFile); } else { $this->data = $this->getClassNameHash(array($this->paths['corePath'], $this->paths['customPath'])); @@ -42,7 +42,7 @@ class ServiceFactory $this->data = array_merge($this->data, $this->getClassNameHash(array($path))); } if ($config->get('useCache')) { - $result = $this->getFileManager()->setContentPHP($this->data, $this->cacheFile); + $result = $this->getFileManager()->putContentsPHP($this->cacheFile, $this->data); if ($result == false) { throw new \Espo\Core\Exceptions\Error(); } diff --git a/application/Espo/Core/Utils/Config.php b/application/Espo/Core/Utils/Config.php index c5dbd531f8..b4cc82c7b8 100644 --- a/application/Espo/Core/Utils/Config.php +++ b/application/Espo/Core/Utils/Config.php @@ -81,7 +81,7 @@ class Config } $content = array($name => $value); - $status = $this->getFileManager()->mergeContentPHP($content, $this->get('configPath'), '', true); + $status = $this->getFileManager()->mergeContentsPHP($this->get('configPath'), $content, true); $this->loadConfig(true); return $status; @@ -104,7 +104,7 @@ class Config return false; } - $status = $this->getFileManager()->mergeContentPHP($values, $this->get('configPath'), '', true); + $status = $this->getFileManager()->mergeContentsPHP($this->get('configPath'), $values, true); $this->loadConfig(true); return $status; @@ -121,9 +121,9 @@ class Config return $this->configData; } - $defaultConfig = $this->getFileManager()->getContent($this->defaultConfigPath); + $defaultConfig = $this->getFileManager()->getContents($this->defaultConfigPath); - $config = $this->getFileManager()->getContent($defaultConfig['configPath']); + $config = $this->getFileManager()->getContents($defaultConfig['configPath']); if (empty($config)) { $GLOBALS['log']->add('FATAL', 'Check syntax or permission of your '.$defaultConfig['configPath']); } diff --git a/application/Espo/Core/Utils/Database/Schema/Converter.php b/application/Espo/Core/Utils/Database/Schema/Converter.php index 9f03a1f16e..ce2b684964 100644 --- a/application/Espo/Core/Utils/Database/Schema/Converter.php +++ b/application/Espo/Core/Utils/Database/Schema/Converter.php @@ -293,7 +293,7 @@ class Converter $fileList = $this->getFileManager()->getFileList($this->customTablePath, false, '\.php$', 'file'); foreach($fileList as $fileName) { - $fileData = $this->getFileManager()->getContent($this->customTablePath, $fileName); + $fileData = $this->getFileManager()->getContents( 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 6fe1c961f4..1f2622978f 100644 --- a/application/Espo/Core/Utils/File/ClassParser.php +++ b/application/Espo/Core/Utils/File/ClassParser.php @@ -68,7 +68,7 @@ class ClassParser } if ($cacheFile && file_exists($cacheFile) && $this->getConfig()->get('useCache')) { - $data = $this->getFileManager()->getContent($cacheFile); + $data = $this->getFileManager()->getContents($cacheFile); } else { $data = $this->getClassNameHash( $this->getInitPaths($paths) ); @@ -81,7 +81,7 @@ class ClassParser } if ($cacheFile && $this->getConfig()->get('useCache')) { - $result = $this->getFileManager()->setContentPHP($data, $cacheFile); + $result = $this->getFileManager()->putContentsPHP($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 4d477f1e9e..88c9b82c2a 100644 --- a/application/Espo/Core/Utils/File/Manager.php +++ b/application/Espo/Core/Utils/File/Manager.php @@ -116,80 +116,100 @@ class Manager } /** - * Get content from file - * - * @param string $folderPath string - Folder path, Ex. myfolder - * @param bool $filePath - File path, Ex. file.json - * - * @return string | bool | array + * Reads entire file into a string + * + * @param string | array $paths Ex. 'path.php' OR array('dir', 'path.php') + * @param boolean $useIncludePath + * @param resource $context + * @param integer $offset + * @param integer $maxlen + * @return mixed */ - function getContent($folderPath, $filePath = '') + public function getContents($paths, $useIncludePath = false, $context = null, $offset = -1, $maxlen = null) { - $fullPath= Utils\Util::concatPath($folderPath, $filePath); + $fullPath = $this->concatPaths($paths); - return $this->fileGetContents($fullPath); + if (file_exists($fullPath)) { + + if (strtolower(substr($fullPath, -4))=='.php') { + return include($fullPath); + } else { + if (isset($maxlen)) { + return file_get_contents($fullPath, $useIncludePath, $context, $offset, $maxlen); + } else { + return file_get_contents($fullPath, $useIncludePath, $context, $offset); + } + } + + } + + return false; } /** - * Save content to file - * - * @param string $data - * @param string $folderPath string - Folder path, Ex. myfolder - * @param bool $filePath - File path, Ex. file.json - * - * @return bool + * Write data to a file + * + * @param string | array $paths + * @param mixed $data + * @param integer $flags + * @param resource $context + * + * @return bool */ - function setContent($content, $folderPath, $filePath = '') + public function putContents($paths, $data, $flags = 0, $context = null) { - $fullPath= Utils\Util::concatPath($folderPath, $filePath); + $fullPath = $this->concatPaths($paths); //todo remove after changing the params - return $this->filePutContents($fullPath, $content); + if ($this->checkCreateFile($fullPath) === false) { + return false; + } + + return (file_put_contents($fullPath, $data, $flags, $context) !== FALSE); } /** * Save PHP content to file * - * @param string $data - * @param string $folderPath string - Folder path, Ex. myfolder - * @param bool $filePath - File path, Ex. file.json + * @param string | array $paths + * @param string $data * * @return bool */ - function setContentPHP($content, $folderPath, $filePath = '') + public function putContentsPHP($paths, $data) { - return $this->setContent($this->getPHPFormat($content), $folderPath, $filePath); + return $this->putContents($paths, $this->getPHPFormat($data)); } /** * Save JSON content to file * + * @param string | array $paths * @param string $data - * @param string $folderPath string - Folder path, Ex. myfolder - * @param bool $filePath - File path, Ex. file.json * * @return bool */ - function setContentJSON($content, $folderPath, $filePath='') + public function putContentsJSON($paths, $data) { - if (!Utils\Json::isJSON($content)) { - $content= Utils\Json::encode($data); + if (!Utils\Json::isJSON($data)) { + $data= Utils\Json::encode($data); } - return $this->setContent($content, $folderPath, $filePath); + + return $this->putContents($paths, $data); } /** * Merge file content and save it to a file * - * @param string $data JSON string - * @param string $folderPath string - Folder path, Ex. myfolder - * @param bool $filePath - File path, Ex. file.json + * @param string | array $paths + * @param string $content JSON string + * @param bool $isJSON * * @return bool */ - function mergeContent($content, $folderPath, $filePath = '', $isJSON = false) + public function mergeContents($paths, $content, $isJSON = false) { - $fileContent= $this->getContent($folderPath, $filePath); + $fileContent= $this->getContents($paths); $savedDataArray= $this->getArrayData($fileContent); $newDataArray= $this->getArrayData($content); @@ -199,22 +219,21 @@ class Manager $data= Utils\Json::encode($data); } - return $this->setContent($data, $folderPath, $filePath); + return $this->putContents($paths, $data); } /** * Merge PHP content and save it to a file * - * @param string $data - * @param string $folderPath string - Folder path, Ex. myfolder - * @param bool $filePath - File path, Ex. file.json + * @param string | array $paths + * @param string $content * @param bool $onlyFirstLevel - Merge only first level. Ex. current: array('test'=>array('item1', 'item2')). $content= array('test'=>array('item1'),). Result will be array('test'=>array('item1')). * * @return bool */ - function mergeContentPHP($content, $folderPath, $filePath='', $onlyFirstLevel= false) + public function mergeContentsPHP($paths, $content, $onlyFirstLevel= false) { - $fileContent= $this->getContent($folderPath, $filePath); + $fileContent= $this->getContents($paths); $savedDataArray= $this->getArrayData($fileContent); $newDataArray= $this->getArrayData($content); @@ -228,69 +247,43 @@ class Manager $data= Utils\Util::merge($savedDataArray, $newDataArray); - return $this->setContentPHP($data, $folderPath, $filePath); + return $this->putContentsPHP($paths, $data); } /** * Append the content to the end of the file * - * @param string $content - * @param string $folderPath string - Folder path, Ex. myfolder - * @param bool $filePath - File path, Ex. file.json + * @param string | array $paths + * @param mixed $data * * @return bool */ - function appendContent($content, $folderPath, $filePath='') + public function appendContents($paths, $data) { - $fullPath= Utils\Util::concatPath($folderPath, $filePath); - - return $this->filePutContents($fullPath, $content, FILE_APPEND | LOCK_EX); + return $this->putContents($paths, $data, FILE_APPEND | LOCK_EX); } - - /** - * Write a string to a file - * - * @param string $filename - * @param mixed $data - * @param int $flags - * - * @return bool + * Concat paths + * @param string | array $paths Ex. array('pathPart1', 'pathPart2', 'pathPart3') + * @return string */ - public function filePutContents($filename, $data, $flags = 0) + protected function concatPaths($paths) { - if ($this->checkCreateFile($filename) === false) { - return false; + if (is_string($paths)) { + return $paths; } - return (file_put_contents($filename, $data, $flags) !== FALSE); - } - - /** - * Reads entire file into a string - * - * @param string $filename - * @param bool $useIncludePath - * - * @return string | false - */ - function fileGetContents($filename, $useIncludePath=false) - { - if (file_exists($filename)) { - - if (strtolower(substr($filename, -4))=='.php') { - return include($filename); - } else { - return file_get_contents($filename, $useIncludePath); - } - + $fullPath = ''; + foreach ($paths as $path) { + $fullPath = Utils\Util::concatPath($fullPath, $path); } - return false; + return $fullPath; } + /** * Create a new file if not exists with all folders in the path. * @@ -330,10 +323,11 @@ class Manager /** * Remove all files in defined directory * + * @param array $filePaths - File paths list * @param string $dirPath - directory path * @return bool */ - public function removeFiles($filePaths, $dirPath='') + public function removeFiles($filePaths, $dirPath = null) { if (!is_array($filePaths)) { $filePaths = (array) $filePaths; @@ -341,7 +335,7 @@ class Manager $result= true; foreach($filePaths as $filePath) { - if (!empty($dirPath)) { + if (isset($dirPath)) { $filePath= Utils\Util::concatPath($dirPath, $filePath); } diff --git a/application/Espo/Core/Utils/File/UniteFiles.php b/application/Espo/Core/Utils/File/UniteFiles.php index 3f8255795f..34327f94ed 100644 --- a/application/Espo/Core/Utils/File/UniteFiles.php +++ b/application/Espo/Core/Utils/File/UniteFiles.php @@ -63,8 +63,8 @@ class UniteFiles $jsonData= $this->getObject('JSON')->encode($content); $cacheFile= Utils\Util::concatPath($configParams['cachePath'], $configParams['name']); - $result= $this->setContent($jsonData, $cacheFile.'.json'); - $result&= $this->setContent($this->getFileManager()->getPHPFormat($content), $cacheFile.'.php'); + $result= $this->putContents($cacheFile.'.json', $jsonData); + $result&= $this->putContents($cacheFile.'.php', $this->getFileManager()->getPHPFormat($content)); //END: save medatada to cache files return $result; */ @@ -106,12 +106,12 @@ class UniteFiles } else { /*get content from a single file*/ if ($fileName == $unsetFileName) { - $fileContent = $this->getFileManager()->getContent($dirPath, $fileName); + $fileContent = $this->getFileManager()->getContents(array($dirPath, $fileName)); $unsets = Utils\Json::getArrayData($fileContent); continue; } /*END: Save data from unset.json*/ - $mergedValues = $this->uniteFilesGetContent($dirPath, $fileName, $defaultValues); + $mergedValues = $this->uniteFilesGetContent(array($dirPath, $fileName), $defaultValues); if (!empty($mergedValues)) { $name = $this->getFileManager()->getFileName($fileName, '.json'); @@ -134,15 +134,14 @@ class UniteFiles /** * Helpful method for get content from files for unite Files * - * @param string $folderPath string - Folder path, Ex. myfolder - * @param bool $filePath - File path, Ex. file.json + * @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($folderPath, $fileName, $defaults) + public function uniteFilesGetContent($paths, $defaults) { - $fileContent= $this->getFileManager()->getContent($folderPath, $fileName); + $fileContent= $this->getFileManager()->getContents($paths); $decoded= Utils\Json::getArrayData($fileContent); if (empty($decoded) && !is_array($decoded)) { @@ -177,10 +176,9 @@ class UniteFiles function loadDefaultValues($name, $type='metadata') { $params = $this->getParams(); - $defaultPath= $params['defaultsPath']; - //$defaultPath= $this->getConfig('defaultsPath'); + $defaultPath= $params['defaultsPath']; - $defaultValue= $this->getFileManager()->getContent( Utils\Util::concatPath($defaultPath, $type), $name.'.json'); + $defaultValue= $this->getFileManager()->getContents( array($defaultPath, $type, $name.'.json') ); if ($defaultValue!==false) { //return default array return Utils\Json::decode($defaultValue, true); diff --git a/application/Espo/Core/Utils/Layout.php b/application/Espo/Core/Utils/Layout.php index 20cffed2ac..ad7e84a13f 100644 --- a/application/Espo/Core/Utils/Layout.php +++ b/application/Espo/Core/Utils/Layout.php @@ -79,7 +79,7 @@ class Layout } } - return $this->getFileManager()->getContent($fileFullPath); + return $this->getFileManager()->getContents($fileFullPath); } @@ -105,7 +105,7 @@ class Layout $data = Json::encode($data); } - return $this->getFileManager()->setContent($data, $layoutPath, $name.'.json'); + return $this->getFileManager()->putContents(array($layoutPath, $name.'.json'), $data); } diff --git a/application/Espo/Core/Utils/Log.php b/application/Espo/Core/Utils/Log.php index 04ec9b4f88..e5a148112c 100644 --- a/application/Espo/Core/Utils/Log.php +++ b/application/Espo/Core/Utils/Log.php @@ -61,12 +61,16 @@ class Log private $output; private $params; + protected $logFilePath; + public function __construct(\Espo\Core\Utils\File\Manager $fileManager, \Espo\Core\Utils\Api\Output $output, array $params) { $this->fileManager = $fileManager; $this->output = $output; $this->params = $params; + + $this->logFilePath = Util::concatPath($params['options']['dir'], $params['options']['file']); } @@ -143,7 +147,7 @@ class Log $text = date('Y-m-d H:i:s') . ' ' . $text; $params = $this->getParams(); - return $this->getFileManager()->appendContent($text, $params['options']['dir'], $params['options']['file']); + return $this->getFileManager()->appendContents($this->logFilePath, $text); } /** diff --git a/application/Espo/Core/Utils/Metadata.php b/application/Espo/Core/Utils/Metadata.php index fef2109c07..8359ab4bf5 100644 --- a/application/Espo/Core/Utils/Metadata.php +++ b/application/Espo/Core/Utils/Metadata.php @@ -79,7 +79,7 @@ class Metadata if ($reload) { //save medatada to a cache file $metaConfig = $this->getMetaConfig(); - $isSaved = $this->getFileManager()->setContentPHP($data, $metaConfig['metadataCacheFile']); + $isSaved = $this->getFileManager()->putContentsPHP($metaConfig['metadataCacheFile'], $data); if ($isSaved === false) { $GLOBALS['log']->add('FATAL', 'Metadata:init() - metadata has not been saved to a cache file'); } @@ -159,7 +159,7 @@ class Metadata } } else if (file_exists($metaConfig['metadataCacheFile'])) { - $data = $this->getFileManager()->getContent($metaConfig['metadataCacheFile']); + $data = $this->getFileManager()->getContents($metaConfig['metadataCacheFile']); } if ($isJSON) { @@ -201,7 +201,7 @@ class Metadata $data= Json::encode($this->meta); //END: merge data with defaults values - $result= $this->getFileManager()->setContent($data, $fullPath, $scope.'.json'); + $result= $this->getFileManager()->putContents(array($fullPath, $scope.'.json'), $data); return $result; } @@ -221,7 +221,7 @@ class Metadata $this->getConverter()->process(); } - $this->espoMetadata = $this->getFileManager()->getContent($espoMetadataFile); + $this->espoMetadata = $this->getFileManager()->getContents($espoMetadataFile); return $this->espoMetadata; } @@ -230,7 +230,7 @@ class Metadata { $metaConfig = $this->getMetaConfig(); - $result = $this->getFileManager()->setContentPHP($espoMetadata, $metaConfig['cachePath'], 'ormMetadata.php'); + $result = $this->getFileManager()->putContentsPHP(array($metaConfig['cachePath'], 'ormMetadata.php'), $espoMetadata); if ($result == false) { $GLOBALS['log']->add('EXCEPTION', 'Metadata::setOrmMetadata() - Cannot save ormMetadata to a file'); throw new \Espo\Core\Exceptions\Error(); diff --git a/application/Espo/Core/Utils/Route.php b/application/Espo/Core/Utils/Route.php index 6121e8a552..651614d126 100644 --- a/application/Espo/Core/Utils/Route.php +++ b/application/Espo/Core/Utils/Route.php @@ -63,11 +63,11 @@ class Route protected function init() { if (file_exists($this->cacheFile) && $this->getConfig()->get('useCache')) { - $this->data = $this->getFileManager()->getContent($this->cacheFile); + $this->data = $this->getFileManager()->getContents($this->cacheFile); } else { $this->data = $this->uniteFiles(); - $result = $this->getFileManager()->setContentPHP($this->data, $this->cacheFile); + $result = $this->getFileManager()->putContentsPHP($this->cacheFile, $this->data); if ($result == false) { $GLOBALS['log']->add('EXCEPTION', 'Route::init() - Cannot save Routes to a file'); throw new \Espo\Core\Exceptions\Error(); @@ -108,7 +108,7 @@ class Route protected function getAddData($currData, $routeFile) { if (file_exists($routeFile)) { - $content= $this->getFileManager()->getContent($routeFile); + $content= $this->getFileManager()->getContents($routeFile); $arrayContent = Json::getArrayData($content); if (empty($arrayContent)) { $GLOBALS['log']->add('ERROR', 'Route::uniteFiles() - Empty file or syntax error - ['.$routeFile.']'); diff --git a/application/Espo/Core/Utils/Util.php b/application/Espo/Core/Utils/Util.php index 2d109c7cf9..1a34890949 100644 --- a/application/Espo/Core/Utils/Util.php +++ b/application/Espo/Core/Utils/Util.php @@ -160,7 +160,7 @@ class Util if (empty($filePath)) { return $folderPath; } - if (empty($folderPath)) { + if (empty($folderPath)) { return $filePath; } diff --git a/application/Espo/Modules/Crm/Services/InboundEmail.php b/application/Espo/Modules/Crm/Services/InboundEmail.php index 5e22556afd..b7a10f1c45 100644 --- a/application/Espo/Modules/Crm/Services/InboundEmail.php +++ b/application/Espo/Modules/Crm/Services/InboundEmail.php @@ -303,7 +303,7 @@ class InboundEmail extends \Espo\Services\Record $path = 'data/upload/' . $attachment->id; $content = base64_decode($part->getContent()); - $this->getFileManager()->setContent($content, $path); + $this->getFileManager()->putContents($path, $content); $attachmentsIds = $email->get('attachmentsIds'); $attachmentsIds[] = $attachment->id; $email->set('attachmentsIds', $attachmentsIds); diff --git a/application/Espo/Repositories/Preferences.php b/application/Espo/Repositories/Preferences.php index dfeb536c41..1f381a938e 100644 --- a/application/Espo/Repositories/Preferences.php +++ b/application/Espo/Repositories/Preferences.php @@ -39,7 +39,7 @@ class Preferences extends \Espo\Core\ORM\Repository $fileName = $this->getFilePath($id); if (file_exists($fileName)) { - $this->data[$id] = json_decode($this->getFileManager()->getContent($fileName), true); + $this->data[$id] = json_decode($this->getFileManager()->getContents($fileName), true); } else { $fields = $this->getMetadata()->get('entityDefs.Preferences.fields'); $defaults = array(); @@ -65,7 +65,7 @@ class Preferences extends \Espo\Core\ORM\Repository $this->data[$entity->id] = $entity->toArray(); $fileName = $this->getFilePath($entity->id); - $this->getFileManager()->setContent(json_encode($this->data[$entity->id]), $fileName); + $this->getFileManager()->putContents($fileName, json_encode($this->data[$entity->id])); return $entity; } } diff --git a/application/Espo/Services/Attachment.php b/application/Espo/Services/Attachment.php index 00af9581f4..b81285622f 100644 --- a/application/Espo/Services/Attachment.php +++ b/application/Espo/Services/Attachment.php @@ -21,7 +21,7 @@ class Attachment extends Record list($prefix, $contents) = explode(',', $data['file']); if (!empty($entity->id)) { - $this->getFileManager()->setContent(base64_decode($contents), 'data/upload/' . $entity->id); + $this->getFileManager()->putContents('data/upload/' . $entity->id, base64_decode($contents)); } return $entity; diff --git a/application/Espo/Services/EmailTemplate.php b/application/Espo/Services/EmailTemplate.php index 0f6504d349..ef8a929659 100644 --- a/application/Espo/Services/EmailTemplate.php +++ b/application/Espo/Services/EmailTemplate.php @@ -94,11 +94,11 @@ class EmailTemplate extends Record $clone->set($data); $this->getEntityManager()->saveEntity($clone); - $contents = $this->getFileManager()->getContent('data/upload/' . $attachment->id); + $contents = $this->getFileManager()->getContents('data/upload/' . $attachment->id); if (empty($contents)) { continue; } - $this->getFileManager()->setContent($contents, 'data/upload/' . $clone->id); + $this->getFileManager()->putContents('data/upload/' . $clone->id, $contents); $attachmentsIds[] = $clone->id; } diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index 97978dd7cb..7bb83f7111 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -400,7 +400,7 @@ class Record extends \Espo\Core\Services\Base $this->getEntityManager()->saveEntity($attachment); if (!empty($attachment->id)) { - $this->getInjection('fileManager')->setContent($csv, 'data/upload/' . $attachment->id); + $this->getInjection('fileManager')->putContents('data/upload/' . $attachment->id, $csv); // TODO cron job to remove file return $attachment->id; } diff --git a/tests/Espo/Core/CronManagerTest.php b/tests/Espo/Core/CronManagerTest.php index e47e193ddb..b192e4b74d 100644 --- a/tests/Espo/Core/CronManagerTest.php +++ b/tests/Espo/Core/CronManagerTest.php @@ -48,7 +48,7 @@ class CronManagerTest extends \PHPUnit_Framework_TestCase { $this->objects['fileManager'] ->expects($this->once()) - ->method('getContent') + ->method('getContents') ->will($this->returnValue(false)); $this->objects['config'] @@ -64,7 +64,7 @@ class CronManagerTest extends \PHPUnit_Framework_TestCase { $this->objects['fileManager'] ->expects($this->once()) - ->method('getContent') + ->method('getContents') ->will($this->returnValue(time()-60)); $this->objects['config'] @@ -80,7 +80,7 @@ class CronManagerTest extends \PHPUnit_Framework_TestCase { $this->objects['fileManager'] ->expects($this->once()) - ->method('getContent') + ->method('getContents') ->will($this->returnValue(time()-49)); $this->objects['config'] diff --git a/tests/Espo/Core/Utils/File/ManagerTest.php b/tests/Espo/Core/Utils/File/ManagerTest.php index 715c08106c..a0784c3b97 100644 --- a/tests/Espo/Core/Utils/File/ManagerTest.php +++ b/tests/Espo/Core/Utils/File/ManagerTest.php @@ -2,6 +2,8 @@ namespace tests\Espo\Core\Utils\File; +use tests\ReflectionHelper; + class ManagerTest extends \PHPUnit_Framework_TestCase { @@ -11,6 +13,8 @@ class ManagerTest extends \PHPUnit_Framework_TestCase protected $filesPath= 'tests/testData/FileManager'; + protected $reflection; + protected function setUp() { $this->object = new \Espo\Core\Utils\File\Manager( @@ -23,6 +27,8 @@ class ManagerTest extends \PHPUnit_Framework_TestCase ), ) ); + + $this->reflection = new ReflectionHelper($this->object); } protected function tearDown() @@ -42,6 +48,63 @@ class ManagerTest extends \PHPUnit_Framework_TestCase $this->assertEquals('Donwload', $this->object->getFileName('application/Espo/EntryPoints/Donwload.php')); } + 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() + { + $testPath= $this->filesPath.'/setContent'; + + $result= 'next value'; + $this->assertTrue($this->object->putContents(array($testPath, 'test.json'), $result)); + + $this->assertEquals($result, $this->object->getContents( array($testPath, 'test.json')) ); + + $this->assertTrue($this->object->putContents(array($testPath, 'test.json'), 'initial value')); + } + + + function testConcatPaths() + { + $input = 'application/Espo/Resources/metadata/app/panel.json'; + $result = 'application/Espo/Resources/metadata/app/panel.json'; + + $this->assertEquals($result, $this->reflection->invokeMethod('concatPaths', array($input)) ); + + + $input = array( + 'application', + 'Espo/Resources/metadata/', + 'app', + 'panel.json', + ); + $result = 'application/Espo/Resources/metadata/app/panel.json'; + + $this->assertEquals($result, $this->reflection->invokeMethod('concatPaths', array($input)) ); + + + $input = array( + 'application/Espo/Resources/metadata/app', + 'panel.json', + ); + $result = 'application/Espo/Resources/metadata/app/panel.json'; + + $this->assertEquals($result, $this->reflection->invokeMethod('concatPaths', array($input)) ); + + + $input = array( + 'application/Espo/Resources/metadata/app/', + 'panel.json', + ); + $result = 'application/Espo/Resources/metadata/app/panel.json'; + + $this->assertEquals($result, $this->reflection->invokeMethod('concatPaths', array($input)) ); + } + } diff --git a/tests/Espo/Core/Utils/LayoutTest.php b/tests/Espo/Core/Utils/LayoutTest.php index 972fb515ab..16d72c697e 100644 --- a/tests/Espo/Core/Utils/LayoutTest.php +++ b/tests/Espo/Core/Utils/LayoutTest.php @@ -65,7 +65,7 @@ class LayoutTest extends \PHPUnit_Framework_TestCase $this->objects['fileManager'] ->expects($this->exactly(1)) - ->method('getContent') + ->method('getContents') ->will($this->returnValue($result)); $this->assertEquals($result, $this->object->get('User', 'detail')); diff --git a/tests/testData/FileManager/setContent/test.json b/tests/testData/FileManager/setContent/test.json index 7d42fb3930..3df7a88d94 100644 --- a/tests/testData/FileManager/setContent/test.json +++ b/tests/testData/FileManager/setContent/test.json @@ -1 +1 @@ -next value \ No newline at end of file +initial value \ No newline at end of file