00001 <?php
00002 Load::components('eventsource');
00003
00004
00005
00006
00007
00008
00009
00010 class RouterBase implements IEventSink {
00011 private $path_invoked = '';
00012
00013
00014
00015
00016 protected $controllers = array();
00017
00018
00019
00020
00021
00022
00023 protected $current_route = null;
00024
00025
00026
00027
00028 public function __construct($class_instantiater) {
00029 $potential_controllers = $class_instantiater->get_all();
00030 foreach ($potential_controllers as $controller) {
00031 if ($controller instanceof IController) {
00032 $this->controllers[] = $controller;
00033 }
00034 }
00035 EventSource::Instance()->register($this);
00036 }
00037
00038
00039
00040
00041 public function initialize($page_data) {
00042 $path = $this->get_path();
00043
00044
00045
00046 $this->path_invoked = $path;
00047 $page_data->set_path($this->path_invoked);
00048 $page_data->router = $this;
00049 }
00050
00051
00052
00053
00054
00055
00056 public function route() {
00057 $token = $this->find_route($this->path_invoked);
00058 if (empty($token)) {
00059 $token = new NotFoundRoute($this->path_invoked);
00060 }
00061 else {
00062
00063 $path_current = Url::current()->get_path();
00064 $current_is_dir = (substr($path_current, -1) === '/');
00065 $route_is_dir = $token->is_directory();
00066
00067 if ($route_is_dir && !$current_is_dir) {
00068 Url::current()->set_path($path_current . '/')->redirect(Url::PERMANENT);
00069 }
00070 else if (!$route_is_dir && $current_is_dir) {
00071 Url::current()->set_path(rtrim($path_current, '/'))->redirect(Url::PERMANENT);
00072 }
00073 }
00074 $this->current_route = $token;
00075 return $token;
00076 }
00077
00078
00079
00080
00081 public function get_route_id() {
00082 $ret = '';
00083 if ($this->current_route) {
00084 $ret = $this->current_route->identify();
00085 }
00086 return $ret;
00087 }
00088
00089
00090
00091
00092
00093
00094 public function preprocess($page_data) {
00095 foreach ($this->controllers as $controller) {
00096 $controller->preprocess($page_data);
00097 }
00098 }
00099
00100
00101
00102
00103 public function postprocess($page_data) {
00104 foreach ($this->controllers as $controller) {
00105 $controller->postprocess($page_data);
00106 }
00107 }
00108
00109
00110
00111
00112 protected function find_route($path) {
00113 if ($path === '') {
00114 return null;
00115 }
00116 $best_matching_route = null;
00117 $best_weight = IRoute::WEIGHT_NO_MATCH;
00118
00119 foreach ($this->controllers as $controller) {
00120 $routes = $controller->get_routes();
00121 foreach ($routes as $route) {
00122 $weight = $route->weight_against_path($path);
00123
00124 if ($weight < $best_weight) {
00125 $best_matching_route = $route;
00126 $best_weight = $weight;
00127 }
00128 }
00129 }
00130
00131 return $best_matching_route;
00132 }
00133
00134
00135
00136
00137
00138
00139
00140
00141 protected function get_path() {
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 $path = Url::current()->get_path();
00157 if ($path === '') {
00158 $path = '.';
00159 }
00160 return $path;
00161 }
00162
00163
00164
00165
00166 public function on_event($name, $params, &$result) {
00167 $ret = new Status();
00168 foreach ($this->controllers as $controller) {
00169 $ret->merge($controller->on_event($name, $params, $result));
00170 }
00171 return $ret;
00172 }
00173 }