Example #1
0
 /**
  * Parse() - This method does the work of parsing the arguments into the enpooint request,
  * this will also (during help operations) fill the response in with information as needed
  *
  * @return null
  */
 public function parse()
 {
     if ($this->_request == null || $this->_response == null) {
         throw new Exception\RuntimeException('The client registry must have both a request and response registered.');
     }
     // setup the help options
     $helpResponseOptions = array();
     // check to see if the first cli arg is the script name
     if ($this->_argumentsWorking[0] == $_SERVER['SCRIPT_NAME']) {
         array_shift($this->_argumentsWorking);
     }
     // process global options
     try {
         $this->_parseGlobalPart();
     } catch (Client\Exception $exception) {
         $this->_createHelpResponse(array('error' => $exception->getMessage()));
         return;
     }
     // ensure there are arguments left
     if (count($this->_argumentsWorking) == 0) {
         $this->_request->setDispatchable(false);
         // at this point request is not dispatchable
         // check to see if this was a help request
         if ($this->_help) {
             $this->_createHelpResponse();
         } else {
             $this->_createHelpResponse(array('error' => 'An action and provider is required.'));
         }
         return;
     }
     // process the action part of the command line
     try {
         $this->_parseActionPart();
     } catch (Client\Exception $exception) {
         $this->_request->setDispatchable(false);
         $this->_createHelpResponse(array('error' => $exception->getMessage()));
         return;
     }
     if ($this->_helpKnownAction) {
         $helpResponseOptions = array_merge($helpResponseOptions, array('actionName' => $this->_request->getActionName()));
     }
     /* @TODO Action Parameter Requirements */
     // make sure there are more "words" on the command line
     if (count($this->_argumentsWorking) == 0) {
         $this->_request->setDispatchable(false);
         // at this point request is not dispatchable
         // check to see if this is a help request
         if ($this->_help) {
             $this->_createHelpResponse($helpResponseOptions);
         } else {
             $this->_createHelpResponse(array_merge($helpResponseOptions, array('error' => 'A provider is required.')));
         }
         return;
     }
     // process the provider part of the command line
     try {
         $this->_parseProviderPart();
     } catch (Client\Exception $exception) {
         $this->_request->setDispatchable(false);
         $this->_createHelpResponse(array('error' => $exception->getMessage()));
         return;
     }
     if ($this->_helpKnownProvider) {
         $helpResponseOptions = array_merge($helpResponseOptions, array('providerName' => $this->_request->getProviderName()));
     }
     if ($this->_helpKnownSpecialty) {
         $helpResponseOptions = array_merge($helpResponseOptions, array('specialtyName' => $this->_request->getSpecialtyName()));
     }
     // if there are arguments on the command line, lets process them as provider options
     if (count($this->_argumentsWorking) != 0) {
         $this->_parseProviderOptionsPart();
     }
     // if there is still arguments lingering around, we can assume something is wrong
     if (count($this->_argumentsWorking) != 0) {
         $this->_request->setDispatchable(false);
         // at this point request is not dispatchable
         if ($this->_help) {
             $this->_createHelpResponse($helpResponseOptions);
         } else {
             $this->_createHelpResponse(array_merge($helpResponseOptions, array('error' => 'Unknown arguments left on the command line: ' . implode(' ', $this->_argumentsWorking))));
         }
         return;
     }
     // everything was processed and this is a request for help information
     if ($this->_help) {
         $this->_request->setDispatchable(false);
         // at this point request is not dispatchable
         $this->_createHelpResponse($helpResponseOptions);
     }
     return;
 }