Created
September 14, 2009 16:33
-
-
Save eb1024/186761 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function DB($query) | |
{ | |
static $db = null; | |
static $result = array(); | |
if (is_file($query) === true) | |
{ | |
$db = new PDO('sqlite:' . $query, null, null, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING)); | |
} | |
else if (is_a($db, 'PDO') === true) | |
{ | |
$hash = md5($query); | |
if (empty($result[$hash]) === true) | |
{ | |
$result[$hash] = $db->prepare($query); | |
} | |
if (is_a($result[$hash], 'PDOStatement') === true) | |
{ | |
if ($result[$hash]->execute(array_slice(func_get_args(), 1)) === true) | |
{ | |
if (stripos($query, 'INSERT') === 0) | |
{ | |
return $db->lastInsertId(); | |
} | |
if (stripos($query, 'SELECT') === 0) | |
{ | |
return $result[$hash]->fetchAll(PDO::FETCH_ASSOC); | |
} | |
return $result[$hash]->rowCount(); | |
} | |
} | |
return false; | |
} | |
return true; | |
} | |
function Route($route, $class, $method, $on = null) | |
{ | |
$on = (empty($on) === true) ? $_SERVER['REQUEST_METHOD'] : $on; | |
$route = (empty($route) === true) ? $_SERVER['SCRIPT_NAME'] : $route; | |
if (strcasecmp($on, $_SERVER['REQUEST_METHOD']) === 0) | |
{ | |
$matches = array(); | |
if (preg_match('~' . rtrim($route, '/') . '$~i', rtrim($_SERVER['PHP_SELF'], '/'), $matches) > 0) | |
{ | |
exit(call_user_func_array(array(Singleton($class), $method), array_slice($matches, 1))); | |
} | |
} | |
return false; | |
} | |
function Singleton($object) | |
{ | |
static $result = array(); | |
if (class_exists($object, false) === true) | |
{ | |
if (array_key_exists($object, $result) === false) | |
{ | |
$result[$object] = new $object(); | |
} | |
return $result[$object]; | |
} | |
else if (is_file($object . '.php') === true) | |
{ | |
$class = basename($object); | |
if (array_key_exists($class, $result) === false) | |
{ | |
if (class_exists($class, false) === false) | |
{ | |
require($object . '.php'); | |
} | |
$result[$class] = new $class(); | |
} | |
return $result[$class]; | |
} | |
return false; | |
} | |
function View($view) | |
{ | |
if (is_file($view . '.php') === true) | |
{ | |
$arguments = array_slice(func_get_args(), 1); | |
foreach ($arguments as $argument) | |
{ | |
if (is_array($argument) === true) | |
{ | |
extract($argument, EXTR_OVERWRITE); | |
} | |
} | |
require($view . '.php'); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment