Skip to content
Browse files

- refactoring with a new and simple configuration system

  • Loading branch information...
1 parent 4d35953 commit 9cf138865a208fb254d2c3db88687482b9e53bf3 Harald Kirschner committed May 9, 2009
Showing with 62 additions and 31 deletions.
  1. +62 −31 Tyke.php
View
93 Tyke.php
@@ -1,27 +1,9 @@
<?php
-if (!defined('TYKE_DEBUG')) define('TYKE_DEBUG', true);
-
-if (TYKE_DEBUG) {
- error_reporting(E_ALL);
- ini_set('display_errors', '1');
-
- // Custom error handler, to throw nice-looking error exceptions
- function errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
- {
- $report = error_reporting();
- if ($report && $report & $errno) {
- throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
- }
- }
- set_error_handler('errorHandler');
-}
-
-
/*
- * Controller
+ * TykeC - The Controller
*/
-class Controller
+class TykeC
{
protected $layout = true;
@@ -38,11 +20,11 @@ class Controller
public function render($file)
{
if (!$this->layout) {
- print $this->renderTemplate($file);
+ print $this->renderPartial($file);
} else {
- $this->content = $this->renderTemplate($file);
+ $this->content = $this->renderPartial($file);
- print $this->renderTemplate($this->layoutTemplate);
+ print $this->renderPartial($this->layoutTemplate);
}
}
@@ -52,7 +34,7 @@ public function render($file)
* @param string Path
* @return string Output
*/
- private function renderTemplate($tyke_file)
+ private function renderPartial($tyke_file)
{
if (!is_readable($tyke_file)) {
throw new Exception('View "'.$tyke_file.'" Not Found');
@@ -85,13 +67,15 @@ public function header($text)
/**
* Redirect to a new page
*
+ * @param string url
* @param boolean Indicates that dispatcher will not wait all process
* @return Controller Instance
*/
public function redirect($url, $now = false)
{
- if (!$now) $this->header('Location: ' . $url);
- else header('Location: ' . $url);
+ if (!$now) return $this->header('Location: ' . $url);
+ header('Location: ' . $url);
+ exit;
}
}
@@ -104,6 +88,27 @@ class Tyke
static protected $routes = array();
+ static public $config = array();
+
+ public static function set($name, $value = null)
+ {
+ if (is_array($name)) {
+ foreach ($name as $key => $value) {
+ self::setConfig($key, $value);
+ }
+ } else {
+ self::$config[$name] = $value;
+ }
+ }
+
+ public static function get($name)
+ {
+ if (!isset(self::$config[$name])) return null;
+
+ return self::$config[$name];
+ }
+
+
/**
* Add url to routes
*
@@ -114,7 +119,7 @@ class Tyke
*
* @return Tyke
*/
- public static function addRoute($pattern, $class, $method = 'index', $options = array())
+ public static function register($pattern, $class, $method = 'index', $options = array())
{
$defaults = array(
'http_method' => null
@@ -130,13 +135,21 @@ public static function addRoute($pattern, $class, $method = 'index', $options =
public static function bootstrap()
{
+ if (self::get('tyke.debug')) {
+ 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 (TYKE_DEBUG) {
+
+ if (self::get('tyke.debug')) {
// fix stack trace in case it doesn't contain the exception origin as the first entry
$fixedTrace = $e->getTrace();
@@ -183,6 +196,14 @@ public static function bootstrap()
}
}
+ 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 requests and dispatch
*/
@@ -212,8 +233,13 @@ public static function dispatch($url = null, array $params = array())
$options = $route['options'];
- if (!empty($options['http_method']) && strtolower($_SERVER['REQUEST_METHOD']) == strtolower($options['http_method'])) {
- continue;
+ 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;
+ }
}
array_shift($matches);
@@ -225,7 +251,12 @@ public static function dispatch($url = null, array $params = array())
self::execute($route['class'], $route['method'], $params);
}
- call_user_func('r404', $_SERVER['REQUEST_METHOD']);
+ if (function_exists('r404')) {
+ call_user_func('r404', $_SERVER['REQUEST_METHOD']);
+ } else {
+ header('HTTP/1.1 404 Not Found');
+ die('Error: 404 Not Found');
+ }
}
protected function execute($class, $method, array $params = array())

0 comments on commit 9cf1388

Please sign in to comment.
Something went wrong with that request. Please try again.