Skip to content

Instantly share code, notes, and snippets.

@eb1024
Created August 20, 2009 13:51
Show Gist options
  • Select an option

  • Save eb1024/171082 to your computer and use it in GitHub Desktop.

Select an option

Save eb1024/171082 to your computer and use it in GitHub Desktop.
<?php
function Doo_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 = sha1($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;
}
?>
<?php
class Doo
{
public static function __callStatic($method, $arguments)
{
if (is_file('./Doo/' . $method . '.php') === true)
{
if (function_exists('Doo_' . $method) === false)
{
require_once('./Doo/' . $method . '.php');
}
return call_user_func_array('Doo_' . $method, $arguments);
}
return false;
}
}
?>
<?php
function Doo_Pattern($string, $pattern, $key = null, $modifiers = null)
{
$matches = array();
if (preg_match('~' . $pattern . '~' . $modifiers, $string, $matches) > 0)
{
return (is_null($key) === true) ? true : Doo::Value($matches, $key);
}
return false;
}
?>
<?php
function Doo_Redirect($url, $permanent = false)
{
if (headers_sent() === false)
{
header('Location: ' . $url, true, ($permanent === true) ? 301 : 302);
}
exit();
}
?>
<?php
function Doo_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(Doo::Singleton($class), $method), array_slice($matches, 1)));
}
}
return false;
}
?>
<?php
function Doo_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;
}
?>
<?php
function Doo_Slug($string)
{
if (Doo::Pattern($string, '[^\x00-\x7F]') === true)
{
$string = preg_replace('~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_COMPAT, 'UTF-8'));
}
return strtolower(trim(preg_replace(array('~[^0-9a-z]~i', '~-+~'), '-', $string), '-'));
}
?>
<?php
function Doo_Value($array, $key, $default = false)
{
if (is_array($array) === true)
{
settype($key, 'array');
foreach ($key as $value)
{
if (array_key_exists($value, $array) === false)
{
return $default;
}
$array = $array[$value];
}
return $array;
}
return $default;
}
?>
<?php
function Doo_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