Exemplo n.º 1
0
 /**
  * Create a URL object.
  *
  * @param string|Url|Request $request
  * @param array $routes
  */
 public function __construct($request = null, array $routes = [])
 {
     if (is_string($request) || is_a($request, static::class)) {
         $request = (string) $request;
         $this->request = Request::create($request);
         if (false !== strpos($request, '#')) {
             $this->fragment = (string) S::create($request)->substr(strpos($request, '#'))->removeLeft('#');
         }
     } elseif (is_a($request, Request::class)) {
         $this->request = $request;
     } else {
         $this->request = Request::createFromGlobals();
     }
     $this->routes = $routes;
     $this->scheme = $this->request->getScheme();
     $this->user = $this->request->getUser();
     $this->password = $this->request->getPassword();
     $this->host = $this->request->getHost();
     $this->port = $this->request->getPort();
     $this->path = array_filter(explode('/', $this->request->getPathInfo()));
     parse_str($this->request->getQueryString(), $this->query);
 }
Exemplo n.º 2
0
 public function testRouteResponseType()
 {
     $_SERVER['PATH_INFO'] = '/project/22';
     $request = new Request(new Env(), new Route('/login', 'controller/action'));
     $this->assertEquals(Request::HTML, $request->response_type());
     $this->assertEquals('/project/22', $request->path());
     $this->assertTrue($request->is_html());
     $this->assertFalse($request->is_json());
     $this->assertFalse($request->is_xml());
     $_SERVER['PATH_INFO'] = '/project/23.json';
     $request = new Request(new Env(), new Route('/project/23', 'controller/action'));
     $this->assertEquals(Request::JSON, $request->response_type());
     $this->assertEquals('/project/23', $request->path());
     $this->assertFalse($request->is_html());
     $this->assertTrue($request->is_json());
     $this->assertFalse($request->is_xml());
     $_SERVER['PATH_INFO'] = '/project/24.html';
     $request = new Request(new Env(), new Route('/login', 'controller/action'));
     $this->assertEquals(Request::HTML, $request->response_type());
     $this->assertEquals('/project/24', $request->path());
     $this->assertTrue($request->is_html());
     $this->assertFalse($request->is_json());
     $this->assertFalse($request->is_xml());
     $_SERVER['PATH_INFO'] = '/project/25.xml';
     $request = new Request(new Env(), new Route('/profile', 'controller/action'));
     $this->assertEquals(Request::XML, $request->response_type());
     $this->assertEquals('/project/25', $request->path());
     $this->assertFalse($request->is_html());
     $this->assertFalse($request->is_json());
     $this->assertTrue($request->is_xml());
     $_SERVER['PATH_INFO'] = '/project/25.xml';
     $_POST['_format'] = 'json';
     $request = new Request(new Env(), new Route('/profile', 'controller/action'));
     $this->assertEquals(Request::JSON, $request->response_type());
     $this->assertEquals('/project/25', $request->path());
     $this->assertFalse($request->is_html());
     $this->assertTrue($request->is_json());
     $this->assertFalse($request->is_xml());
 }