コード例 #1
0
ファイル: RouteTest.php プロジェクト: raynaldmo/php-education
 /**
  * Test route sets and gets middleware
  *
  * Pre-conditions:
  * Route instantiated
  *
  * Post-conditions:
  * Case A: Middleware set as callable, not array
  * Case B: Middleware set after other middleware already set
  * Case C: Middleware set as array of callables
  * Case D: Middleware set as a callable array
  * Case E: Middleware is invalid; throws InvalidArgumentException
  */
 public function testRouteMiddleware()
 {
     $callable1 = function () {
     };
     $callable2 = function () {
     };
     //Case A
     $r1 = new Slim_Route('/foo', function () {
     });
     $r1->setMiddleware($callable1);
     $mw = $r1->getMiddleware();
     $this->assertInternalType('array', $mw);
     $this->assertEquals(1, count($mw));
     //Case B
     $r1->setMiddleware($callable2);
     $mw = $r1->getMiddleware();
     $this->assertEquals(2, count($mw));
     //Case C
     $r2 = new Slim_Route('/foo', function () {
     });
     $r2->setMiddleware(array($callable1, $callable2));
     $mw = $r2->getMiddleware();
     $this->assertInternalType('array', $mw);
     $this->assertEquals(2, count($mw));
     //Case D
     $r3 = new Slim_Route('/foo', function () {
     });
     $r3->setMiddleware(array($this, 'callableTestFunction'));
     $mw = $r3->getMiddleware();
     $this->assertInternalType('array', $mw);
     $this->assertEquals(1, count($mw));
     //Case E
     try {
         $r3->setMiddleware('sdjfsoi788');
         $this->fail('Did not catch InvalidArgumentException when setting invalid route middleware');
     } catch (InvalidArgumentException $e) {
     }
 }
コード例 #2
0
 /**
  * Dispatch route
  *
  * This method invokes the route's callable. If middleware is
  * registered for the route, each callable middleware is invoked in
  * the order specified.
  *
  * This method is smart about trailing slashes on the route pattern.
  * If the route's pattern is defined with a trailing slash, and if the
  * current request URI does not have a trailing slash but otherwise
  * matches the route's pattern, a Slim_Exception_RequestSlash
  * will be thrown triggering an HTTP 301 Permanent Redirect to the same
  * URI _with_ a trailing slash. This Exception is caught in the
  * `Slim::call` loop. If the route's pattern is defined without a
  * trailing slash, and if the current request URI does have a trailing
  * slash, the route will not be matched and a 404 Not Found
  * response will be sent if no subsequent matching routes are found.
  *
  * @param   Slim_Route          $route  The route object
  * @return  bool Was route callable invoked successfully?
  * @throws  Slim_Exception_RequestSlash
  */
 public function dispatch(Slim_Route $route)
 {
     if (substr($route->getPattern(), -1) === '/' && substr($this->resourceUri, -1) !== '/') {
         throw new Slim_Exception_RequestSlash();
     }
     //Invoke middleware
     foreach ($route->getMiddleware() as $mw) {
         if (is_callable($mw)) {
             call_user_func($mw);
         }
     }
     //Invoke callable
     if (is_callable($route->getCallable())) {
         call_user_func_array($route->getCallable(), array_values($route->getParams()));
         return true;
     }
     return false;
 }