From 057f3316accfc3e968039c97d1d7f0918fd29f77 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Tue, 18 Jun 2024 17:10:40 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- src/db/Capacitor.php | 4 +-- src/db/CapacitorChannel.php | 2 +- src/db/CapacitorStorage.php | 53 +++++++++++++++++++++++++------------ 3 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/db/Capacitor.php b/src/db/Capacitor.php index 1b2ebc5..396fa17 100644 --- a/src/db/Capacitor.php +++ b/src/db/Capacitor.php @@ -50,8 +50,8 @@ class Capacitor { $this->storage->_reset($this->channel, $recreate); } - function charge($item, $func=null, ?array $args=null): int { - return $this->storage->_charge($this->channel, $item, $func, $args); + function charge($item, $func=null, ?array $args=null, ?array &$values=null): int { + return $this->storage->_charge($this->channel, $item, $func, $args, $values); } function discharge(bool $reset=true): iterable { diff --git a/src/db/CapacitorChannel.php b/src/db/CapacitorChannel.php index 696f03a..e8202e6 100644 --- a/src/db/CapacitorChannel.php +++ b/src/db/CapacitorChannel.php @@ -248,7 +248,7 @@ class CapacitorChannel { * Si $item est modifié dans cette méthode, il est possible de le retourner * avec la clé "item" pour mettre à jour la ligne correspondante. */ - function onCreate($item, array $rowValues): ?array { + function onCreate($item, array $rowValues, ?array $alwaysNull): ?array { return null; } diff --git a/src/db/CapacitorStorage.php b/src/db/CapacitorStorage.php index 871f177..5f072e6 100644 --- a/src/db/CapacitorStorage.php +++ b/src/db/CapacitorStorage.php @@ -178,7 +178,7 @@ EOT; $this->_reset($this->getChannel($channel), $recreate); } - function _charge(CapacitorChannel $channel, $item, $func, ?array $args): int { + function _charge(CapacitorChannel $channel, $item, $func, ?array $args, ?array &$values=null): int { $this->_create($channel); $tableName = $channel->getTableName(); $now = date("Y-m-d H:i:s"); @@ -204,10 +204,11 @@ EOT; "modified_" => $now, ]); $insert = true; - if ($func === null) $func = "->onCreate"; - func::ensure_func($func, $channel, $args); + $initFunc = [$channel, "onCreate"]; + $initArgs = $args; + func::ensure_func($initFunc, null, $initArgs); $values = $this->unserialize($channel, $row); - $args = [$item, $values, ...$args]; + $pvalues = null; } else { # modification if ($channel->_wasSumModified("item", $row, $prow)) { @@ -216,22 +217,36 @@ EOT; "modified_" => $now, ]); } - if ($func === null) $func = "->onUpdate"; - func::ensure_func($func, $channel, $args); + $initFunc = [$channel, "onUpdate"]; + $initArgs = $args; + func::ensure_func($initFunc, null, $initArgs); $values = $this->unserialize($channel, $row); $pvalues = $this->unserialize($channel, $prow); - $args = [$item, $values, $pvalues, ...$args]; } - $updates = func::call($func, ...$args); + $updates = func::call($initFunc, $item, $values, $pvalues, ...$initArgs); if (is_array($updates) && $updates) { if ($insert === null) $insert = false; if (!array_key_exists("modified_", $updates)) { $updates["modified_"] = $now; } + $values = cl::merge($values, $updates); $row = cl::merge($row, $this->serialize($channel, $updates)); } + if ($func !== null) { + func::ensure_func($func, $channel, $args); + $updates = func::call($func, $item, $values, $pvalues, ...$args); + if (is_array($updates) && $updates) { + if ($insert === null) $insert = false; + if (!array_key_exists("modified_", $updates)) { + $updates["modified_"] = $now; + } + $values = cl::merge($values, $updates); + $row = cl::merge($row, $this->serialize($channel, $updates)); + } + } + if ($insert === null) { # aucune modification return 0; @@ -255,20 +270,24 @@ EOT; /** * charger une valeur dans le canal * - * Si $func!==null, après avoir calculé les valeurs des clés supplémentaires - * avec {@link CapacitorChannel::getItemValues()}, la fonction est appelée - * avec la signature de {@link CapacitorChannel::onCreate()} ou - * {@link CapacitorChannel::onUpdate()} en fonction du type d'opération: - * création ou mise à jour + * Après avoir calculé les valeurs des clés supplémentaires + * avec {@link CapacitorChannel::getItemValues()}, l'une des deux fonctions + * {@link CapacitorChannel::onCreate()} ou {@link CapacitorChannel::onUpdate()} + * est appelée en fonction du type d'opération: création ou mise à jour * - * Si la fonction retourne un tableau, il est utilisé pour modifier les valeurs - * insérées/mises à jour + * Ensuite, si $func !== null, la fonction est appelée avec la signature de + * {@link CapacitorChannel::onCreate()} ou {@link CapacitorChannel::onUpdate()} + * en fonction du type d'opération: création ou mise à jour + * + * Dans les deux cas, si la fonction retourne un tableau, il est utilisé pour + * modifier les valeurs insérées/mises à jour. De plus, $values obtient la + * valeur finale des données insérées/mises à jour * * @return int 1 si l'objet a été chargé ou mis à jour, 0 s'il existait * déjà à l'identique dans le canal */ - function charge(?string $channel, $item, $func=null, ?array $args=null): int { - return $this->_charge($this->getChannel($channel), $item, $func, $args); + function charge(?string $channel, $item, $func=null, ?array $args=null, ?array &$values=null): int { + return $this->_charge($this->getChannel($channel), $item, $func, $args, $values); } function _discharge(CapacitorChannel $channel, bool $reset=true): iterable {