Added support for redirects in Page class

This commit is contained in:
2010-04-03 22:57:15 +01:00
parent df5721181f
commit b0c6302538
3 changed files with 58 additions and 4 deletions

View File

@@ -13,6 +13,7 @@ class HandBrakeCluster_Exception_ConfigException extends HandBrakeCluster
class HandBrakeCluster_Exception_UnknownSetting extends HandBrakeCluster_Exception_ConfigException {}; class HandBrakeCluster_Exception_UnknownSetting extends HandBrakeCluster_Exception_ConfigException {};
class HandBrakeCluster_Exception_TemplateException extends HandBrakeCluster_Exception {}; class HandBrakeCluster_Exception_TemplateException extends HandBrakeCluster_Exception {};
class HandBrakeCluster_Exception_AbortEntirePage extends HandBrakeCluster_Exception_TemplateException {};
class HandBrakeCluster_Exception_Unauthorized extends HandBrakeCluster_Exception_TemplateException {}; class HandBrakeCluster_Exception_Unauthorized extends HandBrakeCluster_Exception_TemplateException {};
class HandBrakeCluster_Exception_FileNotFound extends HandBrakeCluster_Exception_TemplateException {}; class HandBrakeCluster_Exception_FileNotFound extends HandBrakeCluster_Exception_TemplateException {};
class HandBrakeCluster_Exception_InvalidParameters extends HandBrakeCluster_Exception_TemplateException {}; class HandBrakeCluster_Exception_InvalidParameters extends HandBrakeCluster_Exception_TemplateException {};

View File

@@ -12,6 +12,8 @@ class HandBrakeCluster_Main {
private $log; private $log;
private $request; private $request;
private $cache; private $cache;
private $base_uri;
private function __construct() { private function __construct() {
$request_string = isset($_GET['l']) ? $_GET['l'] : ''; $request_string = isset($_GET['l']) ? $_GET['l'] : '';
@@ -31,10 +33,16 @@ class HandBrakeCluster_Main {
$this->smarty->config_fir = './config'; $this->smarty->config_fir = './config';
$this->smarty->assign('version', '0.1'); $this->smarty->assign('version', '0.1');
$this->smarty->assign('base_uri', dirname($_SERVER['SCRIPT_NAME']) . '/');
$this->base_uri = dirname($_SERVER['SCRIPT_NAME']) . '/';
$this->smarty->assign('base_uri', $this->base_uri);
} }
/**
*
* @return HandBrakeCluster_Main
*/
public static function instance() { public static function instance() {
if (!self::$instance) { if (!self::$instance) {
self::$instance = new HandBrakeCluster_Main(); self::$instance = new HandBrakeCluster_Main();
@@ -47,25 +55,57 @@ class HandBrakeCluster_Main {
return $this->smarty; return $this->smarty;
} }
/**
*
* @return HandBrakeCluster_Config
*/
public function config() { public function config() {
return $this->config; return $this->config;
} }
/**
*
* @return HandBrakeCluster_Database
*/
public function database() { public function database() {
return $this->database; return $this->database;
} }
/**
*
* @return HandBrakeCluster_Log
*/
public function log() { public function log() {
return $this->log; return $this->log;
} }
/**
*
* @return HandBrakeCluster_RequestParser
*/
public function request() { public function request() {
return $this->request; return $this->request;
} }
/**
*
* @return HandBrakeCluster_Cache
*/
public function cache() { public function cache() {
return $this->cache; return $this->cache;
} }
public function baseUri() {
return $this->base_uri;
}
public function absoluteUrl($relative_url) {
$secure = isset($_SERVER['secure']);
$port = $_SERVER['HTTP_PORT'];
return 'http' . ($secure ? 's' : '') . '://'
. $_SERVER['HTTP_HOST'] . (($port == 80 || ($secure && $port == 443)) ? '' : ':' . $port)
. '/' . $this->base_uri . $relative_url;
}
public static function initialise() { public static function initialise() {
spl_autoload_register(array('HandBrakeCluster_Main','autoload')); spl_autoload_register(array('HandBrakeCluster_Main','autoload'));
@@ -120,12 +160,13 @@ class HandBrakeCluster_Main {
return $var; return $var;
} }
if (is_subclass_of($default, HandBrakeCluster_Exception)) { if (preg_match('/^HandBrakeCluster_Exception/', $default) && class_exists($default) && is_subclass_of($default, HandBrakeCluster_Exception)) {
throw new $e(); throw new $default();
} }
return $default; return $default;
} }
} }
HandBrakeCluster_Main::initialise(); HandBrakeCluster_Main::initialise();

View File

@@ -27,13 +27,17 @@ class HandBrakeCluster_Page {
try { try {
$this->render($template_filename, $code_filename, $template_variables); $this->render($template_filename, $code_filename, $template_variables);
} catch (HandBrakeCluster_Exception_AbortEntirePage $e) {
return false;
} catch (HandBrakeCluster_Exception_FileNotFound $e) { } catch (HandBrakeCluster_Exception_FileNotFound $e) {
$this->render('errors/404.tpl', 'errors/404.php'); $this->render('errors/404.tpl', 'errors/404.php');
} catch (HandBrakeCluster_Exception_TemplateException $e) { } catch (HandBrakeCluster_Exception $e) {
$this->render('errors/unhandled-exception.tpl', 'errors/unhandled-exception.php', array( $this->render('errors/unhandled-exception.tpl', 'errors/unhandled-exception.php', array(
'exception' => $e, 'exception' => $e,
)); ));
} }
return true;
} }
protected function render($template_filename, $code_filename = null, $template_variables = array()) { protected function render($template_filename, $code_filename = null, $template_variables = array()) {
@@ -56,6 +60,14 @@ class HandBrakeCluster_Page {
// Now execute the template itself, which will render the results of the code file // Now execute the template itself, which will render the results of the code file
$this->smarty->assign('page_content', $this->smarty->fetch($template_filename)); $this->smarty->assign('page_content', $this->smarty->fetch($template_filename));
} }
public static function redirect($relative_url) {
$absolute_url = HandBrakeCluster_Main::instance()->absoluteUrl($relative_url);
header("Location: $absolute_url");
throw new HandBrakeCluster_Exception_AbortEntirePage();
}
}; };