diff options
Diffstat (limited to 'system')
-rw-r--r-- | system/bootstrap.php | 70 | ||||
-rw-r--r-- | system/classes/config.php | 174 | ||||
-rw-r--r-- | system/classes/controller.php | 120 | ||||
-rw-r--r-- | system/classes/debug.php | 192 | ||||
-rw-r--r-- | system/classes/misc.php | 122 | ||||
-rw-r--r-- | system/classes/request.php | 194 | ||||
-rw-r--r-- | system/classes/response.php | 93 | ||||
-rw-r--r-- | system/classes/route.php | 278 | ||||
-rw-r--r-- | system/classes/session.php | 142 | ||||
-rw-r--r-- | system/classes/view.php | 147 | ||||
-rw-r--r-- | system/views/debug.php | 86 |
11 files changed, 851 insertions, 767 deletions
diff --git a/system/bootstrap.php b/system/bootstrap.php index 3e43dc5..f8ae016 100644 --- a/system/bootstrap.php +++ b/system/bootstrap.php @@ -4,56 +4,59 @@ * Bootstraps the system
* @package Core
*/
-class Bootstrap{
+class Bootstrap
+{
+
+ /**
+ * Autload function. Searches for the class file and includes it.
+ *
+ * @param unknown $class Class name
+ * @return void
+ * @access public
+ * @throws Exception If the class is not found
+ * @static
+ */
+ public static function autoload($class)
+ {
- /**
- * Autload function. Searches for the class file and includes it.
- *
- * @param unknown $class Class name
- * @return void
- * @access public
- * @throws Exception If the class is not found
- * @static
- */
- public static function autoload($class) {
-
$path = array_reverse(explode('_', strtolower($class)));
$file = array_pop($path);
- $path = 'classes/'.implode('/',$path);
+ $path = 'classes/'.implode('/', $path);
$file = Misc::find_file($path, $file);
-
- if($file)
+
+ if ($file)
include($file);
}
-
- /**
- * Runs the application
- *
- * @return void
- * @access public
- * @static
- */
- public static function run() {
+
+ /**
+ * Runs the application
+ *
+ * @return void
+ * @access public
+ * @static
+ */
+ public static function run()
+ {
/**
* Application folder
*/
- define('APPDIR', ROOTDIR.'application/');
+ define('APPDIR', ROOTDIR.'application/');
/**
* Modules folder
*/
- define('MODDIR', ROOTDIR.'modules/');
+ define('MODDIR', ROOTDIR.'modules/');
/**
* System folder
*/
- define('SYSDIR', ROOTDIR.'system/');
+ define('SYSDIR', ROOTDIR.'system/');
/**
* Web folder
*/
- define('WEBDIR', ROOTDIR.'web/');
+ define('WEBDIR', ROOTDIR.'web/');
/**
* Helper functions
*/
@@ -63,14 +66,15 @@ class Bootstrap{ * Configuration handler
*/
include('classes/config.php');
-
+
Config::load_group('core', 'application/config/core.php');
spl_autoload_register('Bootstrap::autoload');
Debug::init();
- if(Config::get('core.composer',false))
- include ROOTDIR.'vendor/autoload.php';
+ if (Config::get('core.composer', false))
+ include ROOTDIR.'vendor/autoload.php';
- foreach(Config::get('core.routes') as $route)
- Route::add($route[0],$route[1],$route[2]);
+ foreach (Config::get('core.routes') as $route)
+ Route::add($route[0], $route[1], $route[2]);
}
+
}
\ No newline at end of file diff --git a/system/classes/config.php b/system/classes/config.php index 27b7fa6..75152ac 100644 --- a/system/classes/config.php +++ b/system/classes/config.php @@ -6,61 +6,65 @@ * and later access them via the get() method.
* @package Core
*/
-class Config {
-
- /**
- * Array of configuration files and values loaded from them
- * @var array
- * @access protected
- * @static
- */
+class Config
+{
+
+ /**
+ * Array of configuration files and values loaded from them
+ * @var array
+ * @access protected
+ * @static
+ */
protected static $groups = array();
-
+
/**
- * Loads a group configuration file it has not been loaded before and
+ * Loads a group configuration file it has not been loaded before and
* returns its options. If the group doesn't exist creates an empty one
- *
- * @param string $name Name of the configuration group to load
- * @return array Array of options for this group
- * @access public
- * @static
- */
- public static function get_group($name) {
-
- if (!isset(Config::$groups[$name])) {
+ *
+ * @param string $name Name of the configuration group to load
+ * @return array Array of options for this group
+ * @access public
+ * @static
+ */
+ public static function get_group($name)
+ {
+
+ if (!isset(Config::$groups[$name]))
+ {
$file = Misc::find_file('config', $name);
-
+
if (!$file)
Config::$groups[$name] = array(
'file' => APPDIR.'config/'.$name.'.php',
'options' => array()
);
else
- Config::load_group($name,$file);
+ Config::load_group($name, $file);
}
-
+
return Config::$groups[$name]['options'];
}
-
+
/**
- * Loads group from file
- *
- * @param string $name Name to assign the loaded group
+ * Loads group from file
+ *
+ * @param string $name Name to assign the loaded group
* @param string $file File to load
- * @access public
- * @static
- */
- public static function load_group($name, $file) {
-
+ * @access public
+ * @static
+ */
+ public static function load_group($name, $file)
+ {
+
Config::$groups[$name] = array(
'file' => $file,
'options' => include($file)
);
}
-
- /**
- * Retrieves a configuration value. You can use a dot notation
+
+ /**
+ * Retrieves a configuration value. You can use a dot notation
* to access properties in group arrays. The first part of the key
* specifies the configuration file from which options should be loaded from
* <code>
@@ -68,80 +72,88 @@ class Config { * //from database.php configuration file
* Config::get('database.default.user');
* </code>
- *
- * @param string $key Configuration key to retrieve.
- * @param string $default Default value to return if the key is not found.
- * @return mixed Configuration value
- * @access public
- * @throws Exception If default value is not specified and the key is not found
- * @static
- */
- public static function get() {
+ *
+ * @param string $key Configuration key to retrieve.
+ * @param string $default Default value to return if the key is not found.
+ * @return mixed Configuration value
+ * @access public
+ * @throws Exception If default value is not specified and the key is not found
+ * @static
+ */
+ public static function get()
+ {
$p = func_get_args();
-
+
$keys = explode('.', $p[0]);
$group_name = array_shift($keys);
$group = Config::get_group($group_name);
if (empty($keys))
return $group;
-
- $total=count($keys);
- foreach($keys as $i => $key) {
- if (isset($group[$key])) {
+
+ $total = count($keys);
+ foreach ($keys as $i => $key)
+ {
+ if (isset($group[$key]))
+ {
if ($i == $total - 1)
return $group[$key];
- $group=&$group[$key];
- }else {
- if (array_key_exists (1,$p))
+ $group = &$group[$key];
+ }else
+ {
+ if (array_key_exists(1, $p))
return $p[1];
throw new Exception("Configuration not set for {$p[0]}.");
}
}
}
-
+
/**
- * Sets a configuration option.
- *
- * @param string $key Configuration key to set
+ * Sets a configuration option.
+ *
+ * @param string $key Configuration key to set
* @param string $value Value to set for this option
- * @access public
- * @static
- */
- public static function set($key,$value){
+ * @access public
+ * @static
+ */
+ public static function set($key, $value)
+ {
$keys = explode('.', $key);
$group_name = array_shift($keys);
$group = Config::get_group($group_name);
$subgroup = &$group;
-
- foreach($keys as $i => $key) {
-
- if ($i == count($keys) - 1) {
-
+
+ foreach ($keys as $i => $key)
+ {
+
+ if ($i == count($keys) - 1)
+ {
+
$subgroup[$key] = $value;
-
- } else {
-
- if(!isset($subgroup[$key])||!is_array($subgroup[$key]))
- $subgroup[$key]=array();
+ }
+ else
+ {
+
+ if (!isset($subgroup[$key]) || !is_array($subgroup[$key]))
+ $subgroup[$key] = array();
$subgroup = & $subgroup[$key];
-
}
}
-
+
Config::$groups[$group_name]['options'] = $group;
}
-
+
/**
- * Writes a configuration group back to the file it was loaded from
- *
- * @param string $group Name of the group to write
- * @access public
- * @static
- */
- public static function write($group){
+ * Writes a configuration group back to the file it was loaded from
+ *
+ * @param string $group Name of the group to write
+ * @access public
+ * @static
+ */
+ public static function write($group)
+ {
Config::get_group($group);
- $group=Config::$groups[$group];
- file_put_contents($group['file'],"<?php\r\nreturn ".var_export($group['options'],true).";");
+ $group = Config::$groups[$group];
+ file_put_contents($group['file'], "<?php\r\nreturn ".var_export($group['options'], true).";");
}
}
\ No newline at end of file diff --git a/system/classes/controller.php b/system/classes/controller.php index a1aff33..df0af7b 100644 --- a/system/classes/controller.php +++ b/system/classes/controller.php @@ -1,85 +1,93 @@ -<?php
+<?php
/**
* Base Controller class. Controllers contain the logic of your website,
* each action representing a reply to a particular request, e.g. a single page.
* @package Core
*/
-class Controller {
+class Controller
+{
- /**
- * Request for this controller. Holds all input data.
- * @var Request
- * @access public
- */
+ /**
+ * Request for this controller. Holds all input data.
+ * @var Request
+ * @access public
+ */
public $request;
- /**
- * Response for this controller. It will be updated with headers and
+ /**
+ * Response for this controller. It will be updated with headers and
* response body during controller execution
- * @var Response
- * @access public
- */
+ * @var Response
+ * @access public
+ */
public $response;
- /**
- * If set to False stops controller execution
- * @var boolean
- * @access public
- */
- public $execute=true;
+ /**
+ * If set to False stops controller execution
+ * @var boolean
+ * @access public
+ */
+ public $execute = true;
- /**
- * This method is called before the action.
- * You can override it if you need to,
+ /**
+ * This method is called before the action.
+ * You can override it if you need to,
* it doesn't do anything by default.
- *
- * @return void
- * @access public
- */
- public function before() {}
+ *
+ * @return void
+ * @access public
+ */
+ public function before()
+ {
- /**
- * This method is called after the action.
- * You can override it if you need to,
+ }
+
+ /**
+ * This method is called after the action.
+ * You can override it if you need to,
* it doesn't do anything by default.
- *
- * @return void
- * @access public
- */
- public function after() { }
+ *
+ * @return void
+ * @access public
+ */
+ public function after()
+ {
+
+ }
- /**
- * Creates new Controller
- *
- * @return void
- * @access public
- */
- public function __construct() {
- $this->response=new Response;
+ /**
+ * Creates new Controller
+ *
+ * @return void
+ * @access public
+ */
+ public function __construct()
+ {
+ $this->response = new Response;
}
- /**
- * Runs the appropriate action.
+ /**
+ * Runs the appropriate action.
* It will execute the before() method before the action
* and after() method after the action finishes.
- *
- * @param string $action Name of the action to execute.
- * @return void
- * @access public
- * @throws Exception If the specified action doesn't exist
- */
- public function run($action) {
+ *
+ * @param string $action Name of the action to execute.
+ * @return void
+ * @access public
+ * @throws Exception If the specified action doesn't exist
+ */
+ public function run($action)
+ {
$action = 'action_'.$action;
if (!method_exists($this, $action))
- throw new Exception("Method {$action} doesn't exist in ".get_class($this),404);
- $this->execute=true;
+ throw new Exception("Method {$action} doesn't exist in ".get_class($this), 404);
+ $this->execute = true;
$this->before();
- if($this->execute)
+ if ($this->execute)
$this->$action();
- if($this->execute)
+ if ($this->execute)
$this->after();
}
-
}
\ No newline at end of file diff --git a/system/classes/debug.php b/system/classes/debug.php index d1593e3..615ab3e 100644 --- a/system/classes/debug.php +++ b/system/classes/debug.php @@ -4,128 +4,140 @@ * Handles error reporting and debugging.
* @package Core
*/
-class Debug {
+class Debug
+{
- /**
- * An array of logged items
- * @var array
- * @access public
- * @static
- */
- public static $logged=array();
+ /**
+ * An array of logged items
+ * @var array
+ * @access public
+ * @static
+ */
+ public static $logged = array();
- /**
- * Displays the error page. If you have 'silent_errors' enabled in
+ /**
+ * Displays the error page. If you have 'silent_errors' enabled in
* core.php config file, a small message will be shown instead.
- *
- * @return void
- * @access public
- */
- public function render_error($exception) {
+ *
+ * @return void
+ * @access public
+ */
+ public function render_error($exception)
+ {
ob_end_clean();
-
- if($exception->getCode()==404){
- $status='404 Not Found';
- }else {
- $status='503 Service Temporarily Unavailable';
+
+ if ($exception->getCode() == 404)
+ {
+ $status = '404 Not Found';
+ }
+ else
+ {
+ $status = '503 Service Temporarily Unavailable';
}
-
- header($_SERVER["SERVER_PROTOCOL"].' '.$status);
+
+ header($_SERVER["SERVER_PROTOCOL"].' '.$status);
header("Status: {$status}");
-
- if (Config::get('core.errors.silent', false)) {
+
+ if (Config::get('core.errors.silent', false))
+ {
echo $status;
return;
}
-
+
$view = View::get('debug');
$view->exception = $exception;
$view->log = Debug::$logged;
echo $view->render();
}
- /**
- * Catches errors and exceptions and sends them
+ /**
+ * Catches errors and exceptions and sends them
* to the configured handler if one is present,
* otherwise render_error() will be called.
- *
- * @param Exception $exception Caught exception
- * @return void
- * @access public
- * @static
- */
- public static function onError($exception) {
+ *
+ * @param Exception $exception Caught exception
+ * @return void
+ * @access public
+ * @static
+ */
+ public static function onError($exception)
+ {
set_exception_handler(array('Debug', 'internalException'));
- set_error_handler ( array('Debug', 'internalError'), E_ALL);
+ set_error_handler(array('Debug', 'internalError'), E_ALL);
$handler = Config::get('core.errors.handler', 'Debug::render_error');
- call_user_func($handler,$exception);
+ call_user_func($handler, $exception);
}
- /**
- * Converts PHP Errors to Exceptions
- *
- * @param string $errno Error number
- * @param string $errstr Error message
- * @param string $errfile File in which the error occurred
- * @param string $errline Line at which the error occurred
- * @return void
- * @access public
- * @throws ErrorException Throws converted exception to be immediately caught
- * @static
- */
- public static function errorHandler($errno, $errstr, $errfile, $errline) {
+ /**
+ * Converts PHP Errors to Exceptions
+ *
+ * @param string $errno Error number
+ * @param string $errstr Error message
+ * @param string $errfile File in which the error occurred
+ * @param string $errline Line at which the error occurred
+ * @return void
+ * @access public
+ * @throws ErrorException Throws converted exception to be immediately caught
+ * @static
+ */
+ public static function errorHandler($errno, $errstr, $errfile, $errline)
+ {
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
- /**
- * Handles exceptions that occurred while inside the error handler. Prevents recursion.
- *
- * @param Exception $exception Caught exception
- * @return void
- * @access public
- * @static
- */
- public static function internalException($exception) {
+ /**
+ * Handles exceptions that occurred while inside the error handler. Prevents recursion.
+ *
+ * @param Exception $exception Caught exception
+ * @return void
+ * @access public
+ * @static
+ */
+ public static function internalException($exception)
+ {
echo $exception->getMessage().' in '.$exception->getFile().' on line '.$exception->getLine();
}
- /**
- * Handles errors that occurred while inside the error handler. Prevents recursion.
- *
- * @param string $errno Error number
- * @param string $errstr Error message
- * @param string $errfile File in which the error occurred
- * @param string $errline Line at which the error occurred
- * @return void
- * @access public
- * @static
- */
- public static function internalError($errno, $errstr, $errfile, $errline) {
+ /**
+ * Handles errors that occurred while inside the error handler. Prevents recursion.
+ *
+ * @param string $errno Error number
+ * @param string $errstr Error message
+ * @param string $errfile File in which the error occurred
+ * @param string $errline Line at which the error occurred
+ * @return void
+ * @access public
+ * @static
+ */
+ public static function internalError($errno, $errstr, $errfile, $errline)
+ {
echo $errstr.' in '.$errfile.' on line '.$errline;
}
- /**
- * Initializes the error handler
- *
- * @return void
- * @access public
- * @static
- */
- public static function init(){
+ /**
+ * Initializes the error handler
+ *
+ * @return void
+ * @access public
+ * @static
+ */
+ public static function init()
+ {
set_exception_handler(array('Debug', 'onError'));
- set_error_handler ( array('Debug', 'errorHandler'), E_ALL);
+ set_error_handler(array('Debug', 'errorHandler'), E_ALL);
}
- /**
- * Adds an item to the log.
- *
- * @param mixed $val Item to be logged
- * @return void
- * @access public
- * @static
- */
- public static function log($val){
- array_unshift(Debug::$logged,$val);
+ /**
+ * Adds an item to the log.
+ *
+ * @param mixed $val Item to be logged
+ * @return void
+ * @access public
+ * @static
+ */
+ public static function log($val)
+ {
+ array_unshift(Debug::$logged, $val);
}
-
+
}
\ No newline at end of file diff --git a/system/classes/misc.php b/system/classes/misc.php index 65f2ec6..b14e9eb 100644 --- a/system/classes/misc.php +++ b/system/classes/misc.php @@ -6,70 +6,72 @@ */
class Misc
{
- /**
- * Retrieve value from array by key, with default value support.
- *
- * @param array $array Input array
- * @param string $key Key to retrieve from the array
- * @param mixed $default Default value to return if the key is not found
- * @return mixed An array value if it was found or default value if it is not
- * @access public
- * @static
- */
- public static function arr($array,$key,$default=null)
- {
- if (isset($array[$key]))
- {
- return $array[$key];
- }
- return $default;
- }
- /**
- * Finds full path to a specified file
- * It will search in the /application folder first, then in all enabled modules
- * and then the /system folder
- *
- * @param string $subfolder Subfolder to search in e.g. 'classes' or 'views'
- * @param string $name Name of the file without extension
- * @param string $extension File extension
- * @param boolean $return_all If 'true' returns all mathced files as array,
- * otherwise returns the first file found
- * @return mixed Full path to the file or False if it is not found
- * @access public
- * @static
- */
- public static function find_file($subfolder, $name, $extension = 'php', $return_all = false )
- {
- $folders = array(APPDIR);
+ /**
+ * Retrieve value from array by key, with default value support.
+ *
+ * @param array $array Input array
+ * @param string $key Key to retrieve from the array
+ * @param mixed $default Default value to return if the key is not found
+ * @return mixed An array value if it was found or default value if it is not
+ * @access public
+ * @static
+ */
+ public static function arr($array, $key, $default = null)
+ {
+ if (isset($array[$key]))
+ {
+ return $array[$key];
+ }
+ return $default;
+ }
- foreach(Config::get('core.modules', array()) as $module)
- {
- $folders[] = MODDIR.$module.'/';
- }
- $folders[] = SYSDIR;
+ /**
+ * Finds full path to a specified file
+ * It will search in the /application folder first, then in all enabled modules
+ * and then the /system folder
+ *
+ * @param string $subfolder Subfolder to search in e.g. 'classes' or 'views'
+ * @param string $name Name of the file without extension
+ * @param string $extension File extension
+ * @param boolean $return_all If 'true' returns all mathced files as array,
+ * otherwise returns the first file found
+ * @return mixed Full path to the file or False if it is not found
+ * @access public
+ * @static
+ */
+ public static function find_file($subfolder, $name, $extension = 'php', $return_all = false)
+ {
+ $folders = array(APPDIR);
- $fname = $name.'.'.$extension;
- $found_files = array();
+ foreach (Config::get('core.modules', array()) as $module)
+ {
+ $folders[] = MODDIR.$module.'/';
+ }
+ $folders[] = SYSDIR;
- foreach ($folders as $folder)
- {
- $file = $folder.$subfolder.'/'.$fname;
- if (file_exists($file))
- {
- if (!$return_all)
- {
- return($file);
- }
- $found_files[] = $file;
- }
- }
+ $fname = $name.'.'.$extension;
+ $found_files = array();
- if (!empty($found_files))
- {
- return $found_files;
- }
+ foreach ($folders as $folder)
+ {
+ $file = $folder.$subfolder.'/'.$fname;
+ if (file_exists($file))
+ {
+ if (!$return_all)
+ {
+ return($file);
+ }
+ $found_files[] = $file;
+ }
+ }
+
+ if (!empty($found_files))
+ {
+ return $found_files;
+ }
+
+ return false;
+ }
- return false;
- }
}
diff --git a/system/classes/request.php b/system/classes/request.php index 4105901..1b95b42 100644 --- a/system/classes/request.php +++ b/system/classes/request.php @@ -4,142 +4,150 @@ * Handles client request. * @package Core */ -class Request { +class Request +{ - /** - * Stores POST data - * @var array - * @access private - */ + /** + * Stores POST data + * @var array + * @access private + */ private $_post; - /** - * Stores GET data - * @var array - * @access private - */ + /** + * Stores GET data + * @var array + * @access private + */ private $_get; - /** - * Current Route - * @var Route - * @access public - */ + /** + * Current Route + * @var Route + * @access public + */ public $route; - /** - * Request method - * @var string - * @access public - */ + /** + * Request method + * @var string + * @access public + */ public $method; - + /** - * Creates a new request - * - * @param Route $route Route for this request - * @param string $method HTTP method for the request (e.g. GET, POST) + * Creates a new request + * + * @param Route $route Route for this request + * @param string $method HTTP method for the request (e.g. GET, POST) * @param array $post Array of POST data * @param array $get Array of GET data * @param array $server Array of SERVER data - * @return Request Initialized request + * @return Request Initialized request * - * @access public - */ - public function __construct($route, $method="GET", $post = array(), $get = array(), $server = array()) { - $this->route = $route; - $this->method = $method; - $this->_post = $post; - $this->_get = $get; + * @access public + */ + public function __construct($route, $method = "GET", $post = array(), $get = array(), $server = array()) + { + $this->route = $route; + $this->method = $method; + $this->_post = $post; + $this->_get = $get; $this->_server = $server; } - - /** - * Retrieves a GET parameter - * - * @param string $key Parameter key - * @param mixed $default Default value - * @return mixed Returns a value if a key is specified, + + /** + * Retrieves a GET parameter + * + * @param string $key Parameter key + * @param mixed $default Default value + * @return mixed Returns a value if a key is specified, * or an array of GET parameters if it isn't. - * @access public - */ - public function get($key = null, $default = null) { + * @access public + */ + public function get($key = null, $default = null) + { if ($key == null) return $this->_get; - return Misc::arr($this->_get,$key,$default); + return Misc::arr($this->_get, $key, $default); } - /** - * Retrieves a POST parameter - * - * @param string $key Parameter key - * @param mixed $default Default value - * @return mixed Returns a value if a key is specified, + /** + * Retrieves a POST parameter + * + * @param string $key Parameter key + * @param mixed $default Default value + * @return mixed Returns a value if a key is specified, * or an array of POST parameters if it isn't. - * @access public - */ - public function post($key = null, $default = null) { + * @access public + */ + public function post($key = null, $default = null) + { if ($key == null) return $this->_post; - return Misc::arr($this->_post,$key,$default); + return Misc::arr($this->_post, $key, $default); } - + /** - * Retrieves a SERVER parameter - * - * @param string $key Parameter key - * @param mixed $default Default value - * @return mixed Returns a value if a key is specified, + * Retrieves a SERVER parameter + * + * @param string $key Parameter key + * @param mixed $default Default value + * @return mixed Returns a value if a key is specified, * or an array of SERVER parameters if it isn't. - * @access public - */ - public function server($key = null, $default = null) { + * @access public + */ + public function server($key = null, $default = null) + { if ($key == null) return $this->_server; - return Misc::arr($this->_server,$key,$default); + return Misc::arr($this->_server, $key, $default); } - - /** - * Retrieves a Route parameter - * - * @param string $key Parameter key - * @param mixed $default Default value - * @return mixed Returns a value if a key is specified, + + /** + * Retrieves a Route parameter + * + * @param string $key Parameter key + * @param mixed $default Default value + * @return mixed Returns a value if a key is specified, * or an array of Route parameters if it isn't. - * @access public - */ - public function param($key = null, $default = null) { + * @access public + */ + public function param($key = null, $default = null) + { if ($key == null) return $this->route->params; - return Misc::arr($this->route->params,$key,$default); + return Misc::arr($this->route->params, $key, $default); } - /** - * Initializes the routed Controller and executes specified action - * - * @return Response A Response object with the body and headers set - * @access public - */ - public function execute() { + /** + * Initializes the routed Controller and executes specified action + * + * @return Response A Response object with the body and headers set + * @access public + */ + public function execute() + { $controller = $this->param('controller').'_Controller'; if (!class_exists($controller)) - throw new Exception("Class {$controller} doesn't exist",404); + throw new Exception("Class {$controller} doesn't exist", 404); $controller = new $controller; $controller->request = $this; $controller->run($this->param('action')); return $controller->response; } - /** - * Creates a Request representing current HTTP request. - * - * @return Request Request - * @access public - * @static - */ - public static function create() { + /** + * Creates a Request representing current HTTP request. + * + * @return Request Request + * @access public + * @static + */ + public static function create() + { $uri = $_SERVER['REQUEST_URI']; - $basepath=Config::get('core.basepath','/'); + $basepath = Config::get('core.basepath', '/'); $uri = preg_replace("#^{$basepath}(?:index\.php/)?#i", '/', $uri); $url_parts = parse_url($uri); return new Request(Route::match($url_parts['path']), $_SERVER['REQUEST_METHOD'], $_POST, $_GET, $_SERVER); diff --git a/system/classes/response.php b/system/classes/response.php index b1b38cc..a4b737e 100644 --- a/system/classes/response.php +++ b/system/classes/response.php @@ -4,67 +4,72 @@ * Handles the response that is sent back to the client.
* @package Core
*/
-class Response {
+class Response
+{
- /**
- * Headers for the response
- * @var array
- * @access public
- */
+ /**
+ * Headers for the response
+ * @var array
+ * @access public
+ */
public $headers = array(
'Content-Type: text/html; charset=utf-8'
);
- /**
- * Response body
- * @var string
- * @access public
- */
+ /**
+ * Response body
+ * @var string
+ * @access public
+ */
public $body;
- /**
- * Add header to the response
- *
- * @param string $header Header content
- * @return void
- * @access public
- */
- public function add_header($header){
- $this->headers[]=$header;
+ /**
+ * Add header to the response
+ *
+ * @param string $header Header content
+ * @return void
+ * @access public
+ */
+ public function add_header($header)
+ {
+ $this->headers[] = $header;
}
- /**
- * Add redirection header
- *
- * @param string $url URL to redirect the client to
- * @return void
- * @access public
- */
- public function redirect($url){
+ /**
+ * Add redirection header
+ *
+ * @param string $url URL to redirect the client to
+ * @return void
+ * @access public
+ */
+ public function redirect($url)
+ {
$this->add_header("Location: $url");
}
- /**
- * Sends headers to the client
- *
- * @return Response Same Response object, for method chaining
- * @access public
- */
- public function send_headers(){
- foreach($this->headers as $header)
+ /**
+ * Sends headers to the client
+ *
+ * @return Response Same Response object, for method chaining
+ * @access public
+ */
+ public function send_headers()
+ {
+ foreach ($this->headers as $header)
header($header);
return $this;
}
- /**
- * Send response body to the client
- *
- * @return object Same Response object, for method chaining
- * @access public
- */
- public function send_body(){
+ /**
+ * Send response body to the client
+ *
+ * @return object Same Response object, for method chaining
+ * @access public
+ */
+ public function send_body()
+ {
echo $this->body;
return $this;
}
-
+
}
\ No newline at end of file diff --git a/system/classes/route.php b/system/classes/route.php index 244a0c7..04b9a6c 100644 --- a/system/classes/route.php +++ b/system/classes/route.php @@ -4,198 +4,210 @@ * Routing class to extract and parse request parameters from the URL. * @package Core */ -class Route { +class Route +{ - /** - * Name of the route. - * @var string - * @access public - */ + /** + * Name of the route. + * @var string + * @access public + */ public $name; - + /** - * Rule for this route. - * @var mixed - * @access public - */ + * Rule for this route. + * @var mixed + * @access public + */ public $rule; - + /** - * Default parameters for this route. - * @var mixed - * @access public - */ + * Default parameters for this route. + * @var mixed + * @access public + */ public $defaults; - - /** - * Extracted parameters - * @var array - * @access public - */ - public $params=array(); - - /** - * Associative array of route rules. - * @var array - * @access private - * @static - */ - private static $rules=array(); - + + /** + * Extracted parameters + * @var array + * @access public + */ + public $params = array(); + + /** + * Associative array of route rules. + * @var array + * @access private + * @static + */ + private static $rules = array(); + /** - * Associative array of route instances. - * @var array - * @access private - * @static - */ + * Associative array of route instances. + * @var array + * @access private + * @static + */ private static $routes = array(); - + /** - * Constructs a route. - * - * @param string $name Name of the route + * Constructs a route. + * + * @param string $name Name of the route * @param mixed $rule Rule for this route * @param array $defaults Default parameters for the route * @return Route Initialized Route * @access protected - */ - protected function __construct($name, $rule, $defaults) { + */ + protected function __construct($name, $rule, $defaults) + { $this->name = $name; $this->rule = $rule; $this->defaults = $defaults; } - + /** - * Generates a url for a route - * - * @param array $params Parameters to substitute in the route + * Generates a url for a route + * + * @param array $params Parameters to substitute in the route * @param bool $absolute Whether to return an absolute url * @param string $protocol Protocol to use for absolute url * @return string Generated url * @access public - */ - public function url($params = array(), $absolute = false, $protocol = 'http') { + */ + public function url($params = array(), $absolute = false, $protocol = 'http') + { if (is_callable($this->rule)) throw new Exception("The rule for '{$this->name}' route is a function and cannot be reversed"); - - $url = is_array($this->rule)?$this->rule[0]:$this->rule; - + + $url = is_array($this->rule) ? $this->rule[0] : $this->rule; + $replace = array(); $params = array_merge($this->defaults, $params); - foreach($params as $key => $value) + foreach ($params as $key => $value) $replace["<{$key}>"] = $value; $url = str_replace(array_keys($replace), array_values($replace), $url); - + $count = 1; - $chars='[^\(\)]*?'; - while($count>0) + $chars = '[^\(\)]*?'; + while ($count > 0) $url = preg_replace("#\({$chars}<{$chars}>{$chars}\)#", '', $url, -1, $count); - + $url = str_replace(array('(', ')'), '', $url); - + if ($absolute) $url = $protocol.'://'.$_SERVER['HTTP_HOST'].$url; - + return $url; } - - /** - * Ads a route - * - * @param string $name Name of the route. Routes with the same name will override one another. - * @param mixed $rule Either an expression to match URI against or a function that will + + /** + * Ads a route + * + * @param string $name Name of the route. Routes with the same name will override one another. + * @param mixed $rule Either an expression to match URI against or a function that will * be passed the URI and must return either an associative array of * extracted parameters (if it matches) or False. - * @param array $defaults An associated array of default values. - * @return void - * @access public - * @static - */ - public static function add($name, $rule, $defaults = array()) { - Route::$rules[$name]=array( - 'rule'=>$rule, - 'defaults'=>$defaults + * @param array $defaults An associated array of default values. + * @return void + * @access public + * @static + */ + public static function add($name, $rule, $defaults = array()) + { + Route::$rules[$name] = array( + 'rule' => $rule, + 'defaults' => $defaults ); } - /** - * Gets route by name - * - * @param string $name Route name - * @return Route - * @access public + /** + * Gets route by name + * + * @param string $name Route name + * @return Route + * @access public * @throws Exception If specified route doesn't exist - * @static - */ - public static function get($name) { + * @static + */ + public static function get($name) + { if (!isset(Route::$rules[$name])) throw new Exception("Route {$name} not found."); - - if (!isset(Route::$routes[$name])) { + + if (!isset(Route::$routes[$name])) + { $rules = Route::$rules[$name]; Route::$routes[$name] = new static($name, $rules['rule'], $rules['defaults']); } - + return Route::$routes[$name]; } - /** - * Matches the URI against available routes to find the correct one. - * - * @param string $uri Request URI - * @return Route - * @access public - * @throws Exception If no route matches the URI - * @throws Exception If route matched but no Controller was defined for it - * @throws Exception If route matched but no action was defined for it - * @static - */ - public static function match($uri) { + /** + * Matches the URI against available routes to find the correct one. + * + * @param string $uri Request URI + * @return Route + * @access public + * @throws Exception If no route matches the URI + * @throws Exception If route matched but no Controller was defined for it + * @throws Exception If route matched but no action was defined for it + * @static + */ + public static function match($uri) + { $matched = false; - foreach(Route::$rules as $name=>$rule) { - $rule=$rule['rule']; - if (is_callable($rule)) { - if (($data = $rule($uri)) !== FALSE) { + foreach (Route::$rules as $name => $rule) + { + $rule = $rule['rule']; + if (is_callable($rule)) + { + if (($data = $rule($uri)) !== FALSE) + { $matched = $name; break; } - }else { - $pattern = is_array($rule)?$rule[0]:$rule; + } + else + { + $pattern = is_array($rule) ? $rule[0] : $rule; $pattern = str_replace(')', ')?', $pattern); - - $pattern=preg_replace_callback('/<.*?>/', - function($str) use ($rule){ - $str=$str[0]; - $regexp='[a-zA-Z0-9\-\._]+'; - if(is_array($rule)) - $regexp=Misc::arr($rule[1],str_replace(array('<','>'),'',$str),$regexp); + + $pattern = preg_replace_callback('/<.*?>/', function($str) use ($rule) { + $str = $str[0]; + $regexp = '[a-zA-Z0-9\-\._]+'; + if (is_array($rule)) + $regexp = Misc::arr($rule[1], str_replace(array('<', '>'), '', $str), $regexp); return '(?P'.$str.$regexp.')'; - },$pattern); - - preg_match('#^'.$pattern.'/?$#',$uri,$match); - if(!empty($match[0])){ - $matched=$name; - $data=array(); - foreach($match as $k=>$v) - if(!is_numeric($k)) - $data[$k]=$v; + }, $pattern); + + preg_match('#^'.$pattern.'/?$#', $uri, $match); + if (!empty($match[0])) + { + $matched = $name; + $data = array(); + foreach ($match as $k => $v) + if (!is_numeric($k)) + $data[$k] = $v; break; } } } - if($matched==false) - throw new Exception('No route matched your request',404); - $rule=Route::$rules[$matched]; - - $params=array_merge($rule['defaults'],$data); - - if(!isset($params['controller'])) - throw new Exception("Route {$matched} matched, but no controller was defined for this route",404); - if(!isset($params['action'])) - throw new Exception("Route {$matched} matched with controller {$params['controller']}, but no action was defined for this route",404); - - $route=Route::get($matched); - $route->params=$params; + if ($matched == false) + throw new Exception('No route matched your request', 404); + $rule = Route::$rules[$matched]; + + $params = array_merge($rule['defaults'], $data); + + if (!isset($params['controller'])) + throw new Exception("Route {$matched} matched, but no controller was defined for this route", 404); + if (!isset($params['action'])) + throw new Exception("Route {$matched} matched with controller {$params['controller']}, but no action was defined for this route", 404); + + $route = Route::get($matched); + $route->params = $params; return $route; } diff --git a/system/classes/session.php b/system/classes/session.php index 3749396..f08b2b7 100644 --- a/system/classes/session.php +++ b/system/classes/session.php @@ -4,100 +4,112 @@ * Simple class for accessing session data * @package Core */ -class Session{ +class Session +{ - /** - * Makes sure the session is initialized - * - * @return void - * @access private - * @static - */ - private static function check(){ - if(!session_id()){ + /** + * Makes sure the session is initialized + * + * @return void + * @access private + * @static + */ + private static function check() + { + if (!session_id()) + { session_start(); } } - /** - * Gets a session variable - * - * @param string $key Variable name - * @param mixed $default Default value - * @return mixed Session value - * @access public - * @static - */ - public static function get($key, $default = null) { + /** + * Gets a session variable + * + * @param string $key Variable name + * @param mixed $default Default value + * @return mixed Session value + * @access public + * @static + */ + public static function get($key, $default = null) + { Session::check(); - return Misc::arr($_SESSION,$key,$default); + return Misc::arr($_SESSION, $key, $default); } - /** - * Sets a session variable - * - * @param string $key Variable name - * @param mixed $val Variable value - * @return void - * @access public - * @static - */ - public static function set($key, $val) { + /** + * Sets a session variable + * + * @param string $key Variable name + * @param mixed $val Variable value + * @return void + * @access public + * @static + */ + public static function set($key, $val) + { Session::check(); - $_SESSION[$key]=$val; + $_SESSION[$key] = $val; } - + /** - * Removes a session variable - * - * @param string $key Variable name - * @return void - * @access public - * @static - */ - public static function remove($key) { + * Removes a session variable + * + * @param string $key Variable name + * @return void + * @access public + * @static + */ + public static function remove($key) + { Session::check(); - + if (!isset($_SESSION[$key])) return; - + $var = $_SESSION[$key]; unset($_SESSION[$key], $var); } - + /** - * Resets the session - * - * @return void - * @access public - * @static - */ - public static function reset() { + * Resets the session + * + * @return void + * @access public + * @static + */ + public static function reset() + { Session::check(); - $_SESSION=array(); + $_SESSION = array(); } - + /** - * Gets ot sets flash messages. + * Gets ot sets flash messages. * If the value parameter is passed the message is set, otherwise it is retrieved. * After the message is retrieved for the first time it is removed. - * + * * @param $key The name of the flash message * @param $val Flash message content - * @return mixed - * @access public - * @static - */ - public static function flash($key,$val = null) { + * @return mixed + * @access public + * @static + */ + public static function flash($key, $val = null) + { Session::check(); - $key="flash_{$key}"; - if($val != null) { - Session::set($key,$val); - }else { + $key = "flash_{$key}"; + if ($val != null) + { + Session::set($key, $val); + } + else + { $val = Session::get($key); Session::remove($key); } - + return $val; } + }
\ No newline at end of file diff --git a/system/classes/view.php b/system/classes/view.php index 7c319d0..49e9661 100644 --- a/system/classes/view.php +++ b/system/classes/view.php @@ -4,82 +4,86 @@ * Manages passing variables to templates and rendering them * @package Core */ -class View{ +class View +{ - /** - * Full path to template file - * @var string - * @access private - */ + /** + * Full path to template file + * @var string + * @access private + */ protected $path; - /** - * The name of the view. - * @var string - * @access public - */ + /** + * The name of the view. + * @var string + * @access public + */ public $name; - /** - * Stores all the variables passed to the view - * @var array - * @access protected - */ + /** + * Stores all the variables passed to the view + * @var array + * @access protected + */ protected $_data = array(); /** - * File extension of the templates - * @var string - * @access protected - */ + * File extension of the templates + * @var string + * @access protected + */ protected $_extension = 'php'; - + /** - * Constructs the view - * - * @param string $name The name of the template to use - * @return View - * @throws Exception If specified template is not found - * @access protected - */ - protected function __construct($name){ + * Constructs the view + * + * @param string $name The name of the template to use + * @return View + * @throws Exception If specified template is not found + * @access protected + */ + protected function __construct($name) + { $this->name = $name; - $file = Misc::find_file('views', $name,$this->_extension); - + $file = Misc::find_file('views', $name, $this->_extension); + if ($file == false) throw new Exception("View {$name} not found."); - - $this->path=$file; + + $this->path = $file; } - - /** - * Manages storing the data passed to the view as properties - * - * @param string $key Property name - * @param string $val Property value - * @return void - * @access public - */ - public function __set($key, $val) { - $this->_data[$key]=$val; + + /** + * Manages storing the data passed to the view as properties + * + * @param string $key Property name + * @param string $val Property value + * @return void + * @access public + */ + public function __set($key, $val) + { + $this->_data[$key] = $val; } - /** - * Manages accessing passed data as properties - * - * @param string $key Property name - * @return mixed Property value - * @access public - * @throws Exception If the property is not found - */ - public function __get($key){ + /** + * Manages accessing passed data as properties + * + * @param string $key Property name + * @return mixed Property value + * @access public + * @throws Exception If the property is not found + */ + public function __get($key) + { if (isset($this->_data[$key])) return $this->_data[$key]; - throw new Exception("Value {$key} not set for view {$this->name}"); + throw new Exception("Value {$key} not set for view {$this->name}"); } - /** - * Renders the template, all dynamically set properties + /** + * Renders the template, all dynamically set properties * will be available inside the view file as variables. * Example: * <code> @@ -87,28 +91,31 @@ class View{ * $view->title = "Page title"; * echo $view->render(); * </code> - * - * @return string Rendered template - * @access public - */ - public function render() { + * + * @return string Rendered template + * @access public + */ + public function render() + { extract($this->_data); ob_start(); include($this->path); return ob_get_clean(); } - /** - * Shorthand for constructing a view. + /** + * Shorthand for constructing a view. * - * @param string $name The name of the template to use - * @return View - * @throws Exception If specified template is not found - * @static - * @access public - */ - public static function get($name){ + * @param string $name The name of the template to use + * @return View + * @throws Exception If specified template is not found + * @static + * @access public + */ + public static function get($name) + { $class = get_called_class(); return new $class($name); } + }
\ No newline at end of file diff --git a/system/views/debug.php b/system/views/debug.php index 0d78601..374abb5 100644 --- a/system/views/debug.php +++ b/system/views/debug.php @@ -30,7 +30,7 @@ margin:10px; } .code{ - + padding:10px; } .highlight{ @@ -55,7 +55,7 @@ border-bottom: 1px solid black; } .log.odd{ - + } pre{ margin:0px; @@ -66,61 +66,63 @@ </style> </head> <body> - <?php - $rawblocks=array_merge(array(array( - 'file'=>$exception->getFile(), - 'line'=>$exception->getLine() + <?php + $rawblocks = array_merge(array(array( + 'file' => $exception->getFile(), + 'line' => $exception->getLine() )), $exception->getTrace()); - $blocks = array(); - foreach($rawblocks as $block){ - if(!isset($block['file'])) + $blocks = array(); + foreach ($rawblocks as $block) + { + if (!isset($block['file'])) + continue; + //avoid duplicates + if (count($blocks) > 0) + { + $last = $blocks[count($blocks) - 1]; + if ($block['file'] == $last['file'] && $block['line'] == $last['line']) continue; - //avoid duplicates - if(count($blocks)>0){ - $last=$blocks[count($blocks)-1]; - if($block['file']==$last['file'] && $block['line']==$last['line']) - continue; - } - $blocks[]=$block; } - - + $blocks[] = $block; + } ?> <div id="content"> - <div id="exception"><?php echo str_replace("\n",'<br/>',$exception->getMessage()); ?></div> + <div id="exception"><?php echo str_replace("\n", '<br/>', $exception->getMessage()); ?></div> <div id="blocks"> - <?php foreach($blocks as $bkey=>$block): ?> - <div class="block <?php echo (!empty($log)&&$bkey==0)?'thick':''; ?>"> - <div class="file"><?php echo $block['file'];?></div> + <?php foreach ($blocks as $bkey => $block): ?> + <div class="block <?php echo (!empty($log) && $bkey == 0) ? 'thick' : ''; ?>"> + <div class="file"><?php echo $block['file']; ?></div> <div class="code"> - <?php - $line=$block['line']-1; - $code = explode("\n", file_get_contents($block['file'])); - $start = $line - 3; - if ($start < 0) $start = 0; - $end = $line + 3; - if($end>=count($code)) $end=count($code)-1; - $code=array_slice($code,$start,$end-$start,true); + <?php + $line = $block['line'] - 1; + $code = explode("\n", file_get_contents($block['file'])); + $start = $line - 3; + if ($start < 0) + $start = 0; + $end = $line + 3; + if ($end >= count($code)) + $end = count($code) - 1; + $code = array_slice($code, $start, $end - $start, true); ?> - - <?php foreach($code as $n=>$text):?> - <pre class="line <?php echo $n==$line?'highlight':''; ?>"><?php echo ($n+1).' '.htmlspecialchars($text); ?></pre> - <?php endforeach;?> + + <?php foreach ($code as $n => $text): ?> + <pre class="line <?php echo $n == $line ? 'highlight' : ''; ?>"><?php echo ($n + 1).' '.htmlspecialchars($text); ?></pre> + <?php endforeach; ?> </div> </div> - <?php if($bkey==0&&!empty($log)):?> + <?php if ($bkey == 0 && !empty($log)): ?> <div id="debug"> <div id="log">Logged values:</div> - <?php foreach($log as $key=>$val):?> - <div class="log <?php echo $key%2?'odd':''; ?>"> - <pre><?php var_export($val);?></pre> + <?php foreach ($log as $key => $val): ?> + <div class="log <?php echo $key % 2 ? 'odd' : ''; ?>"> + <pre><?php var_export($val); ?></pre> </div> - <?php endforeach;?> + <?php endforeach; ?> </div> <div id="log">Call stack:</div> - <?php endif;?> - <?php endforeach;?> + <?php endif; ?> +<?php endforeach; ?> </div> </div> </body> -</html>
\ No newline at end of file +</html>
\ No newline at end of file |