/** * Метод постороение сайта * TODO: * 1. Сделать нормальный router с регистрацией, а не простую проверку на длинну * 2. Собрать факторки в кучу * 3. Наследование от handler - кажется не корректным. Понять идею иерархии * 4. Разделение на Run-Callback и Generate-Show стоит делать не фабричными методами, а либо фабрикой, либо прототипами, либо строителем. * 5. Добавить модель лингвистики */ function Build() { // Routing (Shubert 29.05.1012) $behavior = array("type" => "page", "name" => $this->settings['Pages']['default']); foreach ($this->routing["paths"] as $path->{$info}) { if ($count = preg_match($path, $_GET["path"], $matches) > 0) { if ($count > 1) { $_REQUEST['PAGE_PARAMS'] = array_shift($matches); } if (isset($info["page"])) { $behavior = array("type" => "page", "name" => $info["page"]); } else { $behavior = array("type" => "action", "name" => $info["action"]); } break; } } // <- Routing // В первую очередь выполняем "действия" if ($behavior["type"] == "action") { $action = ActionsFactory::GetAction($behavior["name"]); Dependencies::Init($action->GetDependencies(), $this->settings); $action->Run(); $action->Callback(); } else { // И только если делать нечего, то показываем страницу $page = PagesFactory::GetPage($behavior["name"]); Dependencies::Init($page->GetDependencies(), $this->settings); $page->Generate(); $page->Show(); } }
/** * Instantiates action object * * @param string $_sUrl Full action name * @return bool Returns true in case of success */ function stream_open($_sUrl) { try { $aUrl = parse_url($_sUrl); if (isset($aUrl['query'])) { $this->_aArgs = $this->_parseActionParams($aUrl['query']); } $oFactory = ActionsFactory::getInstance(); $this->_oAction = $oFactory->get($aUrl['host'] . $aUrl['path']); if (!is_a($this->_oAction, 'ru\\yukosh\\actions\\IStreamable')) { throw new ActionException('Action must implement IStreamable interface'); } return true; } catch (ActionException $oException) { trigger_error($oException->getMessage(), E_USER_WARNING); return false; } }
/** * Handles request, parses arguments, instantiate action and executes it * * @throws ActionException */ public function run() { $aArguments = []; $xActionName = ''; $bHttpCall = false; if ($this->isCommandLineLaunch()) { // Command-line call if (count($this->_aRunningArguments) > 1) { $xActionName = $this->_aRunningArguments[1]; if ($xActionName == '__email__') { // E-mail call $aResult = []; $aActions = $this->_checkEmail(); if (!empty($aActions)) { foreach ($aActions as $aCurrentAction) { $sPathAlias = ''; if (isset($aCurrentAction['registerPath'])) { ActionsFactory::registerPath($aCurrentAction['registerPath']['alias'], $aCurrentAction['registerPath']['path']); $sPathAlias = $aCurrentAction['registerPath']['alias'] . '/'; } $aArguments = array_slice($this->_aRunningArguments, 2); $aCurrentAction['args'] = array_merge($aCurrentAction['args'], $aArguments); $aResult[] = ['name' => $sPathAlias . $aCurrentAction['name'], 'args' => $aCurrentAction['args']]; } $xActionName = $aResult; } else { // No Actions to execute... return; } } else { $aArguments = array_slice($this->_aRunningArguments, 2); } } } else { // HTTP call $bHttpCall = true; $aGetParams = $this->_parseGetParams($_SERVER['QUERY_STRING']); if (isset($aGetParams['action'])) { $xActionName = $aGetParams['action']; unset($aGetParams['action']); $aArguments = $aGetParams; } } if (!empty($xActionName)) { $oFactory = ActionsFactory::getInstance(); if (is_string($xActionName)) { $xActionName = [['name' => $xActionName, 'args' => $aArguments]]; } foreach ($xActionName as $aAction) { // Fetch and execute action $oAction = $oFactory->get($aAction['name']); // Action must be "streamable" to return string result if (is_a($oAction, 'ru\\yukosh\\actions\\IStreamable')) { call_user_func_array(array($oAction, $bHttpCall ? 'httpCall' : 'call'), $aAction['args']); echo $sResult = $oAction->getResult(); } else { throw new ActionException($aAction['name'] . ' action should implement IStreamable interface'); } } } }