Example #1
0
 private function doGetArguments($controller, array $attributes, array $parameters)
 {
     $arguments = array();
     foreach ($parameters as $param) {
         if (array_key_exists($param->name, $attributes)) {
             $arguments[] = $attributes[$param->name];
         } elseif ($param->isDefaultValueAvailable()) {
             $arguments[] = $param->getDefaultValue();
         } else {
             throw new \RuntimeException(sprintf('Controller "%s" requires that you provide a value for the "$%s" argument (because there is no default value or because there is a non optional argument after this one).', Stringify::varToString($controller), $param->name));
         }
     }
     return $arguments;
 }
Example #2
0
 public function lookup(Request $request)
 {
     try {
         $parameters = $this->urlMatcher->matchRequest($request, $this->routes);
         $this->logger->info(sprintf('Matched route "%s" (parameters: %s)', $parameters['_route'], Stringify::parametersToString($parameters)));
         $request->attributes->add($parameters);
         unset($parameters['_route'], $parameters['_controller']);
         $request->attributes->set('_route_params', $parameters);
     } catch (ResourceNotFoundException $e) {
         $message = sprintf('No route found for "%s %s"', $request->getMethod(), $request->getPathInfo());
         if ($referer = $request->headers->get('referer')) {
             $message .= sprintf(' (from "%s")', $referer);
         }
         throw new NotFoundHttpException($message, $e);
     } catch (MethodNotAllowedException $e) {
         $message = sprintf('No route found for "%s %s": Method Not Allowed (Allow: %s)', $request->getMethod(), $request->getPathInfo(), implode(', ', $e->getAllowedMethods()));
         throw new MethodNotAllowedHttpException($e->getAllowedMethods(), $message, $e);
     }
 }
 public function resolve(Request $request)
 {
     try {
         $controller = $request->attributes->get('_controller');
         if (!$controller) {
             throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s". The route is wrongly configured.', $request->getPathInfo()));
         }
         $controller = $this->controllerResolver->getController($controller);
         $event = new FilterControllerEvent($controller, $request);
         $this->dispatcher->dispatch(KernelEvents::CONTROLLER, $event);
         $controller = $event->getController();
         $attributes = $request->attributes->all();
         $attributes['request'] = $request;
         $arguments = $this->controllerResolver->getArguments($controller, $attributes);
         return array($controller, $arguments);
     } catch (ControllerNotCallableException $exception) {
         throw new \InvalidArgumentException(sprintf('Controller "%s" for URI "%s" is not callable.', Stringify::varToString($controller), $request->getPathInfo()));
     }
 }
Example #4
0
 public function prepare($response, Request $request)
 {
     if (!$response instanceof Response) {
         $event = new GetResponseForControllerResultEvent($request, $response);
         $this->dispatcher->dispatch(KernelEvents::VIEW, $event);
         if ($event->hasResponse()) {
             $response = $event->getResponse();
         }
         if (!$response instanceof Response) {
             $msg = sprintf('The controller must return a response (%s given).', Stringify::varToString($response));
             // the user may have forgotten to return something
             if (null === $response) {
                 $msg .= ' Did you forget to add a return statement somewhere in your controller?';
             }
             throw new \LogicException($msg);
         }
     }
     $event = new FilterResponseEvent($request, $response);
     $this->dispatcher->dispatch(KernelEvents::RESPONSE, $event);
     return $event->getResponse();
 }
Example #5
0
 public function testResourceToString()
 {
     $resource = tmpfile();
     $this->assertEquals('Resource(stream)', Stringify::varToString($resource));
 }