diff --git a/composer.json b/composer.json index bf4abf9..ae07aca 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/composer.lock b/composer.lock index e4c11b1..8559dcd 100644 --- a/composer.lock +++ b/composer.lock @@ -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", diff --git a/src/Db.php b/src/Db.php index b66403a..7d5a089 100644 --- a/src/Db.php +++ b/src/Db.php @@ -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(); diff --git a/src/EventDispatcher.php b/src/EventDispatcher.php new file mode 100644 index 0000000..33b6645 --- /dev/null +++ b/src/EventDispatcher.php @@ -0,0 +1,44 @@ +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); + } + } + } +} \ No newline at end of file diff --git a/tests/DbTest.php b/tests/DbTest.php index aaf9827..b7db0df 100644 --- a/tests/DbTest.php +++ b/tests/DbTest.php @@ -1,6 +1,7 @@ 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(); +}); \ No newline at end of file