From 495ea918408f870226ca5f699b4bf302f7ca81f3 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Mon, 25 Jul 2016 14:03:36 +0000 Subject: [PATCH] dblayer : allow to define the hook functions in the specification of the layer. Can be external ones git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2950 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- dblayer.php | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/dblayer.php b/dblayer.php index 62d649b..45dae89 100644 --- a/dblayer.php +++ b/dblayer.php @@ -64,6 +64,23 @@ class dblayer public $verifyOneFunc; /** Define the name of the method to use to verify all entries */ public $verifyAllFunc; + /** Define the name of the method of hook before read */ + public $hookprereadFunc; + /** Define the name of the method of hook after read */ + public $hookpostreadFunc; + /** Define the name of the method of hook before insert */ + public $hookpreinsertFunc; + /** Define the name of the method of hook after insert */ + public $hookpostinsertFunc; + /** Define the name of the method of hook before update */ + public $hookpreupdateFunc; + /** Define the name of the method of hook after update */ + public $hookpostupdateFunc; + /** Define the name of the method of hook before delete */ + public $hookpredeleteFunc; + /** Define the name of the method of hook after delete */ + public $hookpostdeleteFunc; + /** The verify unitary stack @param string $field The name of the field to test @@ -104,6 +121,14 @@ class dblayer { $this->verifyOneFunc = array ($this, "verifyOne"); $this->verifyAllFunc = array ($this, "verifyAll"); + $this->hookprereadFunc = array ($this, "hookpreread"); + $this->hookpostreadFunc = array ($this, "hookpostread"); + $this->hookpreinsertFunc = array ($this, "hookpreinsert"); + $this->hookpostinsertFunc = array ($this, "hookpostinsert"); + $this->hookpreupdateFunc = array ($this, "hookpreupdate"); + $this->hookpostupdateFunc = array ($this, "hookpostupdate"); + $this->hookpredeleteFunc = array ($this, "hookpredelete"); + $this->hookpostdeleteFunc = array ($this, "hookpostdelete"); $driver = @explode (":", $dsn); if (! isset ($driver[0])) @@ -609,7 +634,7 @@ class dblayer array_walk ($binds, function(&$value, $key) { $value = md5 ($value); }); - $dataOK = $this->hookpreinsert ($dataOK); + $dataOK = call_user_func ($this->hookpreinsertFunc, $dataOK); $req = "INSERT INTO $this->sep$this->tableprefix$this->table$this->sep "; $req .= "($this->sep". implode ("$this->sep,$this->sep", array_keys ($dataOK)). @@ -646,7 +671,7 @@ class dblayer exit; } $lastID = self::$instance[$this->dsn]->lastInsertId(); - $lastID = $this->hookpostinsert ($dataOK, $lastID); + $lastID = call_user_func ($this->hookpostinsertFunc, $dataOK, $lastID); return $lastID; } @@ -707,7 +732,9 @@ class dblayer foreach ($foreignSelect as $s) $foreignSelectCols[] = $s[0]; } - $this->hookpreread ($select, $display, $order, $whereOr, $foreignSelect); + call_user_func_array ($this->hookprereadFunc, + array (&$select, &$display, &$order, + &$whereOr, &$foreignSelect)); $req = "SELECT $this->sep"; $req .= implode ("$this->sep,$this->sep", $display); $req .= "$this->sep "; @@ -832,7 +859,7 @@ class dblayer $res = array (); while ($d = $st->fetch (PDO::FETCH_ASSOC)) $res[] = $d; - $res = $this->hookpostread ($res); + $res = call_user_func ($this->hookpostreadFunc, $res); return $res; } @@ -878,7 +905,7 @@ class dblayer $dataOK[$field] = $data[$field]; } - $dataOK = $this->hookpreupdate ($updatekey, $dataOK); + $dataOK = call_user_func ($this->hookpreupdateFunc, $updatekey, $dataOK); $req = "UPDATE $this->sep".$this->tableprefix."$this->table$this->sep SET "; $i = 0; foreach ($dataOK as $key=>$val) @@ -935,8 +962,8 @@ class dblayer $st->execute (); $nbLinesUpdated = $st->rowCount (); - $nbLinesUpdated = $this->hookpostupdate ($updatekey, $dataOK, - $nbLinesUpdated); + $nbLinesUpdated = call_user_func ($this->hookpostupdateFunc, $updatekey, + $dataOK, $nbLinesUpdated); return $nbLinesUpdated; } @@ -952,7 +979,7 @@ class dblayer throw new \Exception (dgettext("domframework", "No table name defined to delete in the table"), 500); - $deletekey = $this->hookpredelete ($deletekey); + $deletekey = call_user_func ($this->hookpredeleteFunc, $deletekey); $req = "DELETE FROM $this->sep$this->tableprefix$this->table$this->sep "; $req .= "WHERE $this->primary = :primary"; $st = self::$instance[$this->dsn]->prepare ($req); @@ -969,7 +996,8 @@ class dblayer throw new \Exception ($e->getMessage (), 500); } $nbLinesDeleted = $st->rowCount(); - $nbLinesDeleted = $this->hookpostdelete ($deletekey, $nbLinesDeleted); + $nbLinesDeleted = call_user_func ($this->hookpostdeleteFunc, $deletekey, + $nbLinesDeleted); return $nbLinesDeleted; }