Imported Page/RequestParser classes for page templating.

Added formatFilesize method to Main class
This commit is contained in:
2011-01-16 22:46:34 +00:00
parent 67397fd776
commit 998a9d958f
4 changed files with 220 additions and 2 deletions

View File

@@ -21,4 +21,6 @@ class SihnonFramework_Exception_CacheObjectNotFound extends SihnonFramework_E
class SihnonFramework_Exception_InvalidPluginName extends SihnonFramework_Exception {};
class SihnonFramework_Exception_FileNotFound extends SihnonFramework_Exception {};
?>

View File

@@ -185,12 +185,12 @@ class SihnonFramework_Main {
$subclass_dir_prefix .= DIRECTORY_SEPARATOR;
}
self::$autoload_classes[] = array(
array_unshift(self::$autoload_classes, array(
'base' => $base,
'base_dir_prefix' => $base_dir_prefix,
'subclass' => $subclass,
'subclass_dir_prefix' => $subclass_dir_prefix,
);
));
}
/**
@@ -264,6 +264,25 @@ class SihnonFramework_Main {
return $result;
}
public static function formatFilesize($bytes) {
if (is_null($bytes)) {
return 'unknown';
}
$labels = array('B', 'KB', 'MB', 'GB', 'TB');
$limits = array(1, 1024, 1024*1024, 1024*1024*1024, 1024*1024*1024*1024);
$size = $bytes;
$ptr = count($labels) - 1;
while ($ptr >= 0 && $bytes < $limits[$ptr]) {
--$ptr;
}
$size = round($bytes / $limits[$ptr], 2) . ' ' . $labels[$ptr];
return $size;
}
}
SihnonFramework_Main::initialise();

View File

@@ -0,0 +1,74 @@
<?php
class SihnonFramework_Page {
private $smarty;
private $request;
private $page;
public function __construct(Smarty $smarty, SihnonFramework_RequestParser $request) {
$this->smarty = $smarty;
$this->request = $request;
$this->page = $request->page();
}
public function page() {
return $this->page;
}
public function template_filename() {
return $this->page . '.tpl';
}
public function evaluate($template_variables = array()) {
$code_filename = $this->page . '.php';
$template_filename = $this->template_filename();
try {
$this->render($template_filename, $code_filename, $template_variables);
} catch (MediaListing_Exception_AbortEntirePage $e) {
return false;
} catch (MediaListing_Exception_FileNotFound $e) {
$this->render('errors/404.tpl', 'errors/404.php');
} catch (MediaListing_Exception $e) {
$this->render('errors/unhandled-exception.tpl', 'errors/unhandled-exception.php', array(
'exception' => $e,
));
}
return true;
}
protected function render($template_filename, $code_filename = null, $template_variables = array()) {
if ( ! $this->smarty->template_exists($template_filename)) {
throw new MediaListing_Exception_FileNotFound($template_filename);
}
// Copy all the template variables into the namespace for this function,
// so that they are readily available to the template
foreach ($template_variables as $__k => $__v) {
$$__k = $__v;
}
// Include the template code file, which will do all the work for this page
$real_code_filename = '../source/pages/' . $code_filename;
if ($code_filename && file_exists($real_code_filename)) {
include $real_code_filename;
}
// Now execute the template itself, which will render the results of the code file
$this->smarty->assign('page_content', $this->smarty->fetch($template_filename));
}
public static function redirect($relative_url) {
$absolute_url = MediaListing_Main::instance()->absoluteUrl($relative_url);
header("Location: $absolute_url");
throw new MediaListing_Exception_AbortEntirePage();
}
};
?>

View File

@@ -0,0 +1,123 @@
<?php
class SihnonFramework_RequestParser {
private $request_string;
private $page = array();
private $vars = array();
public function __construct($request_string) {
$this->request_string = $request_string;
$this->parse();
}
public function parse() {
if (!$this->request_string) {
$this->page = array('home');
return;
}
$components = explode('/', $this->request_string);
if (!$components) {
return;
}
// Read through the components list looking for elements matching known directories and files
// to determine which page this request is for
$base_dir = '../source/templates';
while (true) {
if ($components && ! $components[0]) {
// Skip over any empty components before we find a page
array_shift($components);
}
if ($components && is_dir($base_dir . '/' . $components[0])) {
$base_dir .= '/' . $components[0];
array_push($this->page, array_shift($components));
} elseif ($components && is_file($base_dir . '/' . $components[0] . '.tpl')) {
// We have found a valid page, so break the loop here,
// leaving the remaining components as key/value pairs
array_push($this->page, array_shift($components));
break;
} else {
// See if we've already seen a component and assumed it referred to a dir when a file of the same name exists
if ($this->page && is_file($base_dir . '.tpl')) {
break;
} elseif ( ! $components && is_file($base_dir . '/index.tpl')) {
// The last component in the path was a valid directory, and a directory index exists
array_push($this->page, 'index');
break;
} else {
// No valid page was found, so display an error page
$this->page = array('404');
return;
}
}
}
// The subsequent tokens are parameters for this page in key value pairs
while ($components) {
// If the url ended with a trailing slash, the last element will be null
$last_element = $components[count($components) - 1];
if ($last_element == "") {
array_pop($components);
}
$this->vars[array_shift($components)] = $components ? array_shift($components) : null;
}
}
public function page() {
return join('/', $this->page);
}
public function exists($key) {
return isset($this->vars[$key]);
}
public function get($key, $default = null) {
if (isset($this->vars[$key])) {
return $this->vars[$key];
}
if (is_string($default) && preg_match('/^[a-zA-Z_]+_Exception/', $default) && class_exists($default) && is_subclass_of($default, MediaListing_Exception)) {
throw new $default();
}
return $default;
}
public function getRemainder($after, $hasValue) {
$include = false;
$result = array();
foreach ($this->vars as $var => $value) {
if ($var == $after) {
$include = true;
if ($hasValue) {
continue;
} else {
$result[] = $value;
}
} elseif ($include) {
$result[] = $var;
$result[] = $value;
} else {
continue;
}
}
if ($result && ( ! $result[count($result) - 1])) {
array_pop($result);
}
return $result;
}
public function request_string() {
return $this->request_string;
}
};
?>