Initial commit
This commit is contained in:
68
application/Espo/Core/Upgrades/Actions/Extension/Delete.php
Normal file
68
application/Espo/Core/Upgrades/Actions/Extension/Delete.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Upgrades\Actions\Extension;
|
||||
use Espo\Core\Exceptions\Error;
|
||||
use Espo\Entities\Extension;
|
||||
|
||||
class Delete extends \Espo\Core\Upgrades\Actions\Base\Delete
|
||||
{
|
||||
protected ?Extension $extensionEntity = null;
|
||||
|
||||
/**
|
||||
* Get an entity of this extension.
|
||||
*
|
||||
* @throws Error
|
||||
*/
|
||||
protected function getExtensionEntity(): Extension
|
||||
{
|
||||
if (!$this->extensionEntity) {
|
||||
$processId = $this->getProcessId();
|
||||
|
||||
$this->extensionEntity = $this->getEntityManager()->getEntityById(Extension::ENTITY_TYPE, $processId);
|
||||
|
||||
if (!$this->extensionEntity) {
|
||||
throw new Error('Extension entity not found.');
|
||||
}
|
||||
}
|
||||
|
||||
return $this->extensionEntity;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Error
|
||||
*/
|
||||
protected function afterRunAction(): void
|
||||
{
|
||||
/** Delete extension entity */
|
||||
$extensionEntity = $this->getExtensionEntity();
|
||||
|
||||
$this->getEntityManager()->removeEntity($extensionEntity);
|
||||
}
|
||||
}
|
||||
245
application/Espo/Core/Upgrades/Actions/Extension/Install.php
Normal file
245
application/Espo/Core/Upgrades/Actions/Extension/Install.php
Normal file
@@ -0,0 +1,245 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Upgrades\Actions\Extension;
|
||||
|
||||
use Espo\Core\Upgrades\Base;
|
||||
use Espo\Core\Utils\Util;
|
||||
use Espo\Core\Exceptions\Error;
|
||||
|
||||
use Espo\Entities\Extension;
|
||||
use Espo\ORM\Name\Attribute;
|
||||
use Throwable;
|
||||
|
||||
class Install extends \Espo\Core\Upgrades\Actions\Base\Install
|
||||
{
|
||||
protected ?Extension $extensionEntity = null;
|
||||
|
||||
/**
|
||||
* @throws Error
|
||||
*/
|
||||
protected function beforeRunAction(): void
|
||||
{
|
||||
$this->loadExtension();
|
||||
|
||||
if (!$this->isNew()) {
|
||||
$this->scriptParams['isUpgrade'] = true;
|
||||
|
||||
$this->compareVersion();
|
||||
$this->uninstallExtension();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Error
|
||||
*/
|
||||
protected function afterRunAction(): void
|
||||
{
|
||||
if (!$this->isNew()) {
|
||||
$this->deleteExtension();
|
||||
}
|
||||
|
||||
$this->storeExtension();
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy Existing files to a backup directory.
|
||||
*
|
||||
* @throws Error
|
||||
*/
|
||||
protected function backupExistingFiles(): bool
|
||||
{
|
||||
parent::backupExistingFiles();
|
||||
|
||||
$backupPath = $this->getPath('backupPath');
|
||||
|
||||
/** copy scripts files */
|
||||
$packagePath = $this->getPackagePath();
|
||||
|
||||
$source = Util::concatPath($packagePath, self::SCRIPTS);
|
||||
$destination = Util::concatPath($backupPath, self::SCRIPTS);
|
||||
|
||||
return $this->copy($source, $destination, true);
|
||||
}
|
||||
|
||||
private function isNew(): bool
|
||||
{
|
||||
$extensionEntity = $this->getExtensionEntity();
|
||||
|
||||
if ($extensionEntity) {
|
||||
$id = $extensionEntity->get(Attribute::ID);
|
||||
}
|
||||
|
||||
return !isset($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the entity of the extension.
|
||||
*/
|
||||
private function getExtensionEntity(): ?Extension
|
||||
{
|
||||
return $this->extensionEntity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the extension entity.
|
||||
*
|
||||
* @throws Error
|
||||
*/
|
||||
private function loadExtension(): void
|
||||
{
|
||||
$manifest = $this->getManifest();
|
||||
|
||||
$this->extensionEntity = $this->getEntityManager()
|
||||
->getRDBRepositoryByClass(Extension::class)
|
||||
->where([
|
||||
'name' => $manifest['name'],
|
||||
'isInstalled' => true,
|
||||
])
|
||||
->findOne();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a record of an extension entity.
|
||||
*
|
||||
* @throws Error
|
||||
*/
|
||||
private function storeExtension(): void
|
||||
{
|
||||
$entityManager = $this->getEntityManager();
|
||||
|
||||
$extensionEntity = null;
|
||||
|
||||
if ($this->getProcessId()) {
|
||||
$extensionEntity = $entityManager->getEntityById(Extension::ENTITY_TYPE, $this->getProcessId());
|
||||
}
|
||||
|
||||
if (!$extensionEntity) {
|
||||
$extensionEntity = $entityManager->getNewEntity(Extension::ENTITY_TYPE);
|
||||
}
|
||||
|
||||
$manifest = $this->getManifest();
|
||||
$fileList = $this->getCopyFileList();
|
||||
|
||||
$data = [
|
||||
'id' => $this->getProcessId(),
|
||||
'name' => trim($manifest['name']),
|
||||
'isInstalled' => true,
|
||||
'version' => $manifest['version'],
|
||||
'fileList' => $fileList,
|
||||
'description' => $manifest['description'],
|
||||
];
|
||||
|
||||
if (!empty($manifest['checkVersionUrl'])) {
|
||||
$data['checkVersionUrl'] = $manifest['checkVersionUrl'];
|
||||
}
|
||||
|
||||
$extensionEntity->set($data);
|
||||
|
||||
try {
|
||||
$entityManager->saveEntity($extensionEntity);
|
||||
} catch (Throwable $e) {
|
||||
$msg = "Error while saving the extension entity. The error occurred in an existing hook. " .
|
||||
"{$e->getMessage()} at {$e->getFile()}:{$e->getLine()}";
|
||||
|
||||
$this->getLog()->error($msg);
|
||||
$this->throwErrorAndRemovePackage('Error saving extension entity. Check logs for details.', false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare version between installed and a new extensions.
|
||||
*
|
||||
* @throws Error
|
||||
*/
|
||||
private function compareVersion(): void
|
||||
{
|
||||
$manifest = $this->getManifest();
|
||||
$extensionEntity = $this->getExtensionEntity();
|
||||
|
||||
if (!$extensionEntity) {
|
||||
return;
|
||||
}
|
||||
|
||||
$comparedVersion = version_compare($manifest['version'], $extensionEntity->getVersion(), '>=');
|
||||
|
||||
if ($comparedVersion <= 0) {
|
||||
$this->throwErrorAndRemovePackage('You cannot install an older version of this extension.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If extension already installed, uninstall an old version.
|
||||
*
|
||||
* @throws Error
|
||||
*/
|
||||
private function uninstallExtension(): void
|
||||
{
|
||||
$extensionEntity = $this->getExtensionEntity();
|
||||
|
||||
if (!$extensionEntity) {
|
||||
throw new Error("Can't uninstall not existing extension.");
|
||||
}
|
||||
|
||||
$this->executeAction(Base::UNINSTALL, [
|
||||
'id' => $extensionEntity->get(Attribute::ID),
|
||||
'skipSystemRebuild' => true,
|
||||
'skipAfterScript' => true,
|
||||
'parentProcessId' => $this->getProcessId(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete extension package.
|
||||
*
|
||||
* @throws Error
|
||||
*/
|
||||
private function deleteExtension(): void
|
||||
{
|
||||
$extensionEntity = $this->getExtensionEntity();
|
||||
|
||||
if (!$extensionEntity) {
|
||||
throw new Error("Can't delete not existing extension.");
|
||||
}
|
||||
|
||||
$this->executeAction(Base::DELETE, [
|
||||
'id' => $extensionEntity->get(Attribute::ID),
|
||||
'parentProcessId' => $this->getProcessId(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, string[]|string> $dependencyList
|
||||
* @throws Error
|
||||
*/
|
||||
protected function checkDependencies(array $dependencyList): bool
|
||||
{
|
||||
return $this->getHelper()->checkDependencies($dependencyList);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Upgrades\Actions\Extension;
|
||||
|
||||
use Espo\Core\Exceptions\Error;
|
||||
|
||||
use Espo\Entities\Extension;
|
||||
use Throwable;
|
||||
|
||||
class Uninstall extends \Espo\Core\Upgrades\Actions\Base\Uninstall
|
||||
{
|
||||
protected ?Extension $extensionEntity = null;
|
||||
|
||||
/**
|
||||
* Get entity of this extension.
|
||||
*
|
||||
* @return Extension
|
||||
* @throws Error
|
||||
*/
|
||||
protected function getExtensionEntity()
|
||||
{
|
||||
if (!isset($this->extensionEntity)) {
|
||||
$processId = $this->getProcessId();
|
||||
|
||||
$this->extensionEntity = $this->getEntityManager()->getEntityById(Extension::ENTITY_TYPE, $processId);
|
||||
|
||||
if (!$this->extensionEntity) {
|
||||
throw new Error('Extension entity not found.');
|
||||
}
|
||||
}
|
||||
|
||||
return $this->extensionEntity;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Error
|
||||
*/
|
||||
protected function afterRunAction(): void
|
||||
{
|
||||
/** Set extension entity, isInstalled = false */
|
||||
$extensionEntity = $this->getExtensionEntity();
|
||||
$extensionEntity->set('isInstalled', false);
|
||||
|
||||
try {
|
||||
$this->getEntityManager()->saveEntity($extensionEntity);
|
||||
} catch (Throwable $e) {
|
||||
$this->getLog()->error(
|
||||
'Error saving Extension entity. The error occurred by existing Hook, more details: ' .
|
||||
$e->getMessage() .' at '. $e->getFile() . ':' . $e->getLine()
|
||||
);
|
||||
|
||||
$this->throwErrorAndRemovePackage('Error saving Extension entity. Check logs for details.', false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
* @throws Error
|
||||
*/
|
||||
protected function getRestoreFileList(): array
|
||||
{
|
||||
if (!isset($this->data['restoreFileList'])) {
|
||||
$extensionEntity = $this->getExtensionEntity();
|
||||
$this->data['restoreFileList'] = $extensionEntity->get('fileList');
|
||||
}
|
||||
|
||||
return $this->data['restoreFileList'] ?? [];
|
||||
}
|
||||
}
|
||||
46
application/Espo/Core/Upgrades/Actions/Extension/Upload.php
Normal file
46
application/Espo/Core/Upgrades/Actions/Extension/Upload.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Upgrades\Actions\Extension;
|
||||
|
||||
use Espo\Core\Exceptions\Error;
|
||||
|
||||
class Upload extends \Espo\Core\Upgrades\Actions\Base\Upload
|
||||
{
|
||||
/**
|
||||
* Check dependencies.
|
||||
*
|
||||
* @param array<string, string[]|string> $dependencyList
|
||||
* @throws Error
|
||||
*/
|
||||
protected function checkDependencies($dependencyList): bool
|
||||
{
|
||||
return $this->getHelper()->checkDependencies($dependencyList);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user