/** * @param WebKit_HTTP_Request $request * * @return WebKit_Controller_Route */ public function route($request) { if ($this->is_not_match_for($request->urn)) { return null; } if ($route = $this->route_index($request)) { return $route; } $uri = $this->clean_url($request->urn); foreach ($this->rules as $rule) { if ($match = Core_Regexps::match_with_results($rule[0], $uri)) { $route = WebKit_Controller::Route(); foreach ($rule[1] as $k => $v) { $route[$v] = isset($match[$k + 1]) ? $match[$k + 1] : $rule[2][$v]; } $route->merge($rule[2]); break; } } return $route ? $route->merge($this->defaults)->add_controller_prefix($this->options['prefix']) : null; }
/** * @param WebKit_HTTP_Request $request * * @return WebKit_Controller_Route */ protected function route_index($request) { return $this->index_parameters && Core_Regexps::replace('{/$}', '', $this->clean_url($request->urn)) == $this->options['path'] ? WebKit_Controller::Route()->merge($this->index_parameters)->add_controller_prefix($this->options['prefix']) : null; }
/** * @param WebKit_HTTP_Request $request * * @return WebKit_Controller_Route */ public function route($request) { if ($this->is_not_match_for($request->urn)) { return null; } if ($route = $this->route_index($request)) { return $route; } $uri = Core_Regexps::replace('{^/|/$}', '', $this->clean_url($request->urn)); $uri = Core_Strings::replace($uri, '.' . ($format = $this->guess_format($uri)), ''); $current = false; $action = false; $nested_ids = array(); $parts = Core_Strings::split_by('/', $uri); $last_idx = count($parts) - 1; foreach ($parts as $idx => $part) { if (!$current) { if (isset($this->resources[$part]) && !isset($this->resources[$part]['parent'])) { $current = $part; } else { return null; } } else { if (isset($nested_ids["{$this->resources[$current]['single']}_id"])) { if (isset($this->resources[$current]['instance']) && isset($this->resources[$current]['instance'][$part]) && Core_Strings::contains($this->resources[$current]['instance'][$part], $request->method) && $idx == $last_idx) { $action = $part; break; } if (isset($this->resources[$part]) && isset($this->resources[$current]['nested']) && Core_Arrays::contains($this->resources[$current]['nested'], $part)) { $current = $part; } else { return null; } } else { if (isset($this->resources[$current]['collection']) && isset($this->resources[$current]['collection'][$part]) && Core_Strings::contains($this->resources[$current]['collection'][$part], $request->method) && $idx == $last_idx) { $action = $part; } else { $nested_ids["{$this->resources[$current]['single']}_id"] = $part; } } } } $id = isset($nested_ids["{$this->resources[$current]['single']}_id"]) ? $nested_ids["{$this->resources[$current]['single']}_id"] : false; if (!$action) { switch ($request->method_name) { case 'get': $action = $id === false ? 'index' : 'view'; break; case 'post': $action = 'create'; break; case 'put': $action = 'update'; break; case 'delete': $action = 'delete'; break; } } return WebKit_Controller::Route()->merge(array('controller' => isset($this->resources[$current]['controller']) ? $this->resources[$current]['controller'] : Core_Strings::capitalize($current) . 'Controller', 'action' => $action, 'format' => $format))->merge($id ? array($id) : array())->merge($nested_ids)->merge($this->defaults)->add_controller_prefix($this->options['prefix']); }
/** * @param WebKit_Environment $env * @param WebKit_HTTP_Response $response * * @return Iterator */ public function process_app(WS_Environment $env, $response) { $uri = $env->request->urn; $original_uri = $uri; CMS::$original_uri = $uri; CMS::$site = CMS::$defsite; if (isset(CMS::$sites[CMS::$defsite]['page_main'])) { CMS::$page_main = CMS::$sites[CMS::$defsite]['page_main']; } $_defdata = false; if (isset(CMS::$sites[CMS::$defsite])) { $_defdata = CMS::$sites[CMS::$defsite]; } if (isset(CMS::$sites)) { foreach (CMS::$sites as $site => $data) { $_host = isset($data['host']) ? trim($data['host']) : ''; $_prefix = isset($data['prefix']) ? trim($data['prefix']) : ''; if ($_host != '' || $_prefix != '') { $_bhost = false; $_bprefix = false; $_uri = $uri; if ($_prefix != '') { if ($m = Core_Regexps::match_with_results('{^/' . $_prefix . '/(.*)$}', $uri)) { $_uri = '/' . $m[1]; $_bprefix = true; } else { continue; } } if ($_host != '') { if ($env->request->host == $_host) { $_bhost = true; } else { if ($_host[0] == '{') { if (Core_Regexps::match($_host, $env->request->host)) { $_bhost = true; } else { continue; } } else { continue; } } } if ($_bprefix || $_bhost) { CMS::$site = $site; if ($_bprefix) { CMS::$site_prefix = '/' . $_prefix; } $uri = $_uri; $env->request->uri($uri); $_defdata = $data; break; } } } } if ($_defdata) { if (isset($_defdata['page_main'])) { CMS::$page_main = $_defdata['page_main']; } if (isset($_defdata['page_404'])) { CMS::$page_404 = $_defdata['page_404']; } if (isset($_defdata['layout'])) { CMS::$force_layout = $_defdata['layout']; } } if (CMS::$db) { $head = CMS::vars()->get('head'); if (isset($head['meta.title'])) { $env->meta->title($head['meta.title']); } if (isset($head['meta.description'])) { $env->meta->description($head['meta.description']); } if (isset($head['meta.keywords'])) { $env->meta->keywords($head['meta.keywords']); } } $curi = $uri; if ($m = Core_Regexps::match_with_results('/^([^\\?]+)\\?/', $curi)) { $curi = $m[1]; } $use_layout = false; // Просмотр всех мапперов зарегистрированных в системе foreach (CMS::mappers() as $name => $mapper) { // Если срабатывает маппер if ($route = $mapper->route($env->request)) { CMS::$current_mapper = $mapper; CMS::$current_component_name = $name; CMS::$current_route = $route; try { Core::load('Component.' . $name); } catch (Core_ModuleNotFoundException $e) { // hush } if ($route instanceof Net_HTTP_Response) { return $route; } // Имя подключаемого модуля $controller_module_name = 'Component.' . $name . '.Controller'; // Имя контроллера по умолчанию $controller_name = Core_Strings::replace($controller_module_name, '.', '_'); // Имя действитя по умолчанию $action_name = 'index'; $do_load_controllers = true; if ($route === true) { $route = array('controller' => $controller_name, 'action' => 'index'); } if (is_array($route)) { $_route = WebKit_Controller::Route(); $_route->merge($route); $route = $_route; } if (!isset($route['action'])) { $route['action'] = 'index'; } // Если маппер вернул нестандартное имя контроллера if (isset($route['controller'])) { $controller_name = $route['controller']; } // Если маппер вернул нестандартное имя действия if (isset($route['action'])) { $action_name = $route['action']; } // Если маппер не велел загружать модуль с конроллером (загрузит сам) if (isset($route['no_load'])) { $do_load_controllers = false; } // Загружаем модуль с контроллером if ($do_load_controllers) { if (strpos($controller_name, '.') === false && strpos($controller_name, '_') === false) { $controller_name = 'Component.' . $name . '.' . $controller_name; } if (strpos($controller_name, '.') !== false) { $controller_module_name = $controller_name; } Core::autoload($controller_module_name); } // Получаем экземпляр контроллера CMS::$current_controller_name = $controller_name; //$controller = Core_Types::reflection_for($controller_name)->newInstance($env, $response); $controller = Core::make($controller_name, $env, $response); if ($use_layout) { if (!property_exists($controller, 'layout')) { $controller->use_layout($use_layout); } else { if (!$controller->layout) { $controller->use_layout($use_layout); } } } if (!CMS::$print_version && is_string(CMS::$force_layout)) { $controller->use_layout(CMS::$force_layout); } CMS::$current_controller = $controller; CMS::$current_route = $route; // Запускаем контроллер с переданными аргументами $rc = $controller->dispatch($route); return $rc; } } if (md5($uri) == 'b0b94791138ef54aeb161e403329f827') { die('cms'); } return Net_HTTP::not_found(); }