From 1e29bc0f9846fb699e37c376cc9ff007da9d06a7 Mon Sep 17 00:00:00 2001 From: Taras Machyshyn Date: Thu, 6 Feb 2014 17:56:01 +0200 Subject: [PATCH] added admin/crearCache --- application/Espo/Controllers/Admin.php | 14 + application/Espo/Core/Utils/File/Manager.php | 443 ++++++++++--------- application/Espo/Resources/routes.json | 11 +- 3 files changed, 252 insertions(+), 216 deletions(-) diff --git a/application/Espo/Controllers/Admin.php b/application/Espo/Controllers/Admin.php index a5ed380f64..d4c37eb52f 100644 --- a/application/Espo/Controllers/Admin.php +++ b/application/Espo/Controllers/Admin.php @@ -29,5 +29,19 @@ class Admin extends \Espo\Core\Controllers\Base return json_encode($result); } + + public function actionClearCache($params, $data) + { + $cacheDir = $this->getContainer()->get('config')->get('cachePath'); + + $result = $this->getContainer()->get('fileManager')->removeInDir($cacheDir); + + if ($result === false) { + throw new Error("Error while clearing cache"); + } + + return json_encode($result); + } + } diff --git a/application/Espo/Core/Utils/File/Manager.php b/application/Espo/Core/Utils/File/Manager.php index 176d9de4af..e948d424f3 100644 --- a/application/Espo/Core/Utils/File/Manager.php +++ b/application/Espo/Core/Utils/File/Manager.php @@ -33,16 +33,16 @@ class Manager /** - * Get a list of files in specified directory - * - * @param string $path string - Folder path, Ex. myfolder - * @param bool | int $recursively - Find files in subfolders - * @param string $filter - Filter for files. Use regular expression, Ex. \.json$ - * @param string $fileType [all, file, dir] - Filter for type of files/directories. - * @param bool $isReturnSingleArray - if need to return a single array of file list - * - * @return array - */ + * Get a list of files in specified directory + * + * @param string $path string - Folder path, Ex. myfolder + * @param bool | int $recursively - Find files in subfolders + * @param string $filter - Filter for files. Use regular expression, Ex. \.json$ + * @param string $fileType [all, file, dir] - Filter for type of files/directories. + * @param bool $isReturnSingleArray - if need to return a single array of file list + * + * @return array + */ function getFileList($path, $recursively=false, $filter='', $fileType='all', $isReturnSingleArray = false) { if (!file_exists($path)) { @@ -92,13 +92,13 @@ class Manager } /** - * Convert file list to a single array - * - * @param aray $fileList - * @param string $parentDirName - * - * @return aray - */ + * Convert file list to a single array + * + * @param aray $fileList + * @param string $parentDirName + * + * @return aray + */ protected function getSingeFileList(array $fileList, $parentDirName = '') { $singleFileList = array(); @@ -116,13 +116,13 @@ 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 - */ + * Get content from file + * + * @param string $folderPath string - Folder path, Ex. myfolder + * @param bool $filePath - File path, Ex. file.json + * + * @return string | bool | array + */ function getContent($folderPath, $filePath = '') { $fullPath= Utils\Util::concatPath($folderPath, $filePath); @@ -132,14 +132,14 @@ class Manager /** - * 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 - */ + * 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 + */ function setContent($content, $folderPath, $filePath = '') { $fullPath= Utils\Util::concatPath($folderPath, $filePath); @@ -148,28 +148,28 @@ class Manager } /** - * Save PHP content to file - * - * @param string $data - * @param string $folderPath string - Folder path, Ex. myfolder - * @param bool $filePath - File path, Ex. file.json - * - * @return bool - */ + * Save PHP content to file + * + * @param string $data + * @param string $folderPath string - Folder path, Ex. myfolder + * @param bool $filePath - File path, Ex. file.json + * + * @return bool + */ function setContentPHP($content, $folderPath, $filePath = '') { return $this->setContent($this->getPHPFormat($content), $folderPath, $filePath); } /** - * Save JSON content to file - * - * @param string $data - * @param string $folderPath string - Folder path, Ex. myfolder - * @param bool $filePath - File path, Ex. file.json - * - * @return bool - */ + * Save JSON content to file + * + * @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='') { if (!Utils\Json::isJSON($content)) { @@ -179,14 +179,14 @@ class Manager } /** - * 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 - * - * @return bool - */ + * 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 + * + * @return bool + */ function mergeContent($content, $folderPath, $filePath = '', $isJSON = false) { $fileContent= $this->getContent($folderPath, $filePath); @@ -203,15 +203,15 @@ class Manager } /** - * 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 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 - */ + * 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 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) { $fileContent= $this->getContent($folderPath, $filePath); @@ -232,14 +232,14 @@ class Manager } /** - * 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 - * - * @return bool - */ + * 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 + * + * @return bool + */ function appendContent($content, $folderPath, $filePath='') { $fullPath= Utils\Util::concatPath($folderPath, $filePath); @@ -251,14 +251,14 @@ class Manager /** - * Write a string to a file - * - * @param string $filename - * @param mixed $data - * @param int $flags - * - * @return bool - */ + * Write a string to a file + * + * @param string $filename + * @param mixed $data + * @param int $flags + * + * @return bool + */ public function filePutContents($filename, $data, $flags = 0) { if ($this->checkCreateFile($filename) === false) { @@ -269,13 +269,13 @@ class Manager } /** - * Reads entire file into a string - * - * @param string $filename - * @param bool $useIncludePath - * - * @return string | 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)) { @@ -292,11 +292,11 @@ class Manager } /** - * Create a new file if not exists with all folders in the path. - * - * @param string $filePath - * @return string - */ + * Create a new file if not exists with all folders in the path. + * + * @param string $filePath + * @return string + */ public function checkCreateFile($filePath) { if (file_exists($filePath)) { @@ -326,11 +326,11 @@ class Manager } /** - * Remove all files in defined directory - * - * @param string $dirPath - directory path - * @return bool - */ + * Remove all files in defined directory + * + * @param string $dirPath - directory path + * @return bool + */ public function removeFiles($filePaths, $dirPath='') { if (!is_array($filePaths)) { @@ -352,28 +352,42 @@ class Manager } /** - * Remove all files in defined directory - * - * @param string $dirPath - directory path - * @return bool - */ - public function removeFilesInDir($dirPath) + * Remove all files in defined directory + * + * @param string $dirPath - directory path + * @param bool $removeWithDir - if remove with directory + * + * @return bool + */ + public function removeInDir($dirPath, $removeWithDir = false) { - $fileList= $this->getFileList($dirPath, false, '', 'file'); - if (!empty($fileList)) { - return $this->removeFiles($fileList, $dirPath); - } + $fileList= $this->getFileList($dirPath, false); + + $result = true; + foreach ($fileList as $file) { + $fullPath = Utils\Util::concatPath($dirPath, $file); + if (is_dir($fullPath)) { + $result &= $this->removeInDir($fullPath, true); + } else { + $result &= unlink($fullPath); + } + } + + if ($removeWithDir) { + rmdir($dirPath); + } + + return $result; + } - return false; - } /** //TODO remove - * Get an array data (if JSON convert to array) - * - * @param mixed $data - can be JSON, array - * - * @return array - */ + * Get an array data (if JSON convert to array) + * + * @param mixed $data - can be JSON, array + * + * @return array + */ protected function getArrayData($data) { if (is_array($data)) { @@ -388,13 +402,13 @@ class Manager /** - * Get a filename without the file extension - * - * @param string $filename - * @param string $ext - extension, ex. '.json' - * - * @return array - */ + * Get a filename without the file extension + * + * @param string $filename + * @param string $ext - extension, ex. '.json' + * + * @return array + */ public function getFileName($fileName, $ext='') { if (empty($ext)) { @@ -417,12 +431,12 @@ class Manager /** - * Get a directory name from the path - * - * @param string $path - * - * @return array - */ + * Get a directory name from the path + * + * @param string $path + * + * @return array + */ function getDirName($path) { $pieces= explode(Utils\Util::getSeparator(), $path); @@ -439,13 +453,13 @@ class Manager /** - * Return content of PHP file - * - * @param string $varName - name of variable which contains the content - * @param array $content - * - * @return string | false - */ + * Return content of PHP file + * + * @param string $varName - name of variable which contains the content + * @param array $content + * + * @return string | false + */ public function getPHPFormat($content) { if (empty($content)) { @@ -461,10 +475,10 @@ return '.var_export($content, true).'; /** - * Get default settings - * - * @return object - */ + * Get default settings + * + * @return object + */ public function getDefaultPermissions() { if (isset($this->defaultPermissions) && is_object($this->defaultPermissions)) { @@ -478,13 +492,13 @@ return '.var_export($content, true).'; } /** - * Set default permission - * - * @param string $path - * @param bool $recurse - * - * @return bool - */ + * Set default permission + * + * @param string $path + * @param bool $recurse + * + * @return bool + */ public function setDefaultPermissions($path, $recurse=false) { if (!file_exists($path)) { @@ -506,11 +520,11 @@ return '.var_export($content, true).'; /** - * Get current permissions - * - * @param string $filename - * @return string | bool - */ + * Get current permissions + * + * @param string $filename + * @return string | bool + */ function getCurrentPermission($filePath) { if (!file_exists($filePath)) { @@ -523,14 +537,14 @@ return '.var_export($content, true).'; } /** - * Change permissions - * - * @param string $filename - * @param int | array $octal - ex. 0755, array(0644, 0755), array('file'=>0644, 'dir'=>0755) - * @param bool $recurse - * - * @return bool - */ + * Change permissions + * + * @param string $filename + * @param int | array $octal - ex. 0755, array(0644, 0755), array('file'=>0644, 'dir'=>0755) + * @param bool $recurse + * + * @return bool + */ function chmod($path, $octal, $recurse=false) { if (!file_exists($path)) { @@ -585,14 +599,14 @@ return '.var_export($content, true).'; /** - * Change permissions recirsive - * - * @param string $filename - * @param int $fileOctal - ex. 0644 - * @param int $dirOctal - ex. 0755 - * - * @return bool - */ + * Change permissions recirsive + * + * @param string $filename + * @param int $fileOctal - ex. 0644 + * @param int $dirOctal - ex. 0755 + * + * @return bool + */ function chmodRecurse($path, $fileOctal=0644, $dirOctal=0755) { if (!file_exists($path)) { @@ -618,13 +632,13 @@ return '.var_export($content, true).'; /** - * Change permissions recirsive - * - * @param string $filename - * @param int $mode - ex. 0644 - * - * @return bool - */ + * Change permissions recirsive + * + * @param string $filename + * @param int $mode - ex. 0644 + * + * @return bool + */ function chmodReal($filename, $mode) { $result= chmod($filename, $mode); @@ -640,14 +654,14 @@ return '.var_export($content, true).'; /** - * Change owner permission - * - * @param string $path - * @param int | string $user - * @param bool $recurse - * - * @return bool - */ + * Change owner permission + * + * @param string $path + * @param int | string $user + * @param bool $recurse + * + * @return bool + */ function chown($path, $user='', $recurse=false) { if (!file_exists($path)) { @@ -668,14 +682,13 @@ return '.var_export($content, true).'; } /** - * Change owner permission recirsive - * - * @param string $filename - * @param int $fileOctal - ex. 0644 - * @param int $dirOctal - ex. 0755 - * - * @return bool - */ + * Change owner permission recirsive + * + * @param string $path + * @param string $user + * + * @return bool + */ function chownRecurse($path, $user) { if (!file_exists($path)) { @@ -693,14 +706,14 @@ return '.var_export($content, true).'; } /** - * Change group permission - * - * @param string $path - * @param int | string $group - * @param bool $recurse - * - * @return bool - */ + * Change group permission + * + * @param string $path + * @param int | string $group + * @param bool $recurse + * + * @return bool + */ function chgrp($path, $group='', $recurse=false) { if (!file_exists($path)) { @@ -721,14 +734,14 @@ return '.var_export($content, true).'; } /** - * Change group permission recirsive - * - * @param string $filename - * @param int $fileOctal - ex. 0644 - * @param int $dirOctal - ex. 0755 - * - * @return bool - */ + * Change group permission recirsive + * + * @param string $filename + * @param int $fileOctal - ex. 0644 + * @param int $dirOctal - ex. 0755 + * + * @return bool + */ function chgrpRecurse($path, $group) { if (!file_exists($path)) { @@ -746,10 +759,10 @@ return '.var_export($content, true).'; } /** - * Get default owner user - * - * @return int - owner id - */ + * Get default owner user + * + * @return int - owner id + */ function getDefaultOwner($usePosix=false) { $owner= $this->getDefaultPermissions()->user; @@ -765,10 +778,10 @@ return '.var_export($content, true).'; } /** - * Get default group user - * - * @return int - group id - */ + * Get default group user + * + * @return int - group id + */ function getDefaultGroup($usePosix=false) { $group= $this->getDefaultPermissions()->group; diff --git a/application/Espo/Resources/routes.json b/application/Espo/Resources/routes.json index 94f2f218ad..c3ad11e3cf 100644 --- a/application/Espo/Resources/routes.json +++ b/application/Espo/Resources/routes.json @@ -120,7 +120,16 @@ "method":"get", "params":{ "controller":"Admin", - "action":"rebuild" + "action":"rebuild" + } + }, + + { + "route":"/Admin/clearCache", + "method":"get", + "params":{ + "controller":"Admin", + "action":"clearCache" } },