Exemple #1
0
 /**
  * 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');
             }
         }
     }
 }