Пример #1
0
 public function testCompositor()
 {
     $compositor = new Compositor();
     $compositor->enable('Pux\\Middleware\\TryCatchMiddleware', ['throw' => true]);
     $compositor->enable(function ($app) {
         return function (array &$environment, array $response) use($app) {
             $environment['middleware.app'] = true;
             return $app($environment, $response);
         };
     });
     // TODO
     // $compositor->mount('/foo', function() {  });
     $compositor->app(function (array &$environment, array $response) {
         $request = RouteRequest::createFromEnv($environment);
         // $mux = new Mux;
         if ($request->pathStartWith('/foo')) {
         }
         $response[0] = 200;
         return $response;
     });
     $app = $compositor->wrap();
     $env = Utils::createEnv('GET', '/foo');
     $res = [];
     $res = $app($env, $res);
     $this->assertNotEmpty($res);
     $this->assertEquals(200, $res[0]);
 }
Пример #2
0
 public function testMatchPathSuffix()
 {
     $request = RouteRequest::create('GET', '/foo/bar.json');
     $this->assertNotNull($request);
     $this->assertTrue($request->pathEndWith('.json'));
     $this->assertFalse($request->pathEndWith('.js'));
     $this->assertFalse($request->pathEndWith('.xml'));
 }
Пример #3
0
 public function testMiddleware()
 {
     $app = function (array $environment, array $response) {
         $request = RouteRequest::createFromEnv($environment);
         return $response;
     };
     $middleware = new TryCatchMiddleware($app);
     $middleware2 = new TryCatchMiddleware($middleware);
     // $request = RouteRequest::create('GET', '/path');
     $env = Utils::createGlobals('GET', '/foo');
     $response = $middleware2($env, [200, [], []]);
     $this->assertNotEmpty($response);
 }
Пример #4
0
 /**
  * Find a matched route with the path constraint in the current mux object.
  *
  * @param string       $path
  * @param RouteRequest $request
  *
  * @return array
  */
 public function match($path, RouteRequest $request = null)
 {
     $requestMethod = null;
     if ($request) {
         $requestMethod = self::convertRequestMethodConstant($request->getRequestMethod());
     } else {
         if (isset($_SERVER['REQUEST_METHOD'])) {
             $requestMethod = self::convertRequestMethodConstant($_SERVER['REQUEST_METHOD']);
         }
     }
     foreach ($this->routes as $route) {
         // If the route is using pcre pattern marching...
         if ($route[0]) {
             if (!preg_match($route[1], $path, $matches)) {
                 continue;
             }
             $route[3]['vars'] = $matches;
             // validate request method
             if (isset($route[3]['method']) && $route[3]['method'] != $requestMethod) {
                 continue;
             }
             if (isset($route[3]['domain']) && $route[3]['domain'] != $_SERVER['HTTP_HOST']) {
                 continue;
             }
             if (isset($route[3]['secure']) && $route[3]['secure'] && $_SERVER['HTTPS']) {
                 continue;
             }
             return $route;
         } else {
             // prefix match is used when expanding is not enabled.
             // TODO: Handle full match here..
             if ((is_int($route[2]) || $route[2] instanceof self || $route[2] instanceof \PHPSGI\App) && strncmp($route[1], $path, strlen($route[1])) === 0 || $route[1] == $path) {
                 // validate request method
                 if (isset($route[3]['method']) && $route[3]['method'] != $requestMethod) {
                     continue;
                 }
                 if (isset($route[3]['domain']) && $route[3]['domain'] != $_SERVER['HTTP_HOST']) {
                     continue;
                 }
                 if (isset($route[3]['secure']) && $route[3]['secure'] && $_SERVER['HTTPS']) {
                     continue;
                 }
                 return $route;
             }
             continue;
         }
     }
 }