00001 <?php
00007 namespace Habari;
00008
00015 class Controller extends Singleton
00016 {
00017 public $base_url = '';
00018 private $stub = '';
00019 private $action = '';
00020 private $handler = null;
00021
00027 protected static function instance()
00028 {
00029 return self::getInstanceOf( get_class() );
00030 }
00031
00037 public static function get_base_url()
00038 {
00039 return Controller::instance()->base_url;
00040 }
00041
00047 public static function get_stub()
00048 {
00049 return Controller::instance()->stub;
00050 }
00051
00057 public static function get_full_url()
00058 {
00059 return Utils::end_in_slash(self::get_base_url()) . self::get_stub();
00060 }
00061
00067 public static function get_action()
00068 {
00069 return Controller::instance()->action;
00070 }
00071
00077 public static function get_handler()
00078 {
00079 return Controller::instance()->handler;
00080 }
00081
00087 public static function get_handler_vars()
00088 {
00089 return Controller::instance()->handler->handler_vars;
00090 }
00091
00100 public static function get_var( $name, $default = null )
00101 {
00102 return isset( Controller::instance()->handler->handler_vars[ $name ] ) ? Controller::instance()->handler->handler_vars[ $name ] : $default;
00103 }
00104
00109 public static function get_matched_rule()
00110 {
00111 return isset( Controller::instance()->handler->handler_vars[ 'matched_rule' ] ) ? Controller::instance()->handler->handler_vars[ 'matched_rule' ] : null;
00112 }
00113
00119 public static function parse_request()
00120 {
00121
00122 $controller = Controller::instance();
00123
00124
00125 $controller->base_url = Site::get_path( 'base', true );
00126
00127
00128
00129
00130
00131 if ( isset( $_SERVER['REQUEST_URI'] ) ) {
00132 $start_url = $_SERVER['REQUEST_URI'];
00133 }
00134 else {
00135 $start_url = $_SERVER['SCRIPT_NAME'];
00136
00137 if ( isset( $_SERVER['PATH_INFO'] ) ) {
00138 $start_url .= $_SERVER['PATH_INFO'];
00139 }
00140
00141
00142 if ( isset( $_SERVER['QUERY_STRING'] ) && ( $_SERVER['QUERY_STRING'] != '' ) ) {
00143 $start_url .= '?' . $_SERVER['QUERY_STRING'];
00144 }
00145
00146 }
00147
00148
00149
00150
00151
00152 if ( '/' != $controller->base_url ) {
00153 if(substr($controller->base_url, -1) == '/') {
00154 $controller->base_url = substr($controller->base_url, 0, -1);
00155 }
00156 $start_url = preg_replace( '#^' . preg_quote($controller->base_url, '#') . '#i', '', $start_url );
00157 }
00158
00159
00160 $start_url = str_replace( '&', '&', $start_url );
00161
00162
00163 $start_url = trim( $start_url, '/' );
00164
00165
00166 $start_url = Plugins::filter( 'rewrite_request', $start_url );
00167
00168 $controller->stub = $start_url;
00169
00170
00171 $matched_rule = URL::parse( $controller->stub );
00172
00173 if ( $matched_rule === false ) {
00174 $matched_rule = URL::set_404();
00175 }
00176
00177
00178 $controller->action = $matched_rule->action;
00179 $handler = $matched_rule->handler;
00180
00181 if(strpos($handler, '\\') === false) {
00182 $handler = '\\Habari\\' . $handler;
00183 }
00184 $controller->handler = new $handler();
00185
00186 $controller->handler->handler_vars['entire_match'] = $matched_rule->entire_match;
00187 $controller->handler->handler_vars['matched_rule'] = $matched_rule;
00188 foreach ( $matched_rule->named_arg_values as $named_arg_key=>$named_arg_value ) {
00189 $controller->handler->handler_vars[$named_arg_key] = $named_arg_value;
00190 }
00191
00192
00193 $handler_vars = new SuperGlobal( $controller->handler->handler_vars );
00194
00195 $controller->handler->handler_vars = $handler_vars;
00196 return true;
00197 }
00198
00202 public static function dispatch_request()
00203 {
00204
00205 Plugins::act( 'handler_' . Controller::instance()->action, Controller::get_handler_vars() );
00206 if ( method_exists( Controller::instance()->handler, 'act' ) ) {
00207 Controller::instance()->handler->act( Controller::instance()->action );
00208 }
00209 }
00210
00215 public static function get_request_obj()
00216 {
00217 $request = new \stdClass();
00218 foreach ( URL::get_active_rules() as $rule ) {
00219 $request->{$rule->name} = false;
00220 }
00221 $matched_rule = URL::get_matched_rule();
00222 $request->{$matched_rule->name} = true;
00223
00224 if(isset($matched_rule->named_arg_values['request_types'])) {
00225 foreach($matched_rule->named_arg_values['request_types'] as $type) {
00226 $request->$type = true;
00227 }
00228 }
00229 $request = Plugins::filter('request_object', $request, $matched_rule);
00230 return $request;
00231 }
00232 }
00233
00234 ?>