public function test_handle()
 {
     $exceptionhandler = new midgardmvc_core_exceptionhandler();
     try {
         $data = midgardmvc_core::get_instance()->templating->dynamic_call('/subdir', 'missing_route', array());
     } catch (Exception $e) {
         ob_start();
         $exceptionhandler->handle($e);
         $errorpage = ob_get_clean();
         $this->assertTrue(strpos($errorpage, '<body class="OutOfRangeException">') !== false);
     }
 }
 public function __invoke($context)
 {
     if (method_exists($this->mgd, 'reopen')) {
         // making sure, that db-connection is still active
         $this->mgd->reopen();
     }
     // setting emulated superglobals
     $_SERVER = $context['env'];
     $_COOKIE = $context['_COOKIE'];
     $_GET = $context['_GET'];
     if (isset($context['_POST'])) {
         $_POST = $context['_POST'];
         if (isset($context['_FILES'])) {
             $_FILES = $context['_FILES'];
         }
     }
     // starting processing
     try {
         $mvc = midgardmvc_core::get_instance();
         $mvc->dispatcher->set_request_data($context);
         // call_user_func($context['logger'], "-> starting midgardmvc");
         try {
             ob_start();
             $request = $mvc->process();
             $mvc->serve($request);
             $body = ob_get_clean();
         } catch (StartNewRequestException $e) {
             $body = ob_get_clean();
             // call_user_func($context['logger'], "--> [!] StartNewRequestException exception arrived");
         } catch (midgardmvc_exception $e) {
             ob_end_clean();
             try {
                 ob_start();
                 midgardmvc_core_exceptionhandler::handle($e);
                 $body = ob_get_clean();
             } catch (Exception $e) {
                 ob_end_clean();
                 call_user_func($context['logger'], "--> [!] " . get_class($e) . " exception arrived");
                 throw $e;
             }
         } catch (Exception $e) {
             ob_end_clean();
             call_user_func($context['logger'], "--> [!] " . get_class($e) . " exception arrived");
             throw $e;
         }
         // call_user_func($context['logger'], "-> done with midgardmvc");
         return array($mvc->dispatcher->_get_status(), $mvc->dispatcher->_get_headers(), $body);
     } catch (Exception $e) {
         echo $e;
         return array(500, array('Content-type', 'text/plain'), "Internal Server Error \n" . $e->getMessage());
     }
 }
 public function __invoke($context)
 {
     // setting emulated superglobals
     $_SERVER = $context['env'];
     $_COOKIE = $context['_COOKIE'];
     if (isset($context['_POST'])) {
         $_POST = $context['_POST'];
         if (isset($context['_FILES'])) {
             $_FILES = $context['_FILES'];
         }
     }
     // starting processing
     try {
         $mvc = midgardmvc_core::get_instance('appserv');
         $mvc->dispatcher->set_request_data($context);
         call_user_func($context['logger'], "-> starting midgardmvc");
         try {
             ob_start();
             $mvc->process();
             $mvc->serve();
             $body = ob_get_clean();
         } catch (StartNewRequestException $e) {
             $body = ob_get_clean();
             call_user_func($context['logger'], "--> [!] StartNewRequestException exception arrived");
         } catch (midgardmvc_exception $e) {
             ob_end_clean();
             try {
                 ob_start();
                 midgardmvc_core_exceptionhandler::handle($e);
                 $body = ob_get_clean();
             } catch (Exception $e) {
                 ob_end_clean();
                 call_user_func($context['logger'], "--> [!] " . get_class($e) . " exception arrived");
                 throw $e;
             }
         } catch (Exception $e) {
             ob_end_clean();
             call_user_func($context['logger'], "--> [!] " . get_class($e) . " exception arrived");
             throw $e;
         }
         call_user_func($context['logger'], "-> done with midgardmvc");
         return array($mvc->dispatcher->_get_status(), $mvc->dispatcher->_get_headers(), $body);
     } catch (Exception $e) {
         echo $e;
         return array(500, array('Content-type', 'text/plain'), "Internal Server Error \n(check log)");
     }
 }
 public function handle_exception(Exception $exception)
 {
     $app = midgardmvc_core::get_instance();
     $request = $app->context->get_request();
     if (isset($_POST['username']) && isset($_POST['password'])) {
         $tokens = array('login' => $_POST['username'], 'password' => $_POST['password']);
         if ($this->login($tokens)) {
             // Dispatch again since now we have a user
             $app->dispatcher->dispatch($request);
             return;
         }
     }
     $log_message = str_replace("\n", ' ', $exception->getMessage());
     $app->log(__CLASS__, $log_message, 'info');
     // Pass some data to the handler
     $data = array();
     $data['message'] = $exception->getMessage();
     $data['exception'] = $exception;
     $route = $request->get_route();
     $route->template_aliases['root'] = 'midgardmvc-login-form';
     $request->set_route($route);
     $request->set_data_item('midgardmvc_core_exceptionhandler', $data);
     $request->set_data_item('midgardmvc_core_services_authentication_message', $data['message']);
     $request->set_data_item('cache_enabled', false);
     $http_code = midgardmvc_core_exceptionhandler::code_by_exception($exception);
     midgardmvc_core::get_instance()->dispatcher->header(midgardmvc_core_exceptionhandler::header_by_code($http_code));
     // Do normal templating
     $app->templating->template($request);
     $app->templating->display($request);
     // Clean up and finish
     $app->context->delete();
     midgardmvc_core::get_instance()->dispatcher->end_request();
 }