Skip to content

Commit

Permalink
move hooks init into abstract Join
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jan 16, 2022
1 parent 0a3b04f commit 47e461a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 32 deletions.
27 changes: 23 additions & 4 deletions src/Model/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @method Model getOwner()
*/
class Join
abstract class Join
{
use DiContainerTrait;
use InitializerTrait {
Expand Down Expand Up @@ -284,6 +284,18 @@ protected function init(): void
protected function initJoinHooks(): void
{
$this->onHookToOwnerEntity(Model::HOOK_AFTER_UNLOAD, \Closure::fromCallable([$this, 'afterUnload']));

if ($this->reverse) {
$this->onHookToOwnerEntity(Model::HOOK_AFTER_INSERT, \Closure::fromCallable([$this, 'afterInsert']), [], -5);
$this->onHookToOwnerEntity(Model::HOOK_BEFORE_UPDATE, \Closure::fromCallable([$this, 'beforeUpdate']), [], -5);
$this->onHookToOwnerEntity(Model::HOOK_BEFORE_DELETE, \Closure::fromCallable([$this, 'doDelete']), [], -5);
} else {
$this->onHookToOwnerEntity(Model::HOOK_BEFORE_INSERT, \Closure::fromCallable([$this, 'beforeInsert']), [], -5);
$this->onHookToOwnerEntity(Model::HOOK_BEFORE_UPDATE, \Closure::fromCallable([$this, 'beforeUpdate']), [], -5);
$this->onHookToOwnerEntity(Model::HOOK_AFTER_DELETE, \Closure::fromCallable([$this, 'doDelete']));

$this->onHookToOwnerEntity(Model::HOOK_AFTER_LOAD, \Closure::fromCallable([$this, 'afterLoad']));
}
}

/**
Expand Down Expand Up @@ -494,12 +506,19 @@ public function setSaveBufferValue(Model $entity, string $fieldName, $value): vo
$this->saveBufferByOid[spl_object_id($entity)][$fieldName] = $value;
}

/**
* Clears id and save buffer.
*/
protected function afterUnload(Model $entity): void
{
$this->unsetId($entity);
$this->unsetSaveBuffer($entity);
}

abstract public function afterLoad(Model $entity): void;

abstract public function beforeInsert(Model $entity, array &$data): void;

abstract public function afterInsert(Model $entity): void;

abstract public function beforeUpdate(Model $entity, array &$data): void;

abstract public function doDelete(Model $entity): void;
}
16 changes: 0 additions & 16 deletions src/Persistence/Array_/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,6 @@
*/
class Join extends Model\Join
{
protected function initJoinHooks(): void
{
parent::initJoinHooks();

if ($this->reverse) {
$this->onHookToOwnerEntity(Model::HOOK_AFTER_INSERT, \Closure::fromCallable([$this, 'afterInsert']), [], -5);
$this->onHookToOwnerEntity(Model::HOOK_BEFORE_UPDATE, \Closure::fromCallable([$this, 'beforeUpdate']), [], -5);
$this->onHookToOwnerEntity(Model::HOOK_BEFORE_DELETE, \Closure::fromCallable([$this, 'doDelete']), [], -5);
} else {
$this->onHookToOwnerEntity(Model::HOOK_BEFORE_INSERT, \Closure::fromCallable([$this, 'beforeInsert']));
$this->onHookToOwnerEntity(Model::HOOK_BEFORE_UPDATE, \Closure::fromCallable([$this, 'beforeUpdate']));
$this->onHookToOwnerEntity(Model::HOOK_AFTER_DELETE, \Closure::fromCallable([$this, 'doDelete']));
$this->onHookToOwnerEntity(Model::HOOK_AFTER_LOAD, \Closure::fromCallable([$this, 'afterLoad']));
}
}

public function afterLoad(Model $entity): void
{
// we need to collect ID
Expand Down
12 changes: 0 additions & 12 deletions src/Persistence/Sql/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,6 @@ protected function initJoinHooks(): void
parent::initJoinHooks();

$this->onHookToOwnerBoth(Persistence\Sql::HOOK_INIT_SELECT_QUERY, \Closure::fromCallable([$this, 'initSelectQuery']));

if ($this->reverse) {
$this->onHookToOwnerEntity(Model::HOOK_AFTER_INSERT, \Closure::fromCallable([$this, 'afterInsert']));
$this->onHookToOwnerEntity(Model::HOOK_BEFORE_UPDATE, \Closure::fromCallable([$this, 'beforeUpdate']));
$this->onHookToOwnerEntity(Model::HOOK_BEFORE_DELETE, \Closure::fromCallable([$this, 'doDelete']), [], -5);
$this->onHookToOwnerEntity(Model::HOOK_AFTER_LOAD, \Closure::fromCallable([$this, 'afterLoad']));
} else {
$this->onHookToOwnerEntity(Model::HOOK_BEFORE_INSERT, \Closure::fromCallable([$this, 'beforeInsert']), [], -5);
$this->onHookToOwnerEntity(Model::HOOK_BEFORE_UPDATE, \Closure::fromCallable([$this, 'beforeUpdate']));
$this->onHookToOwnerEntity(Model::HOOK_AFTER_DELETE, \Closure::fromCallable([$this, 'doDelete']));
$this->onHookToOwnerEntity(Model::HOOK_AFTER_LOAD, \Closure::fromCallable([$this, 'afterLoad']));
}
}

/**
Expand Down

0 comments on commit 47e461a

Please sign in to comment.