Ejemplo n.º 1
0
 /**
  * @param ControllerReadyEvent $event
  *
  * @return array The value to assign to the function parameter.
  *
  * @throws HttpException If the object cannot be instantiated.
  */
 private function getParameters(ControllerReadyEvent $event)
 {
     $controller = $event->getRouteMatch()->getControllerReflection();
     $currentParameters = $event->getParameters();
     $newParameters = [];
     foreach ($controller->getParameters() as $parameter) {
         $name = $parameter->getName();
         if (isset($currentParameters[$name])) {
             $newParameters[$name] = $this->getParameterValue($parameter, $currentParameters[$name]);
         }
     }
     return $newParameters;
 }
Ejemplo n.º 2
0
 /**
  * @param \Brick\Http\Request $request The request to handle.
  *
  * @return \Brick\Http\Response The generated response.
  *
  * @throws \Brick\Http\Exception\HttpException If a route throws such an exception, or no route matches the request.
  * @throws \UnexpectedValueException           If a route or controller returned an invalid value.
  */
 private function handleRequest(Request $request)
 {
     $event = new IncomingRequestEvent($request);
     $this->eventDispatcher->dispatch(IncomingRequestEvent::class, $event);
     $match = $this->route($request);
     $event = new RouteMatchedEvent($request, $match);
     $this->eventDispatcher->dispatch(RouteMatchedEvent::class, $event);
     $controllerReflection = $match->getControllerReflection();
     $instance = null;
     $this->valueResolver->setRequest($request);
     $this->valueResolver->setParameters($match->getClassParameters());
     if ($controllerReflection instanceof \ReflectionMethod) {
         $className = $controllerReflection->getDeclaringClass()->getName();
         $instance = $this->injector->instantiate($className);
         $callable = $controllerReflection->getClosure($instance);
     } elseif ($controllerReflection instanceof \ReflectionFunction) {
         $callable = $controllerReflection->getClosure();
     } else {
         throw new \UnexpectedValueException('Unknown controller reflection type.');
     }
     $event = new ControllerReadyEvent($request, $match, $instance);
     $event->addParameters($match->getFunctionParameters());
     $this->eventDispatcher->dispatch(ControllerReadyEvent::class, $event);
     $response = $event->getResponse();
     if ($response === null) {
         $this->valueResolver->setParameters($event->getParameters());
         try {
             $result = $this->injector->invoke($callable);
             if ($result instanceof Response) {
                 $response = $result;
             } else {
                 $event = new NonResponseResultEvent($request, $match, $instance, $result);
                 $this->eventDispatcher->dispatch(NonResponseResultEvent::class, $event);
                 $response = $event->getResponse();
                 if ($response === null) {
                     throw $this->invalidReturnValue('controller', Response::class, $result);
                 }
             }
         } catch (HttpException $e) {
             $response = $this->handleHttpException($e, $request);
         } finally {
             $event = new ControllerInvocatedEvent($request, $match, $instance);
             $this->eventDispatcher->dispatch(ControllerInvocatedEvent::class, $event);
         }
     }
     $event = new ResponseReceivedEvent($request, $response, $match, $instance);
     $this->eventDispatcher->dispatch(ResponseReceivedEvent::class, $event);
     return $response;
 }