Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 191 lines (162 sloc) 5.253 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
<?php
/**
* @package Habari
*
*/

/**
* Class which handles incoming requests and drives the
* MVC strategy for building the model and assigning to
* a view.
*
*/
class Controller extends Singleton
{
public $base_url = ''; // base url for site
private $stub = ''; // stub supplied by rewriter
private $action = ''; // action name (string)
private $handler = null; // the action handler object

/**
* Enables singleton working properly
*
* @see singleton.php
*/
protected static function instance()
{
return self::getInstanceOf( get_class() );
}

/**
* Returns the base URL
*
* @return string base URL
*/
public static function get_base_url()
{
return Controller::instance()->base_url;
}

/**
* Returns the stub in its entirety
*
* @return string the URL incoming stub
*/
public static function get_stub()
{
return Controller::instance()->stub;
}

/**
* Returns the full requested URL
*
* @return string The full requested URL
*/
public static function get_full_url()
{
return self::get_base_url() . self::get_stub();
}

/**
* Returns the action
*
* @return string name of action
*/
public static function get_action()
{
return Controller::instance()->action;
}

/**
* Returns the action handler
*
* @return ActionHandler handler object
*/
public static function get_handler()
{
return Controller::instance()->handler;
}

/**
* Returns the action handler's variables
*
* @return array variables used by handler
*/
public static function get_handler_vars()
{
return Controller::instance()->handler->handler_vars;
}

/**
* A convenience method for returning a handler variable (handler_var).
* This includes parameters set on the url, and fields submitted by POST.
* The alternative to this, while possible to write, is just too long.
* @param string $name The name of the variable to return.
* @param mixed $default A default value to return if the variable is not set.
* @return mixed The value of that variable in the handler
*/
public static function get_var( $name, $default = null )
{
return isset( Controller::instance()->handler->handler_vars[ $name ] ) ? Controller::instance()->handler->handler_vars[ $name ] : $default;
}

/**
* Parses the requested URL. Automatically
* translates URLs coming in from mod_rewrite and parses
* out any action and parameters in the slug.
*/
public static function parse_request()
{
/* Local scope variable caching */
$controller = Controller::instance();

/* Grab the base URL from the Site class */
$controller->base_url = Site::get_path( 'base', true );

/* Start with the entire URL coming from web server... */
$start_url = '';

if ( isset( $_SERVER['REQUEST_URI'] ) ) {
$start_url = $_SERVER['REQUEST_URI'];
}
else {
$start_url = $_SERVER['SCRIPT_NAME'];

if ( isset( $_SERVER['PATH_INFO'] ) ) {
$start_url .= $_SERVER['PATH_INFO'];
}

// the query string is included in REQUEST_URI, we only need to append it if we're building the URI ourselves
if ( isset( $_SERVER['QUERY_STRING'] ) && ( $_SERVER['QUERY_STRING'] != '' ) ) {
$start_url .= '?' . $_SERVER['QUERY_STRING'];
}

}



/* Strip out the base URL from the requested URL */
/* but only if the base URL isn't / */
if ( '/' != $controller->base_url ) {
$start_url = str_replace( $controller->base_url, '', $start_url );
}

// undo &amp;s
$start_url = str_replace( '&amp;', '&', $start_url );

/* Trim off any leading or trailing slashes */
$start_url = trim( $start_url, '/' );

/* Allow plugins to rewrite the stub before it's passed through the rules */
$start_url = Plugins::filter( 'rewrite_request', $start_url );

$controller->stub = $start_url;

/* Grab the URL filtering rules from DB */
$matched_rule = URL::parse( $controller->stub );

if ( $matched_rule === false ) {
$matched_rule = URL::set_404();
}

/* OK, we have a matching rule. Set the action and create a handler */
$controller->action = $matched_rule->action;
$controller->handler = new $matched_rule->handler();
/* Insert the regexed submatches as the named parameters */
$controller->handler->handler_vars['entire_match'] = $matched_rule->entire_match; // The entire matched string is returned at index 0
foreach ( $matched_rule->named_arg_values as $named_arg_key=>$named_arg_value ) {
$controller->handler->handler_vars[$named_arg_key] = $named_arg_value;
}

/* Also, we musn't forget to add the GET and POST vars into the action's settings array */
$handler_vars = new SuperGlobal( $controller->handler->handler_vars );
$handler_vars = $handler_vars->merge( $_GET, $_POST );
$controller->handler->handler_vars = $handler_vars;
return true;
}

/**
* Handle the requested action by firing off the matched handler action(s)
*/
public static function dispatch_request()
{
/* OK, set the wheels in motion... */
Plugins::act( 'handler_' . Controller::instance()->action, Controller::get_handler_vars() );
if ( method_exists( Controller::instance()->handler, 'act' ) ) {
Controller::instance()->handler->act( Controller::instance()->action );
}
}
}

?>
Something went wrong with that request. Please try again.