public function testGetArguments() { $resolver = new ControllerResolver(); $request = Request::create('/'); $controller = array(new self(), 'testGetArguments'); $this->assertEquals(array(), $resolver->getArguments($request, $controller), '->getArguments() returns an empty array if the method takes no arguments'); $request = Request::create('/'); $request->attributes->set('foo', 'foo'); $controller = array(new self(), 'controllerMethod1'); $this->assertEquals(array('foo'), $resolver->getArguments($request, $controller), '->getArguments() returns an array of arguments for the controller method'); $request = Request::create('/'); $request->attributes->set('foo', 'foo'); $controller = array(new self(), 'controllerMethod2'); $this->assertEquals(array('foo', null), $resolver->getArguments($request, $controller), '->getArguments() uses default values if present'); $request->attributes->set('bar', 'bar'); $this->assertEquals(array('foo', 'bar'), $resolver->getArguments($request, $controller), '->getArguments() overrides default values if provided in the request attributes'); $request = Request::create('/'); $request->attributes->set('foo', 'foo'); $request->attributes->set('foobar', 'foobar'); $controller = array(new self(), 'controllerMethod3'); try { $resolver->getArguments($request, $controller); $this->fail('->getArguments() throws a \\RuntimeException exception if it cannot determine the argument value'); } catch (\Exception $e) { $this->assertInstanceOf('\\RuntimeException', $e, '->getArguments() throws a \\RuntimeException exception if it cannot determine the argument value'); } }
public function testGetArguments() { $resolver = new ControllerResolver(); $request = Request::create('/'); $controller = array(new self(), 'testGetArguments'); $this->assertEquals(array(), $resolver->getArguments($request, $controller), '->getArguments() returns an empty array if the method takes no arguments'); $request = Request::create('/'); $request->attributes->set('foo', 'foo'); $controller = array(new self(), 'controllerMethod1'); $this->assertEquals(array('foo'), $resolver->getArguments($request, $controller), '->getArguments() returns an array of arguments for the controller method'); $request = Request::create('/'); $request->attributes->set('foo', 'foo'); $controller = array(new self(), 'controllerMethod2'); $this->assertEquals(array('foo', null), $resolver->getArguments($request, $controller), '->getArguments() uses default values if present'); $request->attributes->set('bar', 'bar'); $this->assertEquals(array('foo', 'bar'), $resolver->getArguments($request, $controller), '->getArguments() overrides default values if provided in the request attributes'); $request = Request::create('/'); $request->attributes->set('foo', 'foo'); $controller = function ($foo) { }; $this->assertEquals(array('foo'), $resolver->getArguments($request, $controller)); $request = Request::create('/'); $request->attributes->set('foo', 'foo'); $controller = function ($foo, $bar = 'bar') { }; $this->assertEquals(array('foo', 'bar'), $resolver->getArguments($request, $controller)); $request = Request::create('/'); $request->attributes->set('foo', 'foo'); $controller = new self(); $this->assertEquals(array('foo', null), $resolver->getArguments($request, $controller)); $request->attributes->set('bar', 'bar'); $this->assertEquals(array('foo', 'bar'), $resolver->getArguments($request, $controller)); $request = Request::create('/'); $request->attributes->set('foo', 'foo'); $request->attributes->set('foobar', 'foobar'); $controller = 'Symfony\\Component\\HttpKernel\\Tests\\some_controller_function'; $this->assertEquals(array('foo', 'foobar'), $resolver->getArguments($request, $controller)); $request = Request::create('/'); $request->attributes->set('foo', 'foo'); $request->attributes->set('foobar', 'foobar'); $controller = array(new self(), 'controllerMethod3'); if (version_compare(PHP_VERSION, '5.3.16', '==')) { $this->markTestSkipped('PHP 5.3.16 has a major bug in the Reflection sub-system'); } else { try { $resolver->getArguments($request, $controller); $this->fail('->getArguments() throws a \\RuntimeException exception if it cannot determine the argument value'); } catch (\Exception $e) { $this->assertInstanceOf('\\RuntimeException', $e, '->getArguments() throws a \\RuntimeException exception if it cannot determine the argument value'); } } $request = Request::create('/'); $controller = array(new self(), 'controllerMethod5'); $this->assertEquals(array($request), $resolver->getArguments($request, $controller), '->getArguments() injects the request'); }
/** * Run Application. */ public function run() { $request = $this['request'] = Request::createFromGlobals(); $routes = $this['routes']; $context = $this['request_context']; $context->fromRequest($this['request']); $matcher = new UrlMatcher($routes, $context); $resolver = new ControllerResolver(); try { $request->attributes->add($matcher->match($request->getPathInfo())); $controller = $resolver->getController($request); $arguments = $resolver->getArguments($request, $controller); if (is_array($controller) && $controller[0] instanceof \Task\Controllers\BaseController) { $controller[0]->setRequest($request); $controller[0]->setApplication($this); } $response = call_user_func_array($controller, $arguments); } catch (ResourceNotFoundException $e) { $response = new JsonResponse(["errors" => ["type" => "Not found"]], Response::HTTP_NOT_FOUND); } catch (\Exception $e) { $response = new JsonResponse(["errors" => ["type" => $e->getMessage(), "stacktrace" => $e->getTraceAsString()]], Response::HTTP_INTERNAL_SERVER_ERROR); } $response->headers->set("Access-Control-Allow-Origin", "*"); $response->headers->set("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS"); $response->headers->set("Access-Control-Allow-Headers", "Content-Type"); $response->headers->set("Server", "Test task REST"); return $response; }
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) { try { $this->request = $request; $pathInfo = $request->getPathInfo(); $this->loadRoutes($pathInfo); $this->loadGeneralConfig(); $this->loadZyncroAppConfig($pathInfo); $this->loadDatabaseConfig($pathInfo); $this->loadSecurityConfig($pathInfo); $this->loadTwig($pathInfo); $this->loadUtils(); $this->method = $request->getMethod(); $this->dispatcher->dispatch('request', new RequestEvent($request, $this->session, $this->securityConfig, $this->routes)); $this->loadApi(); $context = new Routing\RequestContext(); $matcher = new Routing\Matcher\UrlMatcher($this->routes, $context); $context->fromRequest($request); $request->attributes->add($matcher->match($request->getPathInfo())); $resolver = new ControllerResolver(); $controller = $resolver->getController($request); $arguments = $resolver->getArguments($request, $controller); $arguments[0] = $this; $response = call_user_func_array($controller, $arguments); } catch (Routing\Exception\ResourceNotFoundException $e) { $response = new Response('Not Found', 404); } catch (\Exception $e) { $response = new Response('An error occurred: ' . $e->getMessage(), 500); } return $response; }
/** * @param Request $request * @param Route $route * @param array $parameters * @return Response */ public function execute(Request $request, Route $route, $parameters) { $resolver = new HttpKernel\Controller\ControllerResolver(); $callback = $resolver->getController($request); $arguments = $resolver->getArguments($request, $callback); $controller = $callback[0]; $method = $callback[1]; $controller->on_start(); $response = $controller->runAction($method, $arguments); if ($response instanceof \Symfony\Component\HttpFoundation\Response) { // note, our RedirectResponse doesn't extend Response, it extends symfony2 response return $response; } elseif ($response instanceof \Concrete\Core\View\AbstractView) { $content = $response->render(); } else { $view = $controller->getViewObject(); if (is_object($view)) { $view->setController($controller); if (isset($view) && $view instanceof \Concrete\Core\View\AbstractView) { $content = $view->render(); } } } $response = new Response(); $response->setContent($content); return $response; }
/** * Run Application. */ public function run() { if (empty($this['request'])) { $this['request'] = Request::createFromGlobals(); } $routes = $this['routes']; $request = $this['request']; $context = $this['requestContext']; $context->fromRequest($this['request']); $matcher = new UrlMatcher($routes, $context); $resolver = new ControllerResolver(); try { $request->attributes->add($matcher->match($request->getPathInfo())); $controller = $resolver->getController($request); $arguments = $resolver->getArguments($request, $controller); if (is_array($controller) && $controller[0] instanceof BaseController) { $controller[0]->setRequest($request); $controller[0]->setApplication($this); } $response = call_user_func_array($controller, $arguments); } catch (ResourceNotFoundException $e) { $response = new Response('Not Found', 404); } catch (\Exception $e) { $response = new Response('An error occurred', 500); } return $response; }
/** * @requires PHP 5.6 */ public function testGetVariadicArguments() { $resolver = new ControllerResolver(); $request = Request::create('/'); $request->attributes->set('foo', 'foo'); $request->attributes->set('bar', array('foo', 'bar')); $controller = array(new VariadicController(), 'action'); $this->assertEquals(array('foo', 'foo', 'bar'), $resolver->getArguments($request, $controller)); }
private function loadResource() { $resolver = new ControllerResolver(); $controller = $resolver->getController($this->request); $arguments = $resolver->getArguments($this->request, $controller); $controller[0]->setRequest($this->request); $controller[0]->setConfiguration($this->conf); $controller[0]->init(); $response = call_user_func_array($controller, $arguments); return $response; }
public function handle($params, Request $request) { if (empty($params['_controller'])) { throw new Exception('The CallableHandler requires a non-empty controller value'); } $resolver = new ControllerResolver(); $request->attributes->add($params); $controller = $resolver->getController($request); $arguments = $resolver->getArguments($request, $controller); return call_user_func_array($controller, $arguments); }
public function execute(Request $request, \Concrete\Core\Routing\Route $route, $parameters) { $resolver = new HttpKernel\Controller\ControllerResolver(); $arguments = $resolver->getArguments($request, $this->callback); $callback_response = call_user_func_array($this->callback, $arguments); if ($callback_response instanceof \Concrete\Core\Http\Response) { return $callback_response; } $r = new Response(); $r->setContent($callback_response); return $r; }
/** * @param Request $request * @return Response */ public function handle(Request $request) { try { $request->attributes->add($this->matcher->match($request->getPathInfo())); $session = new Session(new NativeSessionStorage(array('cookie_lifetime' => 60 * 60))); $session->start(); $request->setSession($session); $controllerParams = $this->resolver->getController($request); $arguments = $this->resolver->getArguments($request, $controllerParams); /** @var Controller $controller */ $controller = $controllerParams[0]; $controller->setContainer($this->container); $response = call_user_func_array($controllerParams, $arguments); $this->dispatcher->dispatch('post.controller', new ResponseEvent($session)); return $response; } catch (ResourceNotFoundException $e) { return new Response('Not Found', 404); } catch (\Exception $e) { return new Response('An error occurred', 500); } }
/** * Determines which controller to call based on the current request * Sets the controller based on "page" (Wordpress' way of figuring out which plugin is called), * "orchestraController" and "orchestraAction" * * @param $request * @param $em * @param $twig * @param $formFactory */ public function __construct($request, $em, $twig, $formFactory) { $attributes = $request->query->all(); $attributes['_controller'] = Framework::$pluginNamespace . '\\Controller\\' . ucfirst($request->query->get('orchestraController', 'default')) . 'Controller::' . $request->query->get('orchestraAction', 'index') . 'Action'; unset($attributes['orchestraController']); unset($attributes['orchestraAction']); unset($attributes['page']); $resolver = new ControllerResolver(); $request->attributes->add($attributes); $controller = $resolver->getController($request); $controller[0]->init($request, $em, $twig, $formFactory); $arguments = $resolver->getArguments($request, $controller); $this->response = call_user_func_array($controller, $arguments); }
public static function start() { /* Loading preconfigured DataBased */ try { Muffin\System\DataBase::initDB(); } catch (Exception $e) { } /* Catching global variables, also we create the response */ $request = Request::createFromGlobals(); $response = new Response(); /* Loading the routes file and define the routes as the container of the routers definitions More info: http://symfony.com/doc/current/components/routing/introduction.html#load-routes-fr om-a-file */ $resolver = new HttpKernel\Controller\ControllerResolver(); $locator = new FileLocator(array(__DIR__)); $loader = new YamlFileLoader($locator); $routes = $loader->load('config/routes.yml'); $context = new RequestContext($request->getMethod()); try { $matcher = new UrlMatcher($routes, $context); $request->attributes->add($matcher->match($request->getPathInfo())); $controller = $resolver->getController($request); $arguments = $resolver->getArguments($request, $controller); $response = call_user_func_array($controller, $arguments); /* If the path doesn't exits */ } catch (Symfony\Component\Routing\Exception\ResourceNotFoundException $e) { $response = new Response('Not Found', 404); } catch (Exception $e) { $response = new Response('An error occurred' . $e, 500); } $response->send(); /* IF we created a connection to the DataBase we close it at the end */ try { Muffin\System\DataBase::close(); } catch (Exception $e) { } }
/** * @return array|bool */ protected function resolveController() { try { // doctrine mabo jambo to prepare route detection $context = new RequestContext(); $context->fromRequest($this->request); $matcher = new UrlMatcher($this->routeCollection, $context); // try detecting controller & stuff $this->request->attributes->add($matcher->match($this->request->getPathInfo())); // resolve controller $resolver = new ControllerResolver(); $controller = $resolver->getController($this->request); $controller = $this->assembleController($controller); // adding request and response variables to the controller if (!empty($controller[0]) && $controller[0] instanceof AbstractSimplexController) { $controller[0]->setRequest($this->request)->setResponse($this->response); } else { // or as attributes for a 'function' controller $req = array('request' => $this->request, 'response' => $this->response); $this->request->attributes->add($req); } // parsing arguments for the request and adding a last argument the request parameter itself $arguments = $resolver->getArguments($this->request, $controller); return array($controller, $arguments); } catch (ResourceNotFoundException $e) { } return false; }
<?php require_once dirname(__DIR__) . '/vendor/autoload.php'; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing; use Symfony\Component\HttpKernel; function render_template(Request $request) { extract($request->attributes->all(), EXTR_SKIP); ob_start(); include sprintf(__DIR__ . '/../src/pages/%s.php', $_route); return new Response(ob_get_clean()); } $request = Request::createFromGlobals(); $routes = (include __DIR__ . '/../src/app.php'); $context = new Routing\RequestContext(); $context->fromRequest($request); $matcher = new Routing\Matcher\UrlMatcher($routes, $context); $resolver = new HttpKernel\Controller\ControllerResolver(); try { $request->attributes->add($matcher->match($request->getPathInfo())); $controller = $resolver->getController($request); $arguments = $resolver->getArguments($request, $controller); $response = call_user_func_array($controller, $arguments); } catch (Routing\Exception\ResourceNotFoundException $e) { $response = new Response('Not found', 404); } catch (Exception $e) { $response = new Response('An error occurred', 500); } $response->send();
/** * @requires PHP 7.1 */ public function testGetNullableArgumentsWithDefaults() { $resolver = new ControllerResolver(); $request = Request::create('/'); $request->attributes->set('mandatory', 'mandatory'); $controller = array(new NullableController(), 'action'); $this->assertEquals(array(null, null, 'value', 'mandatory'), $resolver->getArguments($request, $controller)); }