diff --git a/application/Espo/Core/CronManager.php b/application/Espo/Core/CronManager.php index aa9dabbf87..6d3984805a 100644 --- a/application/Espo/Core/CronManager.php +++ b/application/Espo/Core/CronManager.php @@ -283,24 +283,20 @@ class CronManager } try { - $previousDate = $cronExpression->getPreviousRunDate()->format('Y-m-d H:i:s'); + $nextDate = $cronExpression->getNextRunDate()->format('Y-m-d H:i:s'); } catch (\Exception $e) { $GLOBALS['log']->error('CronManager (ScheduledJob ['.$scheduledJob['id'].']): Unsupported CRON expression ['.$scheduling.']'); continue; } - if ($cronExpression->isDue()) { - $previousDate = date('Y-m-d H:i:s'); - } - - $existingJob = $this->getCronJob()->getJobByScheduledJob($scheduledJob['id'], $previousDate); + $existingJob = $this->getCronJob()->getJobByScheduledJob($scheduledJob['id'], $nextDate); if ($existingJob) continue; $className = $this->getScheduledJobUtil()->get($scheduledJob['job']); if ($className) { if (method_exists($className, 'prepare')) { $implementation = new $className($this->container); - $implementation->prepare($scheduledJob, $previousDate); + $implementation->prepare($scheduledJob, $nextDate); continue; } } @@ -314,7 +310,7 @@ class CronManager 'name' => $scheduledJob['name'], 'status' => self::PENDING, 'scheduledJobId' => $scheduledJob['id'], - 'executeTime' => $previousDate, + 'executeTime' => $nextDate, 'method' => $scheduledJob['job'] )); $this->getEntityManager()->saveEntity($jobEntity); diff --git a/application/Espo/Core/ExtensionManager.php b/application/Espo/Core/ExtensionManager.php index b97e3beb4e..977ef1b0b1 100644 --- a/application/Espo/Core/ExtensionManager.php +++ b/application/Espo/Core/ExtensionManager.php @@ -42,6 +42,11 @@ class ExtensionManager extends Upgrades\Base 'after' => 'AfterInstall', 'beforeUninstall' => 'BeforeUninstall', 'afterUninstall' => 'AfterUninstall', + ), + + 'customDirNames' => array( + 'before' => 'beforeInstallFiles', + 'after' => 'afterInstallFiles', ) ); } diff --git a/application/Espo/Core/UpgradeManager.php b/application/Espo/Core/UpgradeManager.php index 6193e041bb..e97b5e19ad 100644 --- a/application/Espo/Core/UpgradeManager.php +++ b/application/Espo/Core/UpgradeManager.php @@ -42,6 +42,11 @@ class UpgradeManager extends Upgrades\Base 'scriptNames' => array( 'before' => 'BeforeUpgrade', 'after' => 'AfterUpgrade', + ), + + 'customDirNames' => array( + 'before' => 'beforeUpgradeFiles', + 'after' => 'afterUpgradeFiles', ) ); } \ No newline at end of file diff --git a/application/Espo/Core/Upgrades/Actions/Base.php b/application/Espo/Core/Upgrades/Actions/Base.php index 7032808333..535efa9ee4 100644 --- a/application/Espo/Core/Upgrades/Actions/Base.php +++ b/application/Espo/Core/Upgrades/Actions/Base.php @@ -389,9 +389,7 @@ abstract class Base { if (!isset($this->data['fileList'])) { $packagePath = $this->getPackagePath(); - $filesPath = Util::concatPath($packagePath, self::FILES); - - $this->data['fileList'] = $this->getFileManager()->getFileList($filesPath, true, '', true, true); + $this->data['fileList'] = $this->getFileList($packagePath); } return $this->data['fileList']; @@ -401,14 +399,55 @@ abstract class Base { if (!isset($this->data['restoreFileList'])) { $backupPath = $this->getPath('backupPath'); - $backupFilePath = Util::concatPath($backupPath, self::FILES); - - $this->data['restoreFileList'] = $this->getFileManager()->getFileList($backupFilePath, true, '', true, true); + $this->data['restoreFileList'] = $this->getFileList($backupPath); } return $this->data['restoreFileList']; } + /** + * Get file directories (files, beforeInstallFiles, afterInstallFiles) + * + * @param sting $parentDirPath + * + * @return array + */ + protected function getFileDirs($parentDirPath = null) + { + $dirNames = $this->getParams('customDirNames'); + $paths = array(self::FILES, $dirNames['before'], $dirNames['after']); + + if (isset($parentDirPath)) { + foreach ($paths as &$path) { + $path = Util::concatPath($parentDirPath, $path); + } + } + + return $paths; + } + + /** + * Get file list from directories: files, beforeUpgradeFiles, afterUpgradeFiles + * + * @param string $dirPath + * + * @return array + */ + protected function getFileList($dirPath) + { + $fileList = array(); + + $paths = $this->getFileDirs($dirPath); + foreach ($paths as $filesPath) { + if (file_exists($filesPath)) { + $files = $this->getFileManager()->getFileList($filesPath, true, '', true, true); + $fileList = array_merge($fileList, $files); + } + } + + return $fileList; + } + protected function copy($sourcePath, $destPath, $recursively = false, array $fileList = null, $copyOnlyFiles = false) { try { @@ -423,15 +462,32 @@ abstract class Base /** * Copy files from upgrade/extension package * - * @param string $processId + * @param string $type + * * @return boolean */ - protected function copyFiles() + protected function copyFiles($type = null) { - $packagePath = $this->getPackagePath(); - $filesPath = Util::concatPath($packagePath, self::FILES); + switch ($type) { + case 'before': + case 'after': + $dirNames = $this->getParams('customDirNames'); + $dirPath = $dirNames[$type]; + break; - return $this->copy($filesPath, '', true); + default: + $dirPath = self::FILES; + break; + } + + $packagePath = $this->getPackagePath(); + $filesPath = Util::concatPath($packagePath, $dirPath); + + if (file_exists($filesPath)) { + return $this->copy($filesPath, '', true); + } + + return true; } public function getManifest() diff --git a/application/Espo/Core/Upgrades/Actions/Base/Install.php b/application/Espo/Core/Upgrades/Actions/Base/Install.php index 9876fea467..5292945759 100644 --- a/application/Espo/Core/Upgrades/Actions/Base/Install.php +++ b/application/Espo/Core/Upgrades/Actions/Base/Install.php @@ -67,6 +67,11 @@ class Install extends \Espo\Core\Upgrades\Actions\Base $this->backupExistingFiles(); + //beforeInstallFiles + if (!$this->copyFiles('before')) { + $this->throwErrorAndRemovePackage('Cannot copy beforeInstall files.'); + } + /* run before install script */ $this->runScript('before'); @@ -85,6 +90,11 @@ class Install extends \Espo\Core\Upgrades\Actions\Base $this->throwErrorAndRemovePackage('Error occurred while EspoCRM rebuild.'); } + //afterInstallFiles + if (!$this->copyFiles('after')) { + $this->throwErrorAndRemovePackage('Cannot copy afterInstall files.'); + } + /* run before install script */ $this->runScript('after'); @@ -112,7 +122,9 @@ class Install extends \Espo\Core\Upgrades\Actions\Base $deleteFileList = array_diff($copyFileList, $backupFileList); $res = $this->copy($backupFilePath, '', true); - $res &= $this->getFileManager()->remove($deleteFileList, null, true); + if (!empty($deleteFileList)) { + $res &= $this->getFileManager()->remove($deleteFileList, null, true); + } if ($res) { $this->getFileManager()->removeInDir($backupPath, true); diff --git a/application/Espo/Core/Upgrades/Actions/Base/Uninstall.php b/application/Espo/Core/Upgrades/Actions/Base/Uninstall.php index f987ba2224..2ffda6b8e8 100644 --- a/application/Espo/Core/Upgrades/Actions/Base/Uninstall.php +++ b/application/Espo/Core/Upgrades/Actions/Base/Uninstall.php @@ -95,15 +95,20 @@ class Uninstall extends \Espo\Core\Upgrades\Actions\Base protected function restoreFiles() { $packagePath = $this->getPath('packagePath'); - $filesPath = Util::concatPath($packagePath, self::FILES); - if (!file_exists($filesPath)) { + $manifestPath = Util::concatPath($packagePath, $this->manifestName); + if (!file_exists($manifestPath)) { $this->unzipArchive($packagePath); } - $res = $this->copy($filesPath, '', true); + $fileDirs = $this->getFileDirs($packagePath); + foreach ($fileDirs as $filesPath) { + if (file_exists($filesPath)) { + $res = $this->copy($filesPath, '', true); + } + } - $manifestJson = $this->getFileManager()->getContents(array($packagePath, $this->manifestName)); + $manifestJson = $this->getFileManager()->getContents($manifestPath); $manifest = Json::decode($manifestJson, true); if (!empty($manifest['delete'])) { $res &= $this->getFileManager()->remove($manifest['delete'], null, true); @@ -114,7 +119,7 @@ class Uninstall extends \Espo\Core\Upgrades\Actions\Base return $res; } - protected function copyFiles() + protected function copyFiles($type = null) { $backupPath = $this->getPath('backupPath'); $res = $this->copy(array($backupPath, self::FILES), '', true); diff --git a/application/Espo/Core/Upgrades/Actions/Extension/Install.php b/application/Espo/Core/Upgrades/Actions/Extension/Install.php index a00632f10c..adbf35acfc 100644 --- a/application/Espo/Core/Upgrades/Actions/Extension/Install.php +++ b/application/Espo/Core/Upgrades/Actions/Extension/Install.php @@ -63,9 +63,8 @@ class Install extends \Espo\Core\Upgrades\Actions\Base\Install /** copy scripts files */ $packagePath = $this->getPackagePath(); - $res &= $this->copy(array($packagePath, self::SCRIPTS), array($backupPath, self::SCRIPTS), true); - return $res; + return $this->copy(array($packagePath, self::SCRIPTS), array($backupPath, self::SCRIPTS), true); } protected function isNew()