/** * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { $routeInfo = $this->dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath()); switch ($routeInfo[0]) { case Dispatcher::NOT_FOUND: $response = $response->withStatus(404); break; case Dispatcher::METHOD_NOT_ALLOWED: $response = $response->withStatus(405); break; case Dispatcher::FOUND: $callable = $routeInfo[1]; $args = $routeInfo[2]; if (isset($this->container)) { $response = $this->container->call($callable, ['request' => $request, 'response' => $response, 'arguments' => $args]); } else { if (is_callable($callable)) { call_user_func($callable, $request, $response, $args); } else { $callable = new $callable(); call_user_func($callable, $request, $response, $args); } } break; default: return $response->withStatus(500); } $response = $next($request, $response); return $response; }
public function dispatch() { // Clear the list of raised events. $raisedEvents = $this->raisedEvents; $this->raisedEvents = []; foreach ($raisedEvents as $event) { // Get the listeners for this event $listeners = $this->getListeners(get_class($event)); // Sort the listeners $sortedListeners = $this->sortListeners($listeners); // Call the listeners foreach ($sortedListeners as $listenerData) { if ($event->isPropagationStopped()) { return; } $this->invoker->call($listenerData['closure'], [$event]); } } }