/** * 构造函数,初始化日志文件目录。 */ protected function __construct() { try { $console = new Zend_Console_Getopt(array('logroot|l=s' => 'the Log Server root directory', 'logdir|d=s' => 'this log file directory', 'logfile|f=s' => 'the log files', 'delay|a=i' => 'delay timestamp', 'start|s=s' => 'start datetime', 'end|e=s' => 'end datetime')); if (!$console->getOptions()) { throw new ZtChart_Model_Monitor_Console_Exception($console->getUsageMessage()); } if (null !== ($logServerRootDirectory = $console->getOption('l'))) { foreach (array_diff(scandir($logServerRootDirectory), array('.', '..')) as $directory) { $this->_logDirectories[] = realpath($logServerRootDirectory) . DIRECTORY_SEPARATOR . $directory; } } if (null !== ($logDirectories = $console->getOption('d'))) { $this->_logDirectories += $logDirectories; } if (null !== ($logFiles = $console->getOption('f'))) { $this->_logFiles = explode(' ', $logFiles); } if (null !== ($delayTimestamp = $console->getOption('a'))) { $this->_delayTimestamp = $delayTimestamp; } if (null !== ($startTimestamp = $console->getOption('s'))) { $this->_startTimestamp = $startTimestamp; } if (null !== ($endTimestamp = $console->getOption('e'))) { $this->_endTimestamp = $endTimestamp; } } catch (Zend_Console_Getopt_Exception $e) { throw new ZtChart_Model_Monitor_Console_Exception($e->getMessage()); } $this->_console = $console; }
function __construct($process_classes_folder) { $this->Logger = \Logger::getLogger('JobLauncher'); $processes = @glob("{$process_classes_folder}/class.*Process.php"); $options = array(); if (count($processes) > 0) { foreach ($processes as $process) { $filename = basename($process); $directory = dirname($process); if (!file_exists($directory . "/" . $filename)) { throw new \Exception(sprintf("File %s does not exist.", $directory . "/" . $filename)); } include_once $directory . "/" . $filename; preg_match("/class.(.*)Process.php/s", $filename, $tmp); $process_name = $tmp[1]; if (class_exists("{$process_name}Process")) { $reflect = new \ReflectionClass("{$process_name}Process"); if ($reflect) { if ($reflect->implementsInterface('Scalr\\System\\Pcntl\\ProcessInterface')) { $options[$process_name] = $reflect->getProperty("ProcessDescription")->getValue($reflect->newInstance()); } else { throw new \Exception("Class '{$process_name}Process' doesn't implement 'ProcessInterface'.", E_ERROR); } } else { throw new \Exception("Cannot use ReflectionAPI for class '{$process_name}Process'", E_ERROR); } } else { throw new \Exception("'{$process}' does not contain definition for '{$process_name}Process'", E_ERROR); } } } else { throw new \Exception(_("No job classes found in {$process_classes_folder}"), E_ERROR); } $options["help"] = "Print this help"; $options["piddir=s"] = "PID directory"; $Getopt = new \Zend_Console_Getopt($options); try { $opts = $Getopt->getOptions(); } catch (\Zend_Console_Getopt_Exception $e) { print "{$e->getMessage()}\n\n"; die($Getopt->getUsageMessage()); } if (in_array("help", $opts) || count($opts) == 0 || !$options[$opts[0]]) { print $Getopt->getUsageMessage(); exit; } else { $this->ProcessName = $opts[0]; /* if (in_array("piddir", $opts)) { $piddir = trim($Getopt->getOption("piddir")); if (substr($piddir, 0, 1) != '/') { //$this->PIDDir = realpath($process_classes_folder . "/" . $piddir); } else { //$this->PIDDir = $piddir; } } */ } }
public function parse() { // get actionname from arguments if (count($this->_arguments) == 0) { return; } // action name will be a free floating string $actionName = array_shift($this->_arguments); // check to make sure that the action exists if (!($actionContext = $this->_buildManifest->getContext('action', $actionName)) instanceof Zend_Build_Manifest_Context) { require_once 'Zend/Tool/Cli/Context/Exception.php'; throw new Zend_Tool_Cli_Context_Exception('No action context by name ' . $actionName . ' was found in the manifest.'); } $getoptRules = array(); // get the attributes from this action context $actionContextAttrs = $actionContext->getAttributes(); foreach ($actionContextAttrs as $actionContextAttr) { if (isset($actionContextAttr['attributes']['getopt'])) { $getoptRules[$actionContextAttr['attributes']['getopt']] = $actionContextAttr['usage']; } } // parse those options out of the arguments array $getopt = new Zend_Console_Getopt($getoptRules, $this->_arguments, array('parseAll' => false)); $getopt->parse(); // put remaining args into local property $this->_arguments = $getopt->getRemainingArgs(); // get class name $actionClassName = $actionContext->getClassName(); // load appropriate file try { Zend_Loader::loadClass($actionClassName); } catch (Zend_Loader_Exception $e) { echo 'couldnt load ' . $actionClassName . PHP_EOL; } // get actual object for class name $this->_action = new $actionClassName(); // make sure its somewhat sane (implements proper interface) if (!$this->_action instanceof Zend_Build_Action_Abstract) { echo 'does not implement Zend_Build_Action_Abstract ' . PHP_EOL; } $parameters = array(); foreach ($getopt->getOptions() as $getoptParsedOption) { $parameters[$getoptParsedOption] = $getopt->getOption($getoptParsedOption); } $this->_action->setParameters($parameters); $this->_action->validate(); $this->setExecutable(); return; // everything succeeded }
public function parse() { try { $getopt = new Zend_Console_Getopt($this->_globalGetoptRules(), $this->_arguments, array('parseAll' => false)); $getopt->parse(); } catch (Exception $e) { die($e->getMessage()); } if (in_array('help', $getopt->getOptions())) { $this->setShortCircuited(true); $this->_displayHelp(); return; } $this->setExecutable(); // check if global switches are supported return; }
public function parse() { $endpointRequest = $this->_endpoint->getRequest(); if ($this->_workingArguments[0] == $_SERVER["SCRIPT_NAME"]) { array_shift($this->_workingArguments); } if (!$this->_parseGlobalPart() || count($this->_workingArguments) == 0) { // @todo process global options? return; } $actionName = array_shift($this->_workingArguments); // is the action name valid? $cliActionNameMetadatas = Zend_Tool_Rpc_Manifest_Registry::getInstance()->getMetadatas(array('type' => 'Action', 'name' => 'cliActionName')); foreach ($cliActionNameMetadatas as $cliActionNameMetadata) { if ($actionName == $cliActionNameMetadata->getValue()) { $action = $cliActionNameMetadata->getReference(); break; } } $endpointRequest->setActionName($action->getName()); /* @TODO Action Parameter Requirements */ if (count($this->_workingArguments) == 0) { return; } if (!$this->_parseActionPart() || count($this->_workingArguments) == 0) { return; } $cliProviderName = array_shift($this->_workingArguments); $cliSpecialtyName = '_global'; if (strstr($cliProviderName, '.')) { list($cliProviderName, $cliSpecialtyName) = explode('.', $cliProviderName); } $cliProviderNameMetadatas = Zend_Tool_Rpc_Manifest_Registry::getInstance()->getMetadatas(array('type' => 'Provider', 'name' => 'cliProviderName')); foreach ($cliProviderNameMetadatas as $cliProviderNameMetadata) { if ($cliProviderName == $cliProviderNameMetadata->getValue()) { $provider = $cliProviderNameMetadata->getReference(); break; } } $endpointRequest->setProviderName($provider->getName()); $cliSpecialtyNameMetadatas = Zend_Tool_Rpc_Manifest_Registry::getInstance()->getMetadatas(array('type' => 'Provider', 'providerName' => $provider->getName(), 'name' => 'cliSpecialtyNames')); foreach ($cliSpecialtyNameMetadatas as $cliSpecialtyNameMetadata) { if ($cliSpecialtyName == $cliSpecialtyNameMetadata->getValue()) { $specialtyName = $cliSpecialtyNameMetadata->getSpecialtyName(); break; } } $endpointRequest->setSpecialtyName($specialtyName); $cliActionableMethodLongParameterMetadata = Zend_Tool_Rpc_Manifest_Registry::getInstance()->getMetadata(array('type' => 'Provider', 'providerName' => $provider->getName(), 'actionName' => $action->getName(), 'specialtyName' => $specialtyName, 'name' => 'cliActionableMethodLongParameters')); $cliActionableMethodShortParameterMetadata = Zend_Tool_Rpc_Manifest_Registry::getInstance()->getMetadata(array('type' => 'Provider', 'providerName' => $provider->getName(), 'actionName' => $action->getName(), 'specialtyName' => $specialtyName, 'name' => 'cliActionableMethodShortParameters')); $cliParameterNameShortValues = $cliActionableMethodShortParameterMetadata->getValue(); $getoptOptions = array(); foreach ($cliActionableMethodLongParameterMetadata->getValue() as $parameterNameLong => $cliParameterNameLong) { $optionConfig = $cliParameterNameLong . '|'; $cliActionableMethodReferenceData = $cliActionableMethodLongParameterMetadata->getReference(); if ($cliActionableMethodReferenceData['type'] == 'string' || $cliActionableMethodReferenceData['type'] == 'bool') { $optionConfig .= $cliParameterNameShortValues[$parameterNameLong] . ($cliActionableMethodReferenceData['optional'] ? '-' : '=') . 's'; } elseif (in_array($cliActionableMethodReferenceData['type'], array('int', 'integer', 'float'))) { $optionConfig .= $cliParameterNameShortValues[$parameterNameLong] . ($cliActionableMethodReferenceData['optional'] ? '-' : '=') . 'i'; } else { $optionConfig .= $cliParameterNameShortValues[$parameterNameLong] . '-s'; } $getoptOptions[$optionConfig] = $cliActionableMethodReferenceData['description'] != '' ? $cliActionableMethodReferenceData['description'] : 'No description available.'; } $getoptParser = new Zend_Console_Getopt($getoptOptions, $this->_workingArguments, array('parseAll' => false)); $getoptParser->parse(); foreach ($getoptParser->getOptions() as $option) { $value = $getoptParser->getOption($option); $endpointRequest->setProviderParameter($option, $value); } /* Zend_Debug::dump($getoptParser); Zend_Debug::dump($endpointRequest); die(); */ $this->_workingArguments = $getoptParser->getRemainingArgs(); return; }
public function testGetoptGetOptions() { $opts = new Zend_Console_Getopt('abp:', array('-a', '-p', 'p_arg')); $this->assertEquals(implode(',', $opts->getOptions()), 'a,p'); }
* * Do not edit or add to this file if you wish to upgrade Magento to newer * versions in the future. If you wish to customize Magento for your * needs please refer to http://www.magentocommerce.com for more information. * * @copyright Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com) * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */ require __DIR__ . '/bootstrap.php'; use Magento\Tools\I18n\Code\ServiceLocator; try { $console = new \Zend_Console_Getopt(array('source|s=s' => 'Path to source dictionary file with translations', 'pack|p=s' => 'Path to language package', 'locale|l=s' => 'Target locale for dictionary, for example "de_DE"', 'mode|m-s' => 'Save mode for dictionary - "replace" - replace language pack by new one - "merge" - merge language packages, by default "replace"', 'allow_duplicates|d=s' => 'Is allowed to save duplicates of translate, by default "no"')); $console->parse(); if (!count($console->getOptions())) { throw new \UnexpectedValueException('Required parameters are missed, please see usage description' . "\n\n" . $console->getUsageMessage()); } $dictionaryPath = $console->getOption('source'); $packPath = $console->getOption('pack'); $locale = $console->getOption('locale'); $allowDuplicates = in_array($console->getOption('allow_duplicates'), array('y', 'Y', 'yes', 'Yes', '1')); $saveMode = $console->getOption('mode'); if (!$dictionaryPath) { throw new \Zend_Console_Getopt_Exception('Dictionary source path parameter is required.'); } if (!$packPath) { throw new \Zend_Console_Getopt_Exception('Pack path parameter is required.'); } if (!$locale) { throw new \Zend_Console_Getopt_Exception('Locale parameter is required.');
/** * Internal routine for parsing the provider options from the command line * * @return null */ protected function _parseProviderOptionsPart() { if (current($this->_argumentsWorking) == '?') { $this->_help = true; return; } $searchParams = array('type' => 'Tool', 'providerName' => $this->_request->getProviderName(), 'actionName' => $this->_request->getActionName(), 'specialtyName' => $this->_request->getSpecialtyName(), 'clientName' => 'console'); $actionableMethodLongParamsMetadata = $this->_manifestRepository->getMetadata(array_merge($searchParams, array('name' => 'actionableMethodLongParams'))); $actionableMethodShortParamsMetadata = $this->_manifestRepository->getMetadata(array_merge($searchParams, array('name' => 'actionableMethodShortParams'))); $paramNameShortValues = $actionableMethodShortParamsMetadata->getValue(); $getoptOptions = array(); $wordArguments = array(); $longParamCanonicalNames = array(); $actionableMethodLongParamsMetadataReference = $actionableMethodLongParamsMetadata->getReference(); foreach ($actionableMethodLongParamsMetadata->getValue() as $parameterNameLong => $consoleParameterNameLong) { $optionConfig = $consoleParameterNameLong . '|'; $parameterInfo = $actionableMethodLongParamsMetadataReference['parameterInfo'][$parameterNameLong]; // process ParameterInfo into array for command line option matching if ($parameterInfo['type'] == 'string' || $parameterInfo['type'] == 'bool') { $optionConfig .= $paramNameShortValues[$parameterNameLong] . ($parameterInfo['optional'] ? '-' : '=') . 's'; } elseif (in_array($parameterInfo['type'], array('int', 'integer', 'float'))) { $optionConfig .= $paramNameShortValues[$parameterNameLong] . ($parameterInfo['optional'] ? '-' : '=') . 'i'; } else { $optionConfig .= $paramNameShortValues[$parameterNameLong] . '-s'; } $getoptOptions[$optionConfig] = $parameterInfo['description'] != '' ? $parameterInfo['description'] : 'No description available.'; // process ParameterInfo into array for command line WORD (argument) matching $wordArguments[$parameterInfo['position']]['parameterName'] = $parameterInfo['name']; $wordArguments[$parameterInfo['position']]['optional'] = $parameterInfo['optional']; $wordArguments[$parameterInfo['position']]['type'] = $parameterInfo['type']; // keep a translation of console to canonical names $longParamCanonicalNames[$consoleParameterNameLong] = $parameterNameLong; } if (!$getoptOptions) { // no options to parse here, return return; } // if non-option arguments exist, attempt to process them before processing options $wordStack = array(); while ($wordOnTop = array_shift($this->_argumentsWorking)) { if (substr($wordOnTop, 0, 1) != '-') { array_push($wordStack, $wordOnTop); } else { // put word back on stack and move on array_unshift($this->_argumentsWorking, $wordOnTop); break; } if (count($wordStack) == count($wordArguments)) { // when we get at most the number of arguments we are expecting // then break out. break; } } if ($wordStack && $wordArguments) { for ($wordIndex = 1; $wordIndex <= count($wordArguments); $wordIndex++) { if (!array_key_exists($wordIndex - 1, $wordStack) || !array_key_exists($wordIndex, $wordArguments)) { break; } $this->_request->setProviderParameter($wordArguments[$wordIndex]['parameterName'], $wordStack[$wordIndex - 1]); unset($wordStack[$wordIndex - 1]); } } $getoptParser = new Zend_Console_Getopt($getoptOptions, $this->_argumentsWorking, array('parseAll' => false)); $getoptParser->parse(); foreach ($getoptParser->getOptions() as $option) { $value = $getoptParser->getOption($option); $providerParamOption = $longParamCanonicalNames[$option]; $this->_request->setProviderParameter($providerParamOption, $value); } $this->_argumentsWorking = $getoptParser->getRemainingArgs(); return; }
} // Check to see if Zend is in the PATH if (!stream_resolve_include_path("Zend/Loader/Autoloader.php")) { cronlog("Zend Framework is not in the current PHP Include Path. Cannot start CLI Environment", 9); exit; } require_once 'Zend/Loader/Autoloader.php'; $autoloader = Zend_Loader_Autoloader::getInstance(); $autoloader->setDefaultAutoloader(create_function('$class', "include str_replace('_', '/', \$class) . '.php';")); $application = new Zend_Application(APPLICATION_ENV, array('config' => array(APPLICATION_PATH . '/configs/application.ini'))); // Bootstrap our application $bootstrap = $application->bootstrap(); $opts = new Zend_Console_Getopt(array('help|h' => 'Displays this help', 'action|a=s' => 'Specifies the action to perform. The action must be in the form module:controller:action or controller:action', 'params|p-s' => 'Specifies any params to pass along with the quest in usual HTTP key1=val1&key2=val2&key3=val3 format.')); $opts->parse(); // Jump out when the request is for help if (isset($opts->h) || sizeof($opts->getOptions()) == 0) { echo $opts->getUsageMessage(); exit(0); } // Prepare our custom router require_once 'Zend/Controller/Router/Interface.php'; require_once 'Zend/Controller/Router/Abstract.php'; class XSync_Controller_Router_Cli extends Zend_Controller_Router_Abstract implements Zend_Controller_Router_Interface { public function assemble($userParams, $name = null, $reset = false, $encode = true) { } public function route(Zend_Controller_Request_Abstract $dispatcher) { } }