/** * 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'); } } } }