Skip to content

Instantly share code, notes, and snippets.

@HarryR
Created July 26, 2012 20:33
Show Gist options
  • Select an option

  • Save HarryR/3184330 to your computer and use it in GitHub Desktop.

Select an option

Save HarryR/3184330 to your computer and use it in GitHub Desktop.
Doo - 80-line micro 'frameowork'
<?php
if( FALSE == isset($_SERVER['REDIRECT_URL']) ) {
if( FALSE == isset($_SERVER['PATH_INFO']) ) {
$_SERVER['REDIRECT_URL'] = $_SERVER['SCRIPT_NAME'];
}
else {
$_SERVER['REDIRECT_URL'] = $_SERVER['PATH_INFO'];
}
}
function Object($object) {
$object = strtolower($object);
static $result = array();
if ( isset($result[$object]) ) return $result[$object];
$file = sprintf('%s/%s.php', __DIR__, str_replace('_', '/', strtolower($object)));
if ( FALSE == file_exists($file) ) {
throw new Exception(sprintf("Cannot find file for object '%s'", $object));
}
require($file);
if ( FALSE == class_exists($object, false) ) {
throw new Exception(sprintf("Cannot find class '%s'", $object));
}
return $result[$object] = new $object();
}
function Redirect($url, $permanent = false) {
header('Location: ' . $url, true, $permanent ? 301 : 302);
exit;
}
function Route($route, $class, $method = NULL, $on = 'GET') {
$method = empty($method) ? 'index' : $method;
$on = strtoupper(empty($on) ? $_SERVER['REQUEST_METHOD'] : $on);
if ( $on === $_SERVER['REQUEST_METHOD'] ) {
$matches = array();
$url = trim($_SERVER['REDIRECT_URL'], '/');
if (preg_match('(^' . $route . '$)i', $url, $matches) > 0) {
// Filter out all integer keys
$matches = array_intersect_uassoc($matches, $matches, function($a, $b){
return is_int($a);
});
call_user_func_array(array(Object($class), $method), $matches);
exit;
}
}
}
function RestRoute($route, $class, $method = NULL, $on = 'GET') {
$method = empty($method) ? 'index' : $method;
$on = strtoupper(empty($on) ? $_SERVER['REQUEST_METHOD'] : $on);
if ( $on === $_SERVER['REQUEST_METHOD'] ) {
$matches = array();
$url = trim($_SERVER['REDIRECT_URL'], '/');
if (preg_match('(^' . $route . '$)i', $url, $matches) > 0) {
try {
$data = array(
'OK' => TRUE,
'ERROR' => NULL,
'DATA' => call_user_func_array(array(Object($class), $method), array_slice($matches, 1))
);
if( $data['DATA'] instanceof Traversable ) {
$data['DATA'] = iterator_to_array($data['DATA']);
}
}
catch( Exception $ex ) {
$data = array(
'OK' => FALSE,
'ERROR' => array(
'CODE' => $ex->getCode(),
'MSG' => $ex->getMessage(),
),
'DATA' => NULL,
);
}
header('Content-Type: text/javascript; charset=utf-8');
echo json_encode($data);
exit;
}
}
}
function View($__view, $arguments = array()) {
$__file = sprintf('%s/%s.phtml', __DIR__, strtolower(str_replace('_', '/', $__view)));
if( FALSE == file_exists($__file) ) {
throw new Exception(sprintf("Template '%s' not found!", $__view));
}
extract($arguments);
require($__file);
}
function Render($__view, $arguments = array()) {
ob_start();
View($__view, $arguments);
return ob_get_clean();
}
function Param($name, $default = NULL, $array = NULL) {
if( $array === NULL ) $array = $_REQUEST;
if( isset($array[$name]) ) return $array[$name];
return $default;
}
function Url($path = NULL, $params = array()) {
if( NULL === $path ) $path = $_SERVER['REDIRECT_URL'];
if( NULL === $params ) $params = $_GET;
return sprintf("%s?%s", $path, http_build_query($params));
}
function Config($name, $default = NULL) {
if( isset($_ENV[$name]) ) return $_ENV[$name];
if( function_exists('apache_getenv') ) {
$value = apache_getenv($name);
if( FALSE === $value ) return $default;
return $value;
}
return $default;
}
function get_public_vars($obj) {
return (array)$obj;
}
@1stvamp

1stvamp commented Oct 24, 2012

Copy link
Copy Markdown

Looks more lik 126ish lines to me 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment