Files
sihnon-php-lib/source/lib/Sihnon/Main.class.php
Ben Roberts ddd2e62c13 Imports code from RippingCluster project
Imports all generic classes from the RippingCluster project, with any ripping-specific code stripped out.
Tested against a dummy project for errors.
2010-10-10 11:36:58 +01:00

173 lines
4.5 KiB
PHP

<?php
class Sihnon_Main {
private static $instance;
private $config;
private $database;
private $log;
private $cache;
private $base_uri;
private function __construct() {
$this->config = new Sihnon_Config(Sihnon_DBConfig);
$this->database = new Sihnon_Database($this->config);
$this->config->setDatabase($this->database);
$this->log = new Sihnon_Log($this->database, $this->config, Sihnon_Log_Table);
$this->cache = new Sihnon_Cache($this->config);
$this->base_uri = dirname($_SERVER['SCRIPT_NAME']) . '/';
}
/**
*
* @return Sihnon_Main
*/
public static function instance() {
if (!self::$instance) {
self::$instance = new Sihnon_Main();
}
return self::$instance;
}
/**
*
* @return Sihnon_Config
*/
public function config() {
return $this->config;
}
/**
*
* @return Sihnon_Database
*/
public function database() {
return $this->database;
}
/**
*
* @return Sihnon_Log
*/
public function log() {
return $this->log;
}
/**
*
* @return Sihnon_Cache
*/
public function cache() {
return $this->cache;
}
public function baseUri() {
return $this->base_uri;
}
public function absoluteUrl($relative_url) {
$secure = isset($_SERVER['secure']);
$port = $_SERVER['SERVER_PORT'];
return 'http' . ($secure ? 's' : '') . '://'
. $_SERVER['HTTP_HOST'] . (($port == 80 || ($secure && $port == 443)) ? '' : ':' . $port)
. '/' . $this->base_uri . $relative_url;
}
public static function initialise() {
spl_autoload_register(array('Sihnon_Main','autoload'));
}
public static function autoload($classname) {
// Ensure the classname contains only valid class name characters
if (!preg_match('/^[A-Z][a-zA-Z0-9_]*$/', $classname)) {
throw new Exception('Illegal characters in classname'); // TODO Subclass this exception
}
// Ensure the class to load begins with our prefix
if (!preg_match('/^Sihnon_/', $classname)) {
return;
}
// Special case: All exceptions are stored in the same file
if (preg_match('/^Sihnon_Exception/', $classname)) {
require_once(Sihnon_Lib . 'Sihnon/Exceptions.class.php');
return;
}
// Replace any underscores with directory separators
$filename = Sihnon_Lib . preg_replace('/_/', '/', $classname);
// Tack on the class file suffix
$filename .= '.class.php';
// If this file exists, load it
if (file_exists($filename)) {
require_once $filename;
}
}
public static function mkdir_recursive($directory, $permissions=0777) {
$parts = explode('/', $directory);
$path = '';
for ($i=1,$l=count($parts); $i<=$l; $i++) {
$iPath = $parts;
$path = join('/', array_slice($iPath, 0, $i));
if (empty($path)) continue;
if (!file_exists($path)) {
if (!mkdir($path)) return false;
if (!chmod($path, $permissions)) return false;
}
}
return true;
}
public static function issetelse($var, $default = null) {
if (isset($var)) {
return $var;
}
if (is_string($default) && preg_match('/^Sihnon_Exception/', $default) && class_exists($default) && is_subclass_of($default, Sihnon_Exception)) {
throw new $default();
}
return $default;
}
public static function formatDuration($time) {
if (is_null($time)) {
return 'unknown';
}
$labels = array('seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years');
$limits = array(1, 60, 3600, 86400, 604800, 2592000, 31556926, PHP_INT_MAX);
$working_time = $time;
$result = "";
$ptr = count($labels) - 1;
while ($ptr >= 0 && $working_time < $limits[$ptr]) {
--$ptr;
}
while ($ptr >= 0) {
$unit_time = floor($working_time / $limits[$ptr]);
$working_time -= $unit_time * $limits[$ptr];
$result = $result . ' ' . $unit_time . ' ' . $labels[$ptr];
--$ptr;
}
return $result;
}
}
Sihnon_Main::initialise();
?>