ajout systeme d'evenement

This commit is contained in:
Gregory Letellier
2023-10-27 15:47:08 +02:00
parent 5c09234cf5
commit bc7fa33473
5 changed files with 111 additions and 3 deletions

View File

@@ -106,7 +106,8 @@ class Db
{
$stmt = $this->pdo->prepare($query->getQuery());
$stmt = $this->bindParams($stmt, $query->getData());
$stmt->execute();
EventDispatcher::emit("on.query",$query);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
@@ -114,6 +115,7 @@ class Db
{
$stmt = $this->pdo->prepare($query->getQuery());
$stmt = $this->bindParams($stmt, $query->getData());
EventDispatcher::emit("on.query",$query);
return $stmt->execute();
}
@@ -121,6 +123,7 @@ class Db
{
$stmt = $this->pdo->prepare($query->getQuery());
$stmt = $this->bindParams($stmt, $query->getData());
EventDispatcher::emit("on.query",$query);
$ok = $stmt->execute();
if ($ok) {
return $this->pdo->lastInsertId();

44
src/EventDispatcher.php Normal file
View File

@@ -0,0 +1,44 @@
<?php
namespace Kletellier\PdoWrapper;
use Evenement\EventEmitter;
class EventDispatcher
{
private static $instance = null;
private EventEmitter $emitter;
private function __construct()
{
$this->emitter = new EventEmitter();
}
private static function getInstance()
{
if (self::$instance == null) {
self::$instance = new EventDispatcher();
}
return self::$instance;
}
public static function __callStatic($name, $arguments)
{
if(count($arguments)==2)
{
$instance = self::getInstance();
$event = $arguments[0];
if($name=="on")
{
if(is_callable($arguments[1]))
{
$instance->emitter->on($event,$arguments[1]);
}
}
if($name=="emit")
{
$instance->emitter->emit($event,$arguments);
}
}
}
}