Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: b1eb108388
Fetching contributors…

Cannot retrieve contributors at this time

file 391 lines (317 sloc) 9.157 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
<?php
/**
* Tyke
*
* Is a nano web framework for PHP, like web.py for Python, Sinatra or Camping for Ruby.
*
* It was forked by Harald Kirschner of Nice Dog, originally by Tiago Bastos.
*
* @author Tiago Bastos
* @author Harald Kirschner <harald@digitarald.com>
* @copyright 2009 Authors
* @license MIT License
*/

/*
* Tyke - Application core
*
* Just configuration and magic routing
*/
class Tyke
{

static public $routes = array();

static public $config = array();

/**
* Sets one or many internal value.
*
* @param string|array Name or list of name/value pairs
* @param mixed|null Value
*/
public static function set($name, $value = null)
{
if (is_array($name)) {
foreach ($name as $key => &$value) {
self::set($key, $value);
}
} else {
self::$config[$name] = $value;
}
}

/**
* Gets one value by name or the default value if name does not not exist.
*
* @param string Name
* @param mixed|null Default value
*
* @return mixed
*/
public static function get($name, $default = null)
{
if (!isset(self::$config[$name])) return $default;

return self::$config[$name];
}


/**
* Register URI pattern as route
*
* @param string|array Regexp pattern or array of pattern/function pairs
* @param array|string Callback array (*not* static! Will be called on the instance) or string for a method
* @param array|null Options
*
* @return Tyke
*/
public static function register($pattern, $callback = null, $options = array())
{
if (is_array($pattern)) {
foreach ($pattern as $key => $value) {
if (is_string($key)) $value = array($key, $value);
call_user_func_array(array('Tyke', 'register'), $value);
}
} else {
$defaults = array(
'http_method' => null
);

$pattern = str_replace('/', '\/', preg_replace('/\\(([a-z][-\w]*):/i', '(?P<$1>', $pattern));

self::$routes[] = array(
'pattern' => '/^' . $pattern . '$/U',
'callback' => $callback,
'options' => array_merge($defaults, $options)
);
}
}

/**
* Starts Tyke with the registered routes.
*
* If "core.debug" is enabled, exceptions are displayed, otherwise a 500-Error is shown.
*/
public static function run()
{
if (self::get('core.debug', true)) {
error_reporting(E_ALL);
ini_set('display_errors', '1');
set_error_handler(array('Tyke', 'rethrow'));
}

try {
self::dispatch();
} catch (Exception $e) {

if (!headers_sent()) header('HTTP/1.1 500 Internal Server Error');

if (self::get('core.debug', true)) {

$fixedTrace = $e->getTrace();

if (isset($fixedTrace[0]['file']) && !($fixedTrace[0]['file'] == $e->getFile() && $fixedTrace[0]['line'] == $e->getLine())) {
array_unshift($fixedTrace, array('file' => $e->getFile(), 'line' => $e->getLine()));
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Error!</title>
</head>
<body style="font-family: monospace;">
<h1>Exception: <?php echo get_class($e); ?></h1>
<h2>Message</h2>
<p><?= html_entity_decode($e->getMessage()); ?></p>
<h2>Stack Trace</h2>
<ol>
<?php
foreach ($fixedTrace as $trace) {
echo '<li>';

if (isset($trace['file'])) echo $trace['file'];
else echo "Unknown file";

if (isset($trace['line'])) echo " (line: " .$trace['line'] .')';
else echo "(Unknown line)";

echo '</li>';
}
?>
</ol>
</body>
</html><?php

} else {
echo 'Internal Server Error';
}

exit;
}
}

/**
* Internal method to re-throw lame PHP errors as nice exceptions
*/
public static function rethrow($errno, $errstr, $errfile, $errline, $errcontext)
{
$report = error_reporting();
if ($report && $report & $errno) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
}

/**
* Process the given URL or the path from the .htaccess redirect.
*
* @param string|null URI
*/
public static function dispatch($uri = null)
{
if ($uri === null) {
$parts = array_merge(array('path' => '', 'query' => ''), parse_url('ty://ke' . $_SERVER['REQUEST_URI']));

$prepend = dirname($_SERVER['SCRIPT_NAME']);

$from = strlen($prepend);
$uri = $parts['path'];

if ($from < 2) {
$prepend = '/';
$page = $uri;
} else {
$uri = substr($uri, $from);
$page = $prepend . $uri;
}

$self = $page;

$_GET = array();

if ($parts['query']) {
parse_str($parts['query'], $query);
if (is_array($query)) {
$_GET = $query;
$self .= '?' . http_build_query($query, null, '&');
}
}

Tyke::set('core.run.basepath', $prepend);
Tyke::set('core.run.uri', $page);
Tyke::set('core.run.self', $self);
}

foreach(self::$routes as $route) {

if (!preg_match($route['pattern'], $uri, $matches)) {
continue;
}

$options = $route['options'];

// optional http_method, can be string or array
if (!empty($options['http_method'])) {
$check = $_SERVER['REQUEST_METHOD'];

if (is_array($options['http_method'])) {
if (!in_array($check, $options['http_method'])) continue;
} else {
if ($options['http_method'] != $check) continue;
}
}

Tyke::set('core.run.route', $route);

// get params from named matches
array_shift($matches);

$params = array();

foreach($matches as $key => $match){
if (is_string($key)) $params[$key] = $match;
}

Tyke::set('core.run.params', $params);

// populate data back to global data holders
$_GET = array_merge($_GET, $params);
$_REQUEST = array_merge($_POST, $_GET, $_COOKIE);

self::execute($route['callback'], $params);
}

$r404 = Tyke::get('core.r404', 'r404');

if ($r404 && function_exists($r404)) {
call_user_func($r404, $_SERVER['REQUEST_METHOD']);
} else {
if (!headers_sent()) header('HTTP/1.1 404 Not Found');
die('Error: 404 Not Found');
}
}

/**
* Execute a function with the given arguments, echoes the result and exits the application.
*
* @param array|string Callback, array (class[, method == 'index']), string 'class::method' or 'function'
* @param array Parameters for the callback
*/
public function execute($callback, array $params = array())
{
if (is_string($callback) && strpos($callback, '::') !== false) $callback = explode('::', $callback, 2);

if (is_array($callback)) {
if (is_string($callback[0])) {
if (!class_exists($callback[0], true)) throw new Exception('Controller "'.$class.'" Not Found');

$callback[0] = new $callback[0]();
}

if (empty($callback[1])) $callback[1] = 'index';

if (!method_exists($callback[0], $callback[1]) && !method_exists($callback[0], '__call')) {
throw new Exception('Method "'.$callback[1].'" Not Found');
}
}

ob_start();

call_user_func_array($callback, $params);

$out = ob_get_contents();
ob_end_clean();

if (is_array($callback) && !empty($callback[0]->headers)) {
foreach($callback[0]->headers as $header) header($header);
}

print $out;
exit;
}

}


/*
* TykeC - The Controller
*/
class TykeC
{
protected $layout = true;

protected $layoutTemplate = 'views/layout.php';

public $headers = array();

/**
* Render function return php rendered in a variable
*
* @param string File
* @return Controller Instance
*/
public function render($file, $vars = null)
{
if (!$this->layout) {
print $this->renderPartial($file, $vars);
} else {
$this->content = $this->renderPartial($file, $vars);

print $this->renderPartial($this->layoutTemplate, $vars);
}
return $this;
}

/**
* Open template to render and return php rendered in a variable using ob_start/ob_end_clean
*
* @param string Path
* @return string Output
*/
protected function renderPartial($tyke_file, $tyke_vars = null)
{
if (!is_readable($tyke_file)) throw new Exception('View "'.$tyke_file.'" Not Found');

if (is_array($tyke_vars)) extract($tyke_vars, EXTR_REFS | EXTR_PREFIX_INVALID, '_');

extract(get_object_vars($this), EXTR_REFS | EXTR_PREFIX_INVALID, '_');

ob_start();

require($tyke_file);

$out = ob_get_contents();
ob_end_clean();

return $out;
}

/**
* Add information in header
*
* @param string Value
* @return Controller Instance
*/
public function header($text)
{
$this->headers[] = $text;
return $this;
}

/**
* Redirect to a new page
*
* @param string uri
* @param boolean Indicates that dispatcher will not wait all process
* @return Controller Instance
*/
public function redirect($uri, $now = false)
{
if (!$now) return $this->header('Location: ' . $uri);

header('Location: ' . $uri);
exit;
}

/**
* Forward to a new action
*
* @param string uri
* @param boolean Indicates that dispatcher will not wait all process
* @return Controller Instance
*/
public function forward($callback, $params = array())
{
Tyke::execute($callback, $params);
}

}

?>
Something went wrong with that request. Please try again.