late save/remove hooks

This commit is contained in:
Yurii
2026-03-03 16:49:01 +02:00
parent 7296fcfc77
commit f6a6aefd5e
4 changed files with 154 additions and 6 deletions
@@ -36,6 +36,8 @@ use Espo\Core\Hook\Hook\AfterSave;
use Espo\Core\Hook\Hook\AfterUnrelate;
use Espo\Core\Hook\Hook\BeforeRemove;
use Espo\Core\Hook\Hook\BeforeSave;
use Espo\Core\Hook\Hook\LateAfterRemove;
use Espo\Core\Hook\Hook\LateAfterSave;
use Espo\ORM\Entity;
use Espo\ORM\Query\Select;
use Espo\ORM\Repository\Option\MassRelateOptions;
@@ -52,8 +54,10 @@ class GeneralInvoker
{
private const HOOK_BEFORE_SAVE = 'beforeSave';
private const HOOK_AFTER_SAVE = 'afterSave';
private const HOOK_LATE_AFTER_SAVE = 'lateAfterSave';
private const HOOK_BEFORE_REMOVE = 'beforeRemove';
private const HOOK_AFTER_REMOVE = 'afterRemove';
private const HOOK_LATE_AFTER_REMOVE = 'lateAfterRemove';
private const HOOK_AFTER_RELATE = 'afterRelate';
private const HOOK_AFTER_UNRELATE = 'afterUnrelate';
private const HOOK_AFTER_MASS_RELATE = 'afterMassRelate';
@@ -93,6 +97,16 @@ class GeneralInvoker
return;
}
if ($name === self::HOOK_LATE_AFTER_SAVE && $hook instanceof LateAfterSave) {
if (!$subject instanceof Entity) {
throw new LogicException();
}
$hook->lateAfterSave($subject, SaveOptions::fromAssoc($options));
return;
}
if ($name === self::HOOK_BEFORE_REMOVE && $hook instanceof BeforeRemove) {
if (!$subject instanceof Entity) {
throw new LogicException();
@@ -113,6 +127,16 @@ class GeneralInvoker
return;
}
if ($name === self::HOOK_LATE_AFTER_REMOVE && $hook instanceof LateAfterRemove) {
if (!$subject instanceof Entity) {
throw new LogicException();
}
$hook->lateAfterRemove($subject, RemoveOptions::fromAssoc($options));
return;
}
if ($name === self::HOOK_AFTER_RELATE && $hook instanceof AfterRelate) {
$relationName = $hookData['relationName'] ?? null;
$relatedEntity = $hookData['foreignEntity'] ?? null;
@@ -0,0 +1,51 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2026 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\Hook\Hook;
use Espo\ORM\Entity;
use Espo\ORM\Repository\Option\RemoveOptions;
/**
* A lateAfterRemove hook.
*
* @template TEntity of Entity = Entity
*
* @since 9.4.0
*/
interface LateAfterRemove
{
/**
* Processed after an entity is removed, after the transaction (if one is used).
*
* @param TEntity $entity An entity.
* @param RemoveOptions $options Options.
*/
public function lateAfterRemove(Entity $entity, RemoveOptions $options): void;
}
@@ -0,0 +1,51 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2026 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\Hook\Hook;
use Espo\ORM\Entity;
use Espo\ORM\Repository\Option\SaveOptions;
/**
* A lateAfterSave hook.
*
* @template TEntity of Entity = Entity
*
* @since 9.4.0
*/
interface LateAfterSave
{
/**
* Processed after an entity is saved, after the transaction (if one is used).
*
* @param TEntity $entity An entity.
* @param SaveOptions $options Options.
*/
public function lateAfterSave(Entity $entity, SaveOptions $options): void;
}
@@ -140,11 +140,11 @@ class Database extends RDBRepository
$this->entityManager->getTransactionManager()->run(function () use ($entity, $options) {
$this->saveInternal($entity, $options);
});
return;
} else {
$this->saveInternal($entity, $options);
}
$this->saveInternal($entity, $options);
$this->lateAfterSave($entity, $options);
}
/**
@@ -170,6 +170,17 @@ class Database extends RDBRepository
parent::save($entity, $options);
}
/**
* @param TEntity $entity
* @param array<string, mixed> $options
*/
private function lateAfterSave(Entity $entity, array $options): void
{
if (!$this->hooksDisabled && empty($options[SaveOption::SKIP_HOOKS])) {
$this->hookManager->process($this->entityType, 'lateAfterSave', $entity, $options);
}
}
/**
* Remove a record (mark as deleted).
*/
@@ -179,11 +190,22 @@ class Database extends RDBRepository
$this->entityManager->getTransactionManager()->run(function () use ($entity, $options) {
$this->removeInternal($entity, $options);
});
return;
} else {
$this->removeInternal($entity, $options);
}
$this->removeInternal($entity, $options);
$this->lateAfterRemove($entity, $options);
}
/**
* @param TEntity $entity
* @param array<string, mixed> $options
*/
private function lateAfterRemove(Entity $entity, array $options): void
{
if (!$this->hooksDisabled && empty($options[SaveOption::SKIP_HOOKS])) {
$this->hookManager->process($this->entityType, 'lateAfterRemove', $entity, $options);
}
}
/**