Added 'deleteBeforeCopy' for upgrade/extensions

This commit is contained in:
Taras Machyshyn
2016-11-16 15:21:39 +02:00
parent 967d5a6387
commit 10d329094c
5 changed files with 149 additions and 17 deletions
+59 -12
View File
@@ -311,6 +311,17 @@ abstract class Base
return $this->getPath('packagePath', $isPackage);
}
protected function getDeleteList($type = 'delete')
{
$manifest = $this->getManifest();
if (isset($manifest[$type])) {
return $manifest[$type];
}
return array();
}
/**
* Get a list of files defined in manifest.json
*
@@ -318,13 +329,26 @@ abstract class Base
*/
protected function getDeleteFileList()
{
$manifest = $this->getManifest();
if (!isset($this->data['deleteFileList'])) {
$deleteFileList = array();
if (!empty($manifest['delete'])) {
return $manifest['delete'];
$deleteList = array_merge($this->getDeleteList('delete'), $this->getDeleteList('deleteBeforeCopy'));
foreach ($deleteList as $key => $itemPath) {
if (is_dir($itemPath)) {
$fileList = $this->getFileManager()->getFileList($itemPath, true, '', true, true);
$fileList = $this->concatStringWithArray($itemPath, $fileList);
$deleteFileList = array_merge($deleteFileList, $fileList);
continue;
}
$deleteFileList[] = $itemPath;
}
$this->data['deleteFileList'] = $deleteFileList;
}
return array();
return $this->data['deleteFileList'];
}
/**
@@ -334,17 +358,28 @@ abstract class Base
*/
protected function deleteFiles($withEmptyDirs = false)
{
$deleteFileList = $this->getDeleteFileList();
$deleteList = $this->getDeleteList('delete');
//remove directories, leave only files
foreach ($deleteFileList as $key => $filePath) {
if (!is_file($filePath)) {
unset($deleteFileList[$key]);
}
if (!empty($deleteList)) {
return $this->getFileManager()->remove($deleteList, null, $withEmptyDirs);
}
if (!empty($deleteFileList)) {
return $this->getFileManager()->remove($deleteFileList, null, $withEmptyDirs);
return true;
}
/**
* Deleted file/forder list before coppy the upgrade files
*
* @param boolean $withEmptyDirs
*
* @return boolean
*/
protected function deleteBeforeCopy($withEmptyDirs = false)
{
$deleteList = $this->getDeleteList('deleteBeforeCopy');
if (!empty($deleteList)) {
$this->getFileManager()->remove($deleteList, null, $withEmptyDirs);
}
return true;
@@ -571,4 +606,16 @@ abstract class Base
return $this->helper;
}
protected function concatStringWithArray($string, array $array)
{
foreach ($array as &$value) {
if (substr($string, -1) != '/') {
$string .= '/';
}
$value = $string . $value;
}
return $array;
}
}
@@ -70,6 +70,9 @@ class Install extends \Espo\Core\Upgrades\Actions\Base
/* run before install script */
$this->runScript('before');
/* remove files defined in a manifest "deleteBeforeCopy" */
$this->deleteBeforeCopy(true);
/* copy files from directory "Files" to EspoCRM files */
if (!$this->copyFiles()) {
$this->throwErrorAndRemovePackage('Cannot copy files.');
@@ -179,13 +179,15 @@ class Uninstall extends \Espo\Core\Upgrades\Actions\Base
return $this->data['restoreFileList'];
}
protected function getDeleteFileList()
protected function getDeleteList($type = 'delete')
{
$packageFileList = $this->getRestoreFileList();
$backupFileList = $this->getCopyFileList();
if ($type == 'delete') {
$packageFileList = $this->getRestoreFileList();
$backupFileList = $this->getCopyFileList();
$deleteFileList = array_diff($packageFileList, $backupFileList);
return array_diff($packageFileList, $backupFileList);
}
return $deleteFileList;
return array();
}
}
@@ -0,0 +1,40 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2015 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Upgrades\Actions\Upgrade;
use Espo\Core\Exceptions\Error;
class Delete extends \Espo\Core\Upgrades\Actions\Base\Delete
{
public function run($data)
{
throw new Error('The operation is not permitted.');
}
}
@@ -0,0 +1,40 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2015 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Upgrades\Actions\Upgrade;
use Espo\Core\Exceptions\Error;
class Uninstall extends \Espo\Core\Upgrades\Actions\Base\Uninstall
{
public function run($data)
{
throw new Error('The operation is not permitted.');
}
}