From a70bccbeb80beb9dc6808c9a29359e8fd2916ca1 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Tue, 21 May 2024 23:47:00 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- src/db/Capacitor.php | 2 +- src/db/CapacitorChannel.php | 35 +++++++++++++++++++++++++++++++ src/db/CapacitorStorage.php | 12 +++++------ src/db/sqlite/SqliteCapacitor.php | 34 ++++++++++++++++-------------- 4 files changed, 60 insertions(+), 23 deletions(-) diff --git a/src/db/Capacitor.php b/src/db/Capacitor.php index c094d6a..883d3a8 100644 --- a/src/db/Capacitor.php +++ b/src/db/Capacitor.php @@ -37,7 +37,7 @@ class Capacitor { return $this->storage->_get($this->channel, $filter); } - function each($filter, callable $func, ?array $args=null): void { + function each($filter, ?callable $func=null, ?array $args=null): void { $this->storage->_each($this->channel, $filter, $func, $args); } diff --git a/src/db/CapacitorChannel.php b/src/db/CapacitorChannel.php index 23bd043..3d103f4 100644 --- a/src/db/CapacitorChannel.php +++ b/src/db/CapacitorChannel.php @@ -59,4 +59,39 @@ class CapacitorChannel { function getKeyValues($item): ?array { return null; } + + /** + * méthode appelée lors du chargement d'un élément avec + * {@link Capacitor::charge()} + * + * @param mixed $item l'élément à charger + * @param array $values les valeurs calculées par {@link getKeyValues()} + * @param ?array $row la ligne à mettre à jour. vaut null s'il faut insérer + * une nouvelle ligne + * @return ?array le cas échéant, un tableau non null à marger dans $values et + * utiliser pour provisionner la ligne nouvelle créée, ou mettre à jour la + * ligne existante + * + * 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 onCharge($item, array $values, ?array $row): ?array { + return null; + } + + /** + * méthode appelée lors du parcours des éléments avec + * {@link Capacitor::each()} + * + * @param mixed $item l'élément courant + * @param ?array $row la ligne à mettre à jour + * @return ?array le cas échéant, un tableau non null utilisé pour mettre à + * jour la ligne courante + * + * 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 onEach($item, array $row): ?array { + return null; + } } diff --git a/src/db/CapacitorStorage.php b/src/db/CapacitorStorage.php index f97ee06..d9428b1 100644 --- a/src/db/CapacitorStorage.php +++ b/src/db/CapacitorStorage.php @@ -29,8 +29,8 @@ abstract class CapacitorStorage { * * Si $func!==null, après avoir calculé les valeurs des clés supplémentaires * avec {@link CapacitorChannel::getKeyValues()}, la fonction est appelée avec - * les arguments ($item, $keyValues, $row, ...$args) - * Si la fonction retourne un tableau, il est utilisé pour modifié les valeurs + * la signature ($item, $keyValues, $row, ...$args) + * Si la fonction retourne un tableau, il est utilisé pour modifier les valeurs * insérées/mises à jour * * @return true si l'objet a été chargé ou mis à jour, false s'il existait @@ -58,17 +58,17 @@ abstract class CapacitorStorage { return $this->_get($this->getChannel($channel), $filter); } - abstract function _each(CapacitorChannel $channel, $filter, callable $func, ?array $args): void; + abstract function _each(CapacitorChannel $channel, $filter, ?callable $func, ?array $args): void; /** * appeler une fonction pour chaque élément du canal spécifié. * * $filter permet de filtrer parmi les élements chargés * - * si $func retourne un tableau, il est utilisé pour mettre à jour - * l'enregistrement. + * $func est appelé avec la signature ($item, $row, ...$args). si la fonction + * retourne un tableau, il est utilisé pour mettre à jour la ligne */ - function each(?string $channel, $filter, callable $func, ?array $args=null): void { + function each(?string $channel, $filter, ?callable $func=null, ?array $args=null): void { $this->_each($this->getChannel($channel), $filter, $func, $args); } diff --git a/src/db/sqlite/SqliteCapacitor.php b/src/db/sqlite/SqliteCapacitor.php index babe130..cc03e96 100644 --- a/src/db/sqlite/SqliteCapacitor.php +++ b/src/db/sqlite/SqliteCapacitor.php @@ -110,22 +110,23 @@ class SqliteCapacitor extends CapacitorStorage { ]); $insert = false; } - if ($func !== null) { - $context = func::_prepare($func); - $args ??= []; - $updates = func::_call($context, [$item, $values, $row, ...$args]); - if (is_array($updates)) { - if (array_key_exists("_item", $updates)) { - $_item = serialize($updates["_item"]); - $updates["_item"] = $_item; - $updates["_sum"] = sha1($_item); - if (!array_key_exists("_modified", $updates)) { - $updates["_modified"] = $now; - } + + if ($func === null) $func = [$channel, "onCharge"]; + $onCharge = func::_prepare($func); + $args ??= []; + $updates = func::_call($onCharge, [$item, $values, $row, ...$args]); + if (is_array($updates)) { + if (array_key_exists("_item", $updates)) { + $_item = serialize($updates["_item"]); + $updates["_item"] = $_item; + $updates["_sum"] = sha1($_item); + if (!array_key_exists("_modified", $updates)) { + $updates["_modified"] = $now; } - $values = cl::merge($values, $updates); } + $values = cl::merge($values, $updates); } + if ($insert === null) { # aucune modification return false; @@ -173,9 +174,10 @@ class SqliteCapacitor extends CapacitorStorage { else return unserialize($row["_item"]); } - function _each(CapacitorChannel $channel, $filter, callable $func, ?array $args): void { + function _each(CapacitorChannel $channel, $filter, ?callable $func, ?array $args): void { + if ($func === null) $func = [$channel, "onEach"]; + $onEach = func::_prepare($func); if ($filter !== null && !is_array($filter)) $filter = ["_id" => $filter]; - $context = func::_prepare($func); $sqlite = $this->sqlite; $tableName = $channel->getTableName(); $commited = false; @@ -189,7 +191,7 @@ class SqliteCapacitor extends CapacitorStorage { $args ??= []; foreach ($rows as $row) { $item = unserialize($row['_item']); - $updates = func::_call($context, [$item, $row, ...$args]); + $updates = func::_call($onEach, [$item, $row, ...$args]); if (is_array($updates)) { if (array_key_exists("_item", $updates)) { $updates["_item"] = serialize($updates["_item"]);