ajout systeme d'evenement
This commit is contained in:
@@ -3,7 +3,8 @@
|
||||
"description": "Pdo Wrapper for php with ActiveRecord system",
|
||||
"type": "library",
|
||||
"require": {
|
||||
"vlucas/phpdotenv": "^5.5"
|
||||
"vlucas/phpdotenv": "^5.5",
|
||||
"evenement/evenement": "^3.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"squizlabs/php_codesniffer": "^3.4",
|
||||
|
||||
49
composer.lock
generated
49
composer.lock
generated
@@ -4,8 +4,55 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "a7c52dc9c74ab6df807cb93281d8bd70",
|
||||
"content-hash": "7b31fc4374d54dc20d8f35625a8f74a7",
|
||||
"packages": [
|
||||
{
|
||||
"name": "evenement/evenement",
|
||||
"version": "v3.0.2",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/igorw/evenement.git",
|
||||
"reference": "0a16b0d71ab13284339abb99d9d2bd813640efbc"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/igorw/evenement/zipball/0a16b0d71ab13284339abb99d9d2bd813640efbc",
|
||||
"reference": "0a16b0d71ab13284339abb99d9d2bd813640efbc",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=7.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^9 || ^6"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Evenement\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Igor Wiedler",
|
||||
"email": "igor@wiedler.ch"
|
||||
}
|
||||
],
|
||||
"description": "Événement is a very simple event dispatching library for PHP",
|
||||
"keywords": [
|
||||
"event-dispatcher",
|
||||
"event-emitter"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/igorw/evenement/issues",
|
||||
"source": "https://github.com/igorw/evenement/tree/v3.0.2"
|
||||
},
|
||||
"time": "2023-08-08T05:53:35+00:00"
|
||||
},
|
||||
{
|
||||
"name": "graham-campbell/result-type",
|
||||
"version": "v1.1.1",
|
||||
|
||||
@@ -106,6 +106,7 @@ class Db
|
||||
{
|
||||
$stmt = $this->pdo->prepare($query->getQuery());
|
||||
$stmt = $this->bindParams($stmt, $query->getData());
|
||||
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
44
src/EventDispatcher.php
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use Kletellier\PdoWrapper\Db;
|
||||
use Kletellier\PdoWrapper\EventDispatcher;
|
||||
use Kletellier\PdoWrapper\Model;
|
||||
use Kletellier\PdoWrapper\QueryBuilder;
|
||||
|
||||
@@ -98,3 +99,15 @@ test("update row", function() {
|
||||
expect(count($rows))->toBe(1);
|
||||
expect($rows[0]->col1)->toBe("testupdate");
|
||||
});
|
||||
|
||||
test("event", function() {
|
||||
$ret = null;
|
||||
EventDispatcher::on("on.query",function($event,$query) use (&$ret) {
|
||||
$ret = $query;
|
||||
});
|
||||
|
||||
$qb = new QueryBuilder("test");
|
||||
$qb->find(1);
|
||||
|
||||
expect($ret)->toBeObject();
|
||||
});
|
||||
Reference in New Issue
Block a user