From 14f6044bbb3e2011dee41e239cb842fa16d9f8ff Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laurent=20L=C3=A9cluse?= <laurent.lecluse@unicaen.fr>
Date: Tue, 3 Dec 2024 13:52:42 +0100
Subject: [PATCH] 1.0.4 (03/12/2024) ------------------
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- Nouvelle option "callback" de Table->merge pour pouvoir suivre en temps réel l'évolution de l'opération
---
 CHANGELOG.md  |  6 ++++++
 src/Table.php | 41 +++++++++++++++++++++++++++++++++--------
 2 files changed, 39 insertions(+), 8 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2dbfc7c..cb1578a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,9 @@
+1.0.4 (03/12/2024)
+------------------
+
+- Nouvelle option "callback" de Table->merge pour pouvoir suivre en temps réel l'évolution de l'opération
+
+
 1.0.3 (29/11/2024)
 ------------------
 
diff --git a/src/Table.php b/src/Table.php
index 570835d..5b148df 100644
--- a/src/Table.php
+++ b/src/Table.php
@@ -60,7 +60,7 @@ class Table
 
     public function getDdlFromFile(): array
     {
-        [$schema,$name] = Util::explodedFullObjectName($this->getName());
+        [$schema, $name] = Util::explodedFullObjectName($this->getName());
 
         $ddlDir   = $this->getBdd()->getOption(Bdd::OPTION_DDL . '/' . Ddl::OPTION_DIR);
         $filename = $ddlDir . '/' . Ddl::TABLE . '/' . Util::fullObjectName($schema, $name) . '.php';
@@ -511,6 +511,7 @@ class Table
             'has-historique'     => $hasHistorique,
             'histo-user-id'      => null,
             'histo-date'         => new \DateTime(),
+            'callback'           => null, // Format function(string $action, int $progress, int $total, array $data=[], array $key=[]){ ... }
         ];
         if ($hasHistorique) {
             $defaultOptions['histo-user-id']                = $bdd->getHistoUserId();
@@ -559,6 +560,14 @@ class Table
             $new[$k] = $d;
         }
 
+        $callback   = $options['callback'];
+        $doCallback = $callback !== null;
+        if ($doCallback && !is_callable($callback)) {
+            throw new \Exception('L\'option callback doit être une fonction "callable"');
+        }
+        $callbackCount    = count($new);
+        $callbackProgress = 0;
+
 
         /* Chargement des données actuelles et traitement */
         $bdd->beginTransaction();
@@ -603,24 +612,32 @@ class Table
             if ($action) {
                 switch ($action) {
                     case 'delete':
-                        $this->delete($this->makeKeyArray($o, $key));
-
+                        $callbackCount++;
+                        $callbackProgress++;
+                        $key = [$this->makeKeyArray($o, $key)];
+                        $this->delete($key);
+                        if ($doCallback) call_user_func($callback, 'delete', $callbackProgress, $callbackCount, $o, $key);
                         break;
                     case 'soft-delete':
                         $toUpdate = [
                             $options['histo-destruction-column']    => $options['histo-date'],
                             $options['histo-destructeur-id-column'] => $options['histo-user-id'],
                         ];
-                        $this->update($toUpdate, $this->makeKeyArray($o, $key), ['ddl' => $ddl]);
-
+                        $callbackCount++;
+                        $callbackProgress++;
+                        $key = $this->makeKeyArray($o, $key);
+                        $this->update($toUpdate, $key, ['ddl' => $ddl]);
+                        if ($doCallback) call_user_func($callback, 'soft-delete', $callbackProgress, $callbackCount, $o, $key);
                         break;
                     case 'undelete':
                         $toUpdate = [
                             $options['histo-destruction-column']    => null,
                             $options['histo-destructeur-id-column'] => null,
                         ];
-                        $this->update($toUpdate, $this->makeKeyArray($o, $key), ['ddl' => $ddl]);
-
+                        $callbackProgress++;
+                        $key = $this->makeKeyArray($o, $key);
+                        $this->update($toUpdate, $key, ['ddl' => $ddl]);
+                        if ($doCallback) call_user_func($callback, 'undelete', $callbackProgress, $callbackCount, $o, $key);
                         break;
                     case 'update':
                         $toUpdate = $diff;
@@ -628,11 +645,17 @@ class Table
                             $toUpdate[$options['histo-modification-column']]    = $options['histo-date'];
                             $toUpdate[$options['histo-modificateur-id-column']] = $options['histo-user-id'];
                         }
-                        $this->update($toUpdate, $this->makeKeyArray($o, $key), ['ddl' => $ddl]);
+                        $callbackProgress++;
+                        $key = $this->makeKeyArray($o, $key);
+                        $this->update($toUpdate, $key, ['ddl' => $ddl]);
+                        if ($doCallback) call_user_func($callback, 'update', $callbackProgress, $callbackCount, $toUpdate, $key);
 
                         break;
                 }
                 $result[$action]++;
+            } else {
+                $callbackProgress++;
+                if ($doCallback) call_user_func($callback, 'same', $callbackProgress, $callbackCount);
             }
         }
 
@@ -645,7 +668,9 @@ class Table
                     $n[$options['histo-modification-column']]    = $options['histo-date'];
                     $n[$options['histo-modificateur-id-column']] = $options['histo-user-id'];
                 }
+                $callbackProgress++;
                 $this->insert($n);
+                if ($doCallback) call_user_func($callback, 'insert', $callbackProgress, $callbackCount, $n);
                 $result['insert']++;
                 if ($options['return-insert-data']) {
                     $insertedData = [];
-- 
GitLab