/** * load the configuration of jelix-scripts * @param string $appname the application name * @return Jelix\DevHelper\CommandConfig */ static function loadConfig($appname = '') { $config = new CommandConfig(); if ($appname === '') { $appname = $config->loadFromProject(); } else { if ($appname === false) { // don't load from project.. $appname = ''; } } // try to find a .jelix-scripts.ini in the current directory or parent directories $dir = getcwd(); $found = false; do { if (file_exists($dir . DIRECTORY_SEPARATOR . '.jelix-scripts.ini')) { $config->loadFromIni($dir . DIRECTORY_SEPARATOR . '.jelix-scripts.ini', $appname); $found = true; } else { if (file_exists($dir . DIRECTORY_SEPARATOR . 'jelix-scripts.ini')) { $config->loadFromIni($dir . DIRECTORY_SEPARATOR . 'jelix-scripts.ini', $appname); // windows users don't often use dot files. $found = true; } } $previousdir = $dir; $dir = dirname($dir); } while ($dir != '.' && $dir != $previousdir && !$found); // we didn't find a .jelix-scripts, try to read one from the home directory if (!$found) { $home = ''; if (isset($_SERVER['HOME'])) { $home = $_SERVER['HOME']; } else { if (isset($_ENV['HOME'])) { $home = $_ENV['HOME']; } else { if (isset($_SERVER['USERPROFILE'])) { // windows $home = $_SERVER['USERPROFILE']; } else { if (isset($_SERVER['HOMEDRIVE']) && isset($_SERVER['HOMEPATH'])) { // windows $home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH']; } } } } if ($home) { if (file_exists($home . DIRECTORY_SEPARATOR . '.jelix-scripts.ini')) { $config->loadFromIni($home . DIRECTORY_SEPARATOR . '.jelix-scripts.ini', $appname); } else { $config->loadFromIni($home . DIRECTORY_SEPARATOR . 'jelix-scripts.ini', $appname); } // windows users don't often use dot files. } } self::$debugMode = $config->debugMode; if (function_exists('date_default_timezone_set')) { date_default_timezone_set($config->infoTimezone); } $config->appName = $appname; return $config; }
/** * @param string $subcommandName * @param string[] $options * @param CommandConfig $commandConfig * @param bool $hasMagicOption * @return void */ private function checkOptions($subcommandName, $options, $commandConfig, $hasMagicOption) { $optionConfigs = $commandConfig->getOptionConfigs($subcommandName); foreach ($optionConfigs as $optionConfig) { if ($optionConfig->isRequired()) { $name = $optionConfig->getName(); if (isset($options[$name])) { continue; } if ($hasMagicOption === false) { $message = "Option '{$name}' is required."; throw new CommandParsingException($message, $subcommandName); } } } foreach ($options as $name => $value) { $optionConfig = $commandConfig->getOptionConfig($name, $subcommandName); $argumentConfig = $optionConfig->getArgumentConfig(); if ($argumentConfig !== null) { $values = $argumentConfig->getValues(); if ($values !== null) { if (in_array($value, $values, true) === false) { $message = "The value of option '{$name}' is invalid."; throw new CommandParsingException($message, $subcommandName); } } } } $mutuallyExclusiveOptionGroupConfigs = $commandConfig->getMutuallyExclusiveOptionGroupConfigs($subcommandName); if ($mutuallyExclusiveOptionGroupConfigs !== null) { foreach ($mutuallyExclusiveOptionGroupConfigs as $groupConfig) { $optionName = null; $optionNames = []; foreach ($groupConfig->getOptionConfigs() as $optionConfig) { $name = $optionConfig->getName(); if (isset($options[$name])) { if ($optionName !== null && $optionName !== $name) { $message = "The option '{$optionName}' and '{$name}'" . " are mutually exclusive."; throw new CommandParsingException($message, $subcommandName); } $optionName = $name; } $optionNames[] = "'" . $name . "'"; } if ($groupConfig->isRequired() && $optionName === null) { if ($hasMagicOption === false && count($optionNames) !== 0) { $message = "One of option " . implode(' or ', $optionNames) . " is required."; throw new CommandParsingException($message, $subcommandName); } } } } }
/** * {@inheritdoc} */ protected function getDefaultLenientArgsParsing() { return $this->parentConfig ? $this->parentConfig->isLenientArgsParsingEnabled() : parent::getDefaultLenientArgsParsing(); }