Created
September 14, 2009 16:27
-
-
Save eb1024/186757 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Dump() | |
| { | |
| foreach (func_get_args() as $argument) | |
| { | |
| echo '<pre style="text-align: left;">' . "\n"; | |
| if (is_array($argument) === true) | |
| { | |
| print_r($argument); | |
| } | |
| else | |
| { | |
| var_dump($argument); | |
| } | |
| echo '</pre>' . "\n"; | |
| } | |
| } | |
| function Redirect($url, $permanent = false) | |
| { | |
| if (headers_sent() === false) | |
| { | |
| header('Location: ' . $url, true, ($permanent === true) ? 301 : 302); | |
| } | |
| exit(); | |
| } | |
| 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) | |
| { | |
| for ($i = 1; $i < func_num_args(); $i++) | |
| { | |
| $argument = func_get_arg($i); | |
| 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