Ejemplo n.º 1
0
 /**
  * Call
  *
  * Iterate each matching Route until all Routes are exhausted.
  *
  * @return void
  */
 public function call()
 {
     try {
         if (isset($this->environment['slim.flash'])) {
             $this->view()->setData('flash', $this->environment['slim.flash']);
         }
         $this->applyHook('slim.before');
         ob_start();
         $this->applyHook('slim.before.router');
         $dispatched = false;
         $httpMethodsAllowed = array();
         $this->router->getMatchedRoutes();
         foreach ($this->router as $route) {
             if ($route->supportsHttpMethod($this->environment['REQUEST_METHOD'])) {
                 try {
                     $this->applyHook('slim.before.dispatch');
                     $dispatched = $this->router->dispatch($route);
                     $this->applyHook('slim.after.dispatch');
                     if ($dispatched) {
                         break;
                     }
                 } catch (Slim_Exception_Pass $e) {
                     continue;
                 }
             } else {
                 $httpMethodsAllowed = array_merge($httpMethodsAllowed, $route->getHttpMethods());
             }
         }
         if (!$dispatched) {
             if ($httpMethodsAllowed) {
                 $this->response['Allow'] = implode(' ', $httpMethodsAllowed);
                 // This implementation discards the httpMethodsAllowed info.
                 // Maybe we should save it somewhere for use by methodNotAllowed?
                 $this->methodNotAllowed();
             } else {
                 $this->notFound();
             }
         }
         $this->applyHook('slim.after.router');
         $this->stop();
     } catch (Slim_Exception_Stop $e) {
         $this->response()->write(ob_get_clean());
         $this->applyHook('slim.after');
     } catch (Slim_Exception_RequestSlash $e) {
         $this->response->redirect($this->request->getPath() . '/', 301);
     } catch (Exception $e) {
         if ($this->config('debug')) {
             throw $e;
         } else {
             try {
                 $this->error($e);
             } catch (Slim_Exception_Stop $e) {
             }
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * Call
  *
  * Iterate each matching Route until all Routes are exhausted.
  *
  * @return void
  */
 public function call()
 {
     try {
         if (isset($this->environment['slim.flash'])) {
             $this->view()->setData('flash', $this->environment['slim.flash']);
         }
         $this->applyHook('slim.before');
         ob_start();
         $this->applyHook('slim.before.router');
         $dispatched = false;
         $httpMethodsAllowed = array();
         $this->router->setResourceUri($this->request->getResourceUri());
         $this->router->getMatchedRoutes();
         foreach ($this->router as $route) {
             if ($route->supportsHttpMethod($this->environment['REQUEST_METHOD'])) {
                 try {
                     $this->applyHook('slim.before.dispatch');
                     $dispatched = $this->router->dispatch($route);
                     $this->applyHook('slim.after.dispatch');
                     if ($dispatched) {
                         break;
                     }
                 } catch (Slim_Exception_Pass $e) {
                     continue;
                 }
             } else {
                 $httpMethodsAllowed = array_merge($httpMethodsAllowed, $route->getHttpMethods());
             }
         }
         if (!$dispatched) {
             if ($httpMethodsAllowed) {
                 $this->response['Allow'] = implode(' ', $httpMethodsAllowed);
                 $this->halt(405, 'HTTP method not allowed for the requested resource. Use one of these instead: ' . implode(', ', $httpMethodsAllowed));
             } else {
                 $this->notFound();
             }
         }
         $this->applyHook('slim.after.router');
         $this->stop();
     } catch (Slim_Exception_Stop $e) {
         $this->response()->write(ob_get_clean());
         $this->applyHook('slim.after');
     } catch (Slim_Exception_RequestSlash $e) {
         $this->response->redirect($this->request->getPath() . '/', 301);
     } catch (Exception $e) {
         if ($this->config('debug')) {
             throw $e;
         } else {
             try {
                 $this->error($e);
             } catch (Slim_Exception_Stop $e) {
             }
         }
     }
 }
 /**
  * Run the Slim application
  *
  * This method is the "meat and potatoes" of Slim and should be the last
  * method called. This fires up Slim, invokes the Route that matches
  * the current request, and returns the response to the client.
  *
  * This method will invoke the Not Found handler if no matching
  * routes are found.
  *
  * This method will also catch any unexpected Exceptions thrown by this
  * application; the Exceptions will be logged to this application's log
  * and rethrown to the global Exception handler.
  *
  * @return void
  */
 public function run()
 {
     try {
         $this->applyHook('slim.before');
         ob_start();
         $this->applyHook('slim.before.router');
         $dispatched = false;
         foreach ($this->router->getMatchedRoutes() as $route) {
             try {
                 $this->applyHook('slim.before.dispatch');
                 $dispatched = $route->dispatch();
                 $this->applyHook('slim.after.dispatch');
                 if ($dispatched) {
                     break;
                 }
             } catch (Slim_Exception_Pass $e) {
                 continue;
             }
         }
         if (!$dispatched) {
             $this->notFound();
         }
         $this->response()->write(ob_get_clean());
         $this->applyHook('slim.after.router');
         $this->view->getData('flash')->save();
         session_write_close();
         $this->response->send();
         $this->applyHook('slim.after');
     } catch (Slim_Exception_RequestSlash $e) {
         try {
             $this->redirect($this->request->getRootUri() . $this->request->getResourceUri() . '/', 301);
         } catch (Slim_Exception_Stop $e2) {
             //Ignore Slim_Exception_Stop and exit application context
         }
     } catch (Slim_Exception_Stop $e) {
         //Exit application context
     } catch (Exception $e) {
         $this->getLog()->error($e);
         try {
             if ($this->config('debug') === true) {
                 $this->halt(500, self::generateErrorMarkup($e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString()));
             } else {
                 $this->error($e);
             }
         } catch (Slim_Exception_Stop $e2) {
             //Ignore Slim_Exception_Stop and exit application context
         }
     }
 }
Ejemplo n.º 4
0
 /**
  * Router::urlFor
  */
 public function testRouterUrlFor()
 {
     $_SERVER['REQUEST_METHOD'] = 'GET';
     $request = new Slim_Http_Request();
     $router = new Slim_Router($request);
     $route1 = $router->map('/foo/bar', function () {
     }, 'GET');
     $route2 = $router->map('/foo/:one/:two', function () {
     }, 'GET');
     $route3 = $router->map('/foo/:one(/:two)', function () {
     }, 'GET');
     $route4 = $router->map('/foo/:one/(:two/)', function () {
     }, 'GET');
     $route5 = $router->map('/foo/:one/(:two/(:three/))', function () {
     }, 'GET');
     $route1->setName('route1');
     $route2->setName('route2');
     $route3->setName('route3');
     $route4->setName('route4');
     $route5->setName('route5');
     //Route
     $this->assertEquals('/foo/bar', $router->urlFor('route1'));
     //Route with params
     $this->assertEquals('/foo/foo/bar', $router->urlFor('route2', array('one' => 'foo', 'two' => 'bar')));
     $this->assertEquals('/foo/foo/:two', $router->urlFor('route2', array('one' => 'foo')));
     $this->assertEquals('/foo/:one/bar', $router->urlFor('route2', array('two' => 'bar')));
     //Route with params and optional segments
     $this->assertEquals('/foo/foo/bar', $router->urlFor('route3', array('one' => 'foo', 'two' => 'bar')));
     $this->assertEquals('/foo/foo', $router->urlFor('route3', array('one' => 'foo')));
     $this->assertEquals('/foo/:one/bar', $router->urlFor('route3', array('two' => 'bar')));
     $this->assertEquals('/foo/:one', $router->urlFor('route3'));
     //Route with params and optional segments
     $this->assertEquals('/foo/foo/bar/', $router->urlFor('route4', array('one' => 'foo', 'two' => 'bar')));
     $this->assertEquals('/foo/foo/', $router->urlFor('route4', array('one' => 'foo')));
     $this->assertEquals('/foo/:one/bar/', $router->urlFor('route4', array('two' => 'bar')));
     $this->assertEquals('/foo/:one/', $router->urlFor('route4'));
     //Route with params and optional segments
     $this->assertEquals('/foo/foo/bar/what/', $router->urlFor('route5', array('one' => 'foo', 'two' => 'bar', 'three' => 'what')));
     $this->assertEquals('/foo/foo/', $router->urlFor('route5', array('one' => 'foo')));
     $this->assertEquals('/foo/:one/bar/', $router->urlFor('route5', array('two' => 'bar')));
     $this->assertEquals('/foo/:one/bar/what/', $router->urlFor('route5', array('two' => 'bar', 'three' => 'what')));
     $this->assertEquals('/foo/:one/', $router->urlFor('route5'));
 }
Ejemplo n.º 5
0
 /**
  * Test that Router implements IteratorAggregate interface
  */
 public function testRouterImplementsIteratorAggregate()
 {
     $router = new Slim_Router($this->req, $this->res);
     $router->map('/bar', function () {
     })->via('GET');
     $router->map('/foo1', function () {
     })->via('POST');
     $router->map('/bar', function () {
     })->via('PUT');
     $router->map('/foo/bar/xyz', function () {
     })->via('DELETE');
     $iterator = $router->getIterator();
     $this->assertInstanceOf('ArrayIterator', $iterator);
     $this->assertEquals(2, $iterator->count());
 }
Ejemplo n.º 6
0
 /**
  * Get the URL for a named Route
  * @param   string          $name       The route name
  * @param   array           $params     Key-value array of URL parameters
  * @throws  RuntimeException            If named route does not exist
  * @return  string
  */
 public function urlFor($name, $params = array())
 {
     return $this->router->urlFor($name, $params);
 }
Ejemplo n.º 7
0
 /**
  * Test that router returns matched routes based on URI only, not
  * based on the HTTP method.
  */
 public function testRouterMatchesRoutesByUriOnly()
 {
     Slim_Environment::mock(array('REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '127.0.0.1', 'SCRIPT_NAME' => '', 'PATH_INFO' => '/foo', 'QUERY_STRING' => 'one=1&two=2&three=3', 'SERVER_NAME' => 'slim', 'SERVER_PORT' => 80, 'slim.url_scheme' => 'http', 'slim.input' => '', 'slim.errors' => fopen('php://stderr', 'w'), 'HTTP_HOST' => 'slim'));
     $this->env = Slim_Environment::getInstance();
     $this->req = new Slim_Http_Request($this->env);
     $this->res = new Slim_Http_Response();
     $router = new Slim_Router($this->req, $this->res);
     $router->map('/foo', function () {
     })->via('GET');
     $router->map('/foo', function () {
     })->via('POST');
     $router->map('/foo', function () {
     })->via('PUT');
     $router->map('/foo/bar/xyz', function () {
     })->via('DELETE');
     $this->assertEquals(3, count($router->getMatchedRoutes()));
 }
Ejemplo n.º 8
0
 public function testDispatchWithoutCallable()
 {
     Slim_Environment::mock(array('REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '127.0.0.1', 'SCRIPT_NAME' => '', 'PATH_INFO' => '/hello/josh', 'QUERY_STRING' => 'one=1&two=2&three=3', 'SERVER_NAME' => 'slim', 'SERVER_PORT' => 80, 'slim.url_scheme' => 'http', 'slim.input' => '', 'slim.errors' => fopen('php://stderr', 'w'), 'HTTP_HOST' => 'slim'));
     $env = Slim_Environment::getInstance();
     $req = new Slim_Http_Request($env);
     $router = new Slim_Router($req);
     $route = new Slim_Route('/hello/:name', 'foo');
     $route->matches($req->getResourceUri());
     //<-- Extracts params from resource URI
     $this->assertFalse($router->dispatch($route));
 }
Ejemplo n.º 9
0
 /**
  * Test that router returns matched routes based on URI only, not
  * based on the HTTP method.
  */
 public function testRouterMatchesRoutesByUriOnly()
 {
     $_SERVER['REQUEST_METHOD'] = 'GET';
     $_SERVER['REQUEST_URI'] = '/foo';
     $request = new Slim_Http_Request();
     $router = new Slim_Router($request);
     $router->map('/foo', function () {
     })->via('GET');
     $router->map('/foo', function () {
     })->via('POST');
     $router->map('/foo', function () {
     })->via('PUT');
     $router->map('/foo/bar/xyz', function () {
     })->via('DELETE');
     $this->assertEquals(3, count($router->getMatchedRoutes()));
 }
Ejemplo n.º 10
0
 /**
  * Run the Slim application
  *
  * This method is the "meat and potatoes" of Slim and should be the last
  * method called. This fires up Slim, invokes the Route that matches
  * the current request, and returns the response to the client.
  *
  * This method will invoke the Not Found handler if no matching
  * routes are found.
  *
  * This method will also catch any unexpected Exceptions thrown by this
  * application; the Exceptions will be logged to this application's log
  * and rethrown to the global Exception handler.
  *
  * @return void
  */
 public function run()
 {
     try {
         try {
             $this->applyHook('slim.before');
             ob_start();
             $this->applyHook('slim.before.router');
             $matchedRoutes = $this->router->getMatchedRoutes();
             $dispatched = false;
             $httpMethod = $this->request()->getMethod();
             $httpMethodsAllowed = array();
             foreach ($matchedRoutes as $route) {
                 if ($route->supportsHttpMethod($httpMethod)) {
                     try {
                         $this->applyHook('slim.before.dispatch');
                         $dispatched = $route->dispatch();
                         $this->applyHook('slim.after.dispatch');
                         if ($dispatched) {
                             break;
                         }
                     } catch (Slim_Exception_Pass $e) {
                         continue;
                     }
                 } else {
                     $httpMethodsAllowed = array_merge($httpMethodsAllowed, $route->getHttpMethods());
                 }
             }
             if (!$dispatched) {
                 if ($httpMethodsAllowed) {
                     $this->response()->header('Allow', implode(' ', $httpMethodsAllowed));
                     $this->halt(405);
                 } else {
                     $this->notFound();
                 }
             }
             $this->response()->write(ob_get_clean());
             $this->applyHook('slim.after.router');
             if ($this->view->getData('flash')) {
                 $this->view->getData('flash')->save();
             }
             session_write_close();
             $this->response->send();
             $this->applyHook('slim.after');
         } catch (Slim_Exception_RequestSlash $e) {
             $this->redirect($this->request->getRootUri() . $this->request->getResourceUri() . '/', 301);
         } catch (Exception $e) {
             if ($e instanceof Slim_Exception_Stop) {
                 throw $e;
             }
             $this->getLog()->error($e);
             if ($this->config('debug') === true) {
                 $this->halt(500, self::generateErrorMarkup($e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString()));
             } else {
                 $this->error($e);
             }
         }
     } catch (Slim_Exception_Stop $e) {
         //Exit application context
     }
 }