/** * @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 $routes * @param array $middleware */ public function setRoutes($routes, $middleware = []) { if (!empty($middleware) && is_array($middleware)) { $this->collection->setMiddleware($middleware); } foreach ($routes as $key => $block) { if (is_array($block)) { $path = substr($block['path'], -4) == '.php' ? $block['path'] : rtrim($block['path'], '/') . '/routes.php'; $block['view_dir'] = isset($block['view_dir']) ? $block['view_dir'] : rtrim($block['path'], '/') . '/Views'; $options = isset($block['prefix']) ? ['block' => $block['path'], 'view_dir' => $block['view_dir'], 'ctrl_namespace' => $block['namespace'] . '\\Controllers', 'prefix' => $block['prefix']] : ['block' => $block['path'], 'view_dir' => $block['view_dir'], 'ctrl_namespace' => $block['namespace'] . '\\Controllers']; if (isset($block['subdomain'])) { $options['subdomain'] = $block['subdomain']; } $this->collection->addRoutes($path, $options); } } }
/** * @description main function */ public function run() { $this->setUrl(); if ($this->config['generateRoutesPath']) { $this->collection->generateRoutesPath(); } if ($this->match()) { $this->callTarget(); } $this->callResponse(); }
/** * @depends testGenerateRoutes * @param RouteCollection $collection */ public function testGetPath(RouteCollection $collection) { $this->assertEquals('http://localhost/public/contact', $collection->getRoutePath('contact')); $this->assertNotEquals('http://localhost/contact', $collection->getRoutePath('search')); }
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()); }