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. * * @param Request $req * * @return bool */ final public function handle(Request $req) { $req->forDebugOnly = get_called_class(); $processed = $this->processing($req); if (!$processed) { // the request has not been processed by this handler => see the next if (!is_null($this->successor)) { $processed = $this->successor->handle($req); } } return $processed; } /** * Each concrete handler has to implement the processing of the request * * @param Request $req * * @return bool true if the request has been processed */ abstract protected function processing(Request $req); }