Exemple #1
0
 function testGenerateAdvanced()
 {
     $route = new Route('/users/{id}/{action}', '');
     $route->setRequirement('id', '\\d+');
     $generator = new Generator(RequestContext::create());
     $this->assertEquals('http://localhost/users/100/edit', $generator->generate($route, ['id' => 100, 'action' => 'edit']));
 }
Exemple #2
0
 /**
  * 匹配scheme
  * @param RouteInterface $route
  * @return boolean
  */
 protected function matchSchema(RouteInterface $route)
 {
     //没有scheme直接忽略
     if (empty($route->getSchemes())) {
         return true;
     }
     return in_array($this->context->getScheme(), $route->getSchemes());
 }
Exemple #3
0
 /**
  * 获取route的host
  * @param RouteInterface $route
  * @param array $parameters
  * @return string
  */
 protected function getRouteHost(RouteInterface $route, $parameters)
 {
     // 如果route没有主机域名限制则直接使用环境中主机
     $requireHost = $route->getHost();
     if (empty($requireHost)) {
         return $this->context->getHost();
     }
     // 有限制则根据route的host限制生成域名
     return $this->formatRouteHostOrPath($requireHost, $parameters, $route->getRequirements());
 }
Exemple #4
0
 function testSchemes()
 {
     $routes = new RouteCollection();
     $route = new Route('/foo/{id}/bar/{name}', '');
     $route->setSchemes(['https']);
     $routes->add($route);
     $context = RequestContext::create();
     $context->setScheme('https');
     $this->assertEquals(['https'], $route->getSchemes());
     $this->assertEquals('https', $context->getScheme());
     $matcher = new Matcher($context);
     $this->assertEquals($route, $matcher->match('/foo/100/bar/steven', $routes));
     $context->setScheme('http');
     $this->setExpectedExceptionRegExp('Slince\\Routing\\Exception\\RouteNotFoundException');
     $matcher->match('/foo/100/bar/steven', $routes);
 }
Exemple #5
0
 function testGenerate()
 {
     $context = RequestContext::create();
     $context->setHost('m.domain.com');
     $router = RouterFactory::create($context);
     $routeBuilder = $router->getRouteBuilder();
     $routeBuilder->add('/path', '');
     $routeBuilder->http('/users/{id}', ['name' => 'home.dash', 'action' => 'UsersController@dashboard'])->setRequirements(['id' => '\\d+', 'subdomain' => 'm'])->setHost('{subdomain}.domain.com')->setDefaults(['subdomain' => 'm']);
     $routeBuilder->prefix('user', function (RouteBuilder $routeBuilder) {
         $routeBuilder->http('/messages', 'MessagesController@index');
         $routeBuilder->http('/messages/{id}', 'MessagesController@show');
         $routeBuilder->prefix('me', function (RouteBuilder $routeBuilder) {
             $routeBuilder->http('/account', 'UsersController@me');
         });
     });
     $routeBuilder->prefix('admin', function (RouteBuilder $routeBuilder) {
         $routeBuilder->http('/dashboard/', 'HomeController@index');
     });
     $this->assertEquals('http://www.domain.com/users/100', $router->generateByName('home.dash', ['subdomain' => 'www', 'id' => 100], true));
     $this->assertEquals('http://m.domain.com/user/messages/100', $router->generateByAction('MessagesController@show', ['subdomain' => 'www', 'id' => 100], true));
     $this->assertEquals('/user/me/account', $router->generateByAction('UsersController@me'));
     $this->assertEquals('/admin/dashboard', $router->generateByAction('HomeController@index'));
 }
Exemple #6
0
 /**
  * 绑定request到routing的context
  * @param Request $request
  * @param RequestContext $context
  * @return RequestContext
  */
 protected function bindRequestToContext(Request $request, RequestContext $context)
 {
     $context->setBaseUrl($request->getBaseUrl());
     $context->setPathInfo($request->getPathInfo());
     $context->setMethod($request->getMethod());
     $context->setHost($request->getHost());
     $context->setScheme($request->getScheme());
     $context->setHttpPort($request->getPort());
     $context->setHttpPort($request->isSecure() ? null : $request->getPort());
     $context->setHttpsPort($request->isSecure() ? $request->getPort() : null);
     $context->setQueryString($request->server->get('QUERY_STRING', ''));
     return $context;
 }
Exemple #7
0
 function testCreate()
 {
     $this->assertInstanceOf('Slince\\Routing\\Matcher', Factory::createMatcher());
     $this->assertInstanceOf('Slince\\Routing\\Generator', Factory::createGenerator(RequestContext::create()));
     $this->assertInstanceOf('Slince\\Routing\\RouteCollection', Factory::createRoutes());
 }