#0001
#0002
#0003
#0004
#0005
#0006
#0007
#0008
#0009
#0010
#0011
#0012
#0013
#0014
#0015
#0016
#0017
#0018
#0019
#0020
#0021
#0022
#0023
#0024
#0025
#0026
#0027
#0028
#0029
#0030
#0031
#0032
#0033
#0034
#0035
#0036
#0037
#0038
#0039
#0040
#0041
#0042
#0043
#0044
#0045
#0046
#0047
#0048
#0049
#0050
#0051
#0052
#0053
#0054
#0055
#0056
#0057
#0058
#0059
#0060
#0061
#0062
#0063
#0064
#0065
#0066
#0067
#0068
#0069
#0070
#0071
#0072
#0073
#0074
#0075
#0076
#0077
#0078
#0079
#0080
#0081
#0082
#0083
#0084
#0085
#0086
#0087
#0088
#0089
#0090
#0091
#0092
#0093
#0094
#0095
#0096
#0097
#0098
#0099
#0100
#0101
#0102
#0103
#0104
#0105
#0106
#0107
#0108
#0109
#0110
#0111
#0112
#0113
#0114
#0115
#0116
#0117
#0118
#0119
#0120
#0121
#0122
#0123
#0124
#0125
#0126
#0127
#0128
#0129
#0130
#0131
#0132
#0133
#0134
#0135
#0136
#0137
#0138
#0139
#0140
#0141
#0142
#0143
#0144
#0145
#0146
#0147
#0148
#0149
#0150
#0151
#0152
#0153
#0154
#0155
#0156
#0157
#0158
#0159
#0160
#0161
#0162
#0163
#0164
#0165
#0166
#0167
#0168
#0169
#0170
#0171
#0172
#0173
#0174
#0175
#0176
|
<?php /* * Framework base class. * * @todo test nested controllers */ class creovel { const VERSION = '0.01'; const RELEASE_DATE = 'Nov 24 2005 16:55:55'; /** * Run framework. * * @author Nesbert Hidalgo * @access public * @param array $events optional assoc. array of CONTORLLER, ACTION & ID * @param array $events optional assoc. array of params * @param bool $return_as_str optional returns controller as string * @return object */ public function run($events = null, $params = null, $return_as_str = false) { // set-up environment require_once( CONFIG_PATH . 'environments' . DS . ( $_ENV['mode'] = ( isset($_ENV['mode']) ? $_ENV['mode'] : 'development' ) ) . '.php' ); // set event and params $events = $events ? $events : self::get_events(); $params = $params ? $params : self::get_params(); // set controller & action $events['controller'] = $events['controller'] ? $events['controller'] : $_ENV['routes']['default']['controller']; $events['action'] = $events['action'] ? $events['action'] : $_ENV['routes']['default']['action']; // include controllers and helpers self::include_controller($events['nested_controller_path'] . $events['controller']); // create controller object and build the framework $controller = $events['controller'] . '_controller'; $controller = new $controller(); // set controller properties $controller->_set_events($events); $controller->_set_params($params); // execute action $controller->_execute_action(); // output to user return $controller->_output($return_as_str); } /** * Returns the framework events (CONTORLLER, ACTION & ID). * * @author Nesbert Hidalgo * @access public * @param string $event_to_return optional name of event to return * @return array */ public function get_events($event_to_return = null) { // read URI which was given in order to access this page, remove any trailing forward slashes $uri = explode('?', ( $_SERVER['REQUEST_URI']{strlen($_SERVER['REQUEST_URI']) - 1} == '/' ? substr($_SERVER['REQUEST_URI'], 0, strlen($_SERVER['REQUEST_URI']) - 1) : $_SERVER['REQUEST_URI'] )); // get event args from uri $args = explode(DS, substr($uri[0], 1)); // set events for framework with array indexes if ( count($args) > 2 ) { $events['controller'] = $args[ count($args) - 3 ]; $events['action'] = $args[ count($args) - 2 ]; $events['id'] = $args[ count($args) - 1 ]; } else { $events['controller'] = $args[0]; $events['action'] = $args[1]; } // check for nested controller if ( count($args) > 3 ) { $events['nested_controller_path'] = implode(DS, array_slice($args, 0, count($args) - 3) ) . DS; } return ( $event_to_return ? $events[$event_to_return] : $events ); } /** * Returns the framework params. * * @author Nesbert Hidalgo * @access public * @param string $param_to_return optional name of param to return * @return array */ public function get_params($param_to_return = null) { // get id from events $id = self::get_events('id'); // intialize params $params = $id ? array('id'=>$id) : array(); $requests = array($_GET, $_POST); // add each request add keys & values to $params foreach ( $requests as $request ) { if ( count($request) === 0 ) continue; foreach ( $request as $field => $value ) $params[$field] = $value; } // $GLOBALS['HTTP_RAW_POST_DATA'] used for observer ajax calls // Note: HTTP_RAW_POST_DATA must set to on in php.ini if ( $GLOBALS['HTTP_RAW_POST_DATA'] ) { $params['raw_post'] = str_replace('&_=', '', $GLOBALS['HTTP_RAW_POST_DATA']); } // unset blank vaiable set by $GLOBALS['HTTP_RAW_POST_DATA'] unset($params['_']); return ( $param_to_return ? $params[$param_to_return] : $params ); } /** * Includes the required files for a controller and the controller helpers. * * @author Nesbert Hidalgo * @access private * @param string $controller_path required */ private function include_controller($controller_path) { // include application controller $controllers = array_merge(array('application'), explode(DS, $controller_path)); $path = ''; foreach ( $controllers as $controller ) { $class = $controller . '_controller'; $controller_path = CONTROLLERS_PATH . $path . $class . '.php'; $helper_path = HELPERS_PATH . $path . $controller . '_helper.php'; try { if ( $class == '_controller' ) { $_ENV['error']->add("Looking for an 'Unknown Controller' in <strong>".str_replace('_controller'.'.php', '', $controller_path)."</strong>"); } if ( file_exists($controller_path) ) { require_once($controller_path); } else { $controller_path = str_replace($class . '.php', '', $controller_path); throw new Exception("Controller '{$class}' not found in <strong>".str_replace('_controller'.'.php', '', $controller_path)."</strong>"); } } catch ( Exception $e ) { // add to errors $_ENV['error']->add($e->getMessage(), $e); } // include helper if ( file_exists($helper_path) ) require_once($helper_path); // append to path if a nested controller $path .= str_replace('application'.DS, '', $controller.DS); } } } ?>
|