Ejemplo n.º 1
0
 /**
  * Call
  * @param array $env
  * @return array[status, header, body]
  */
 public function call(&$env)
 {
     try {
         return $this->app->call($env);
     } catch (Exception $e) {
         $env['slim.log']->error($e);
         $response = new Slim_Http_Response($this->renderBody($env, $e), 500);
         return $response->finalize();
     }
 }
Ejemplo n.º 2
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
     }
 }
Ejemplo n.º 3
0
 /**
  * Call
  *
  * Iterate each matching Route until all Routes are exhausted.
  * Return an array of HTTP status, header, and body.
  *
  * @param   array   $env    Key-value array of environment properties
  * @return  array           [status, header, body]
  */
 public function call(&$env)
 {
     try {
         if (isset($env['slim.flash'])) {
             $this->view()->setData('flash', $env['slim.flash']);
         }
         $this->applyHook('slim.before');
         ob_start();
         $this->applyHook('slim.before.router');
         $dispatched = false;
         $httpMethodsAllowed = array();
         foreach ($this->router as $route) {
             if ($route->supportsHttpMethod($env['REQUEST_METHOD'])) {
                 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['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->response->write(ob_get_clean());
         $this->applyHook('slim.after');
         $this->stop();
     } catch (Slim_Exception_Stop $e) {
         $this->response()->write(ob_get_contents());
         return $this->response->finalize();
     } catch (Slim_Exception_RequestSlash $e) {
         $this->response->redirect($this->request->getPath() . '/', 301);
         return $this->response->finalize();
     } catch (Exception $e) {
         if ($this->config('debug')) {
             throw $e;
         } else {
             try {
                 $this->error($e);
             } catch (Slim_Exception_Stop $e) {
             }
             return $this->response->finalize();
         }
     }
 }
Ejemplo n.º 4
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) {
             }
         }
     }
 }
 /**
  * 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.º 6
0
 /**
  * Test sets and gets CookieJar
  */
 public function testSetsAndGetsCookieJar()
 {
     $r = new Slim_Http_Response(new Slim_Http_Request());
     $cj = new Slim_Http_CookieJar('secret');
     $r->setCookieJar($cj);
     $this->assertSame($cj, $r->getCookieJar());
 }
Ejemplo n.º 7
0
 /**
  * Test message for code when message exists
  */
 public function testMessageForCodeWithInvalidCode()
 {
     $this->assertNull(Slim_Http_Response::getMessageForCode(600));
 }
Ejemplo n.º 8
0
Archivo: Slim.php Proyecto: ntdt/Slim
 /**
  * Constructor
  *
  * @param   array $userSettings
  * @return  void
  */
 private function __construct( $userSettings = array() ) {
     $this->settings = array_merge(array(
         //Logging
         'log.enable' => false,
         'log.logger' => null,
         'log.path' => './logs',
         'log.level' => 4,
         //Debugging
         'debug' => true,
         //View
         'templates_dir' => './templates',
         'view' => 'Slim_View',
         //Settings for all cookies
         'cookies.lifetime' => '20 minutes',
         'cookies.path' => '/',
         'cookies.domain' => '',
         'cookies.secure' => false,
         'cookies.httponly' => false,
         //Settings for encrypted cookies
         'cookies.secret_key' => 'CHANGE_ME',
         'cookies.cipher' => MCRYPT_RIJNDAEL_256,
         'cookies.cipher_mode' => MCRYPT_MODE_CBC,
         'cookies.encrypt' => true,
         'cookies.user_id' => 'DEFAULT'
     ), $userSettings);
     $this->request = new Slim_Http_Request();
     $this->response = new Slim_Http_Response($this->request);
     $this->router = new Slim_Router($this->request);
     $this->response->setCookieJar(new Slim_Http_CookieJar($this->settings['cookies.secret_key'], array(
         'high_confidentiality' => $this->settings['cookies.encrypt'],
         'mcrypt_algorithm' => $this->settings['cookies.cipher'],
         'mcrypt_mode' => $this->settings['cookies.cipher_mode'],
         'enable_ssl' => $this->settings['cookies.secure']
     )));
 }
Ejemplo n.º 9
0
 /**
  * Test can have body
  *
  * Pre-conditions:
  * Case A: Status code = 100
  * Case B: Status code = 200
  * Case C: Status code = 204
  * Case D: Status code = 304
  *
  * Post-conditions:
  * Case A: false
  * Case B: true
  * Case C: false
  * Case D: false
  */
 public function testCanHaveBody()
 {
     $r1 = new Slim_Http_Response(new Slim_Http_Request());
     //Case A
     $r1->status(100);
     $this->assertFalse($r1->canHaveBody());
     //Case B
     $r1->status(200);
     $this->assertTrue($r1->canHaveBody());
     //Case C
     $r1->status(204);
     $this->assertFalse($r1->canHaveBody());
     //Case D
     $r1->status(304);
     $this->assertFalse($r1->canHaveBody());
 }
Ejemplo n.º 10
0
 /**
  * Save session
  * @param   int     $status
  * @param   array   $header
  * @param   string  $body
  * @return  array[status, header, body]
  */
 protected function saveSession(&$env, $status, $header, $body)
 {
     $r = new Slim_Http_Response($body, $status, $header);
     $value = Slim_Http_Util::encodeSecureCookie(serialize($_SESSION), $this->settings['expires'], $this->settings['secret'], $this->settings['cipher'], $this->settings['cipher_mode']);
     if (strlen($value) > 4096) {
         fwrite($env['slim.errors'], 'WARNING! Slim_Middleware_SessionCookie data size is larger than 4KB. Content save failed.');
     } else {
         $r->setCookie($this->settings['name'], $value, $this->settings['expires'], $this->settings['path'], $this->settings['domain'], $this->settings['secure'], $this->settings['httponly']);
     }
     return $r->finalize();
 }
 /**
  * Test can set HTTP version
  */
 public function testCannotSetInvalidHttpVersion()
 {
     $this->setExpectedException('InvalidArgumentException');
     $r = new Slim_Http_Response(new Slim_Http_Request());
     $r->httpVersion('1.2');
 }
Ejemplo n.º 12
0
 /**
  * Test response body if HEAD request
  *
  * Pre-conditions:
  * HTTP method is HEAD
  *
  * Post-conditions:
  * Response body is NOT set;
  * Response headers are set;
  */
 function testResponseBodyIfHeadRequest() {
     $this->expectOutputString('');
     $_SERVER['REQUEST_METHOD'] = 'HEAD';
     $req = new Slim_Http_Request();
     $res = new Slim_Http_Response($req);
     $res->body('This is a test body');
     $res->send();
     $this->assertEquals('text/html', $res->header('Content-Type'));
     $this->assertEquals(19, $res->header('Content-Length'));
 }