• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List

system/classes/controller.php

00001 <?php
00007 namespace Habari;
00008 
00015 class Controller extends Singleton
00016 {
00017   public $base_url = '';        // base url for site
00018   private $stub = '';            // stub supplied by rewriter
00019   private $action = '';          // action name (string)
00020   private $handler = null;       // the action handler object
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     /* Local scope variable caching */
00122     $controller = Controller::instance();
00123 
00124     /* Grab the base URL from the Site class */
00125     $controller->base_url = Site::get_path( 'base', true );
00126 
00127     /* If we're installed in a directory subsite, add that to the base_url */
00128 //    $controller->base_url .= Site::$config_urldir;
00129 
00130     /* Start with the entire URL coming from web server... */
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       // the query string is included in REQUEST_URI, we only need to append it if we're building the URI ourselves
00142       if ( isset( $_SERVER['QUERY_STRING'] ) && ( $_SERVER['QUERY_STRING'] != '' ) ) {
00143         $start_url .= '?' . $_SERVER['QUERY_STRING'];
00144       }
00145       
00146     }
00147     
00148     
00149 
00150     /* Strip out the base URL from the requested URL */
00151     /* but only if the base URL isn't / */
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     // undo &amp;s
00160     $start_url = str_replace( '&amp;', '&', $start_url );
00161 
00162     /* Trim off any leading or trailing slashes */
00163     $start_url = trim( $start_url, '/' );
00164 
00165     /* Allow plugins to rewrite the stub before it's passed through the rules */
00166     $start_url = Plugins::filter( 'rewrite_request', $start_url );
00167 
00168     $controller->stub = $start_url;
00169 
00170     /* Grab the URL filtering rules from DB */
00171     $matched_rule = URL::parse( $controller->stub );
00172 
00173     if ( $matched_rule === false ) {
00174       $matched_rule = URL::set_404();
00175     }
00176 
00177     /* OK, we have a matching rule.  Set the action and create a handler */
00178     $controller->action = $matched_rule->action;
00179     $handler = $matched_rule->handler;
00180     // @todo This is pretty kludgy.  If there's no namespace in the handler class, add one.
00181     if(strpos($handler, '\\') === false) {
00182       $handler = '\\Habari\\' . $handler;
00183     }
00184     $controller->handler = new $handler();
00185     /* Insert the regexed submatches as the named parameters */
00186     $controller->handler->handler_vars['entire_match'] = $matched_rule->entire_match; // The entire matched string is returned at index 0
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     /* Also, we musn't forget to add the GET and POST vars into the action's settings array */
00193     $handler_vars = new SuperGlobal( $controller->handler->handler_vars );
00194     //$handler_vars = $handler_vars->merge( $_GET, $_POST );
00195     $controller->handler->handler_vars = $handler_vars;
00196     return true;
00197   }
00198 
00202   public static function dispatch_request()
00203   {
00204     /* OK, set the wheels in motion... */
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     // Does the rule have any supplemental request types?
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 ?>

Generated on Sun Aug 4 2013 12:51:43 for Habari by  doxygen 1.7.1