/** * @param $job * @param $collection * @return callable */ private function getClosure($job, RouteCollection $collection) { $collection->addRoutes(['/' => ['use' => $job['controller'], 'ajax' => isset($job['ajax']) && $job['ajax'] ? true : false]], ['path' => ROOT . '/Views', 'namespace' => '']); $router = new Router($collection); $router->setUrl('/'); $router->match(); return function () use($router) { $router->callTarget(); $router->response->sendContent(); }; }
/** * @param $class * @return Route|RouteCollection|Router|mixed */ private function getClass($class) { switch ($class) { case 'JetFire\\Routing\\Route': return $this->router->route; case 'JetFire\\Routing\\Router': return $this->router; case 'JetFire\\Routing\\RouteCollection': return $this->router->collection; default: return call_user_func_array($this->router->getConfig()['di'], [$class]); } }
/** * @param $template */ public function setTemplateCallback($template) { $ext = explode('.', $template['engines'][$template['use']]['extension']); $ext = end($ext); $this->router->setConfig(['templateCallback' => [$ext => function (Route $route) use($template) { $this->app->addRule($template['view'], ['shared' => true]); $view = $this->app->get($template['view']); $dir = $route->getTarget('view_dir') == '' ? substr($route->getTarget('template'), 0, strrpos($route->getTarget('template'), '/')) : $route->getTarget('view_dir'); $view->setPath($dir); $view->setExtension($template['engines'][$template['use']]['extension']); $view->setTemplate(str_replace($dir, '', $route->getTarget('template'))); $data = isset($route->getTarget()['data']) ? $route->getTarget('data') : []; $flash = $this->app->get('session')->getSession()->allFlash(); foreach ($flash as $key => $content) { $data[$key] = $content; } $view->addData(isset($route->getParams()['data']) ? array_merge($route->getParams()['data'], $data) : $data); return $this->app->get('template')->getTemplate()->render($view); }]]); }
public function testGetResponseMethod() { $collection = new RouteCollection(); $collection->addRoutes(ROOT . '/Config/routes.php', ['view_dir' => ROOT . '/Views', 'ctrl_namespace' => 'JetFire\\Routing\\App\\Controllers']); $router = new Router($collection); $router->addMatcher(new ArrayMatcher($router)); $_SERVER['REQUEST_METHOD'] = 'GET'; $router->setUrl('/search'); $this->assertFalse($router->match()); $this->assertEquals(405, $router->response->getStatusCode()); }