From 5ef810f3d0e9bcd34d5bb5e28bd0bc852da0efed Mon Sep 17 00:00:00 2001 From: Trismegiste Date: Sun, 12 May 2013 20:52:29 +0200 Subject: [PATCH] add the generic handler and the request classes --- ChainOfResponsibilities/Handler.php | 58 +++++++++++++++++++++ ChainOfResponsibilities/Request.php | 23 ++++++++ Tests/ChainOfResponsibilities/ChainTest.php | 22 ++++++++ 3 files changed, 103 insertions(+) create mode 100644 ChainOfResponsibilities/Handler.php create mode 100644 ChainOfResponsibilities/Request.php create mode 100644 Tests/ChainOfResponsibilities/ChainTest.php diff --git a/ChainOfResponsibilities/Handler.php b/ChainOfResponsibilities/Handler.php new file mode 100644 index 0000000..43d08e3 --- /dev/null +++ b/ChainOfResponsibilities/Handler.php @@ -0,0 +1,58 @@ +successor)) { + $this->successor = $handler; + } else { + $this->successor->append($handler); + } + } + + /** + * Handle the request. + * + * This approach by using a template method pattern ensures you that + * each subclass will not forget to call the successor. Beside, the returned + * boolean value indicates you if the request have been processed or not. + */ + final public function handle(Request $req) + { + $processed = $this->processing($req); + if (!$processed) { + if (!is_null($this->successor)) { + $processed = $this->successor->handle($req); + } + } + + return $processed; + } + + abstract protected function processing(Request $req); +} \ No newline at end of file diff --git a/ChainOfResponsibilities/Request.php b/ChainOfResponsibilities/Request.php new file mode 100644 index 0000000..d1cddfc --- /dev/null +++ b/ChainOfResponsibilities/Request.php @@ -0,0 +1,23 @@ +