PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/vendor/symfony/lib/command/sfCommandManager.class.php

https://github.com/IDCI-Consulting/WebsiteEval
PHP | 381 lines | 244 code | 40 blank | 97 comment | 43 complexity | 3e3d6de47eebcb0f5b74be027f6fddb1 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Class to manage command line arguments and options.
  11. *
  12. * @package symfony
  13. * @subpackage command
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfCommandManager.class.php 21908 2009-09-11 12:06:21Z fabien $
  16. */
  17. class sfCommandManager
  18. {
  19. protected
  20. $arguments = '',
  21. $errors = array(),
  22. $optionSet = null,
  23. $argumentSet = array(),
  24. $optionValues = array(),
  25. $argumentValues = array(),
  26. $parsedArgumentValues = array();
  27. /**
  28. * Constructor.
  29. *
  30. * @param sfCommandArgumentSet $argumentSet A sfCommandArgumentSet object
  31. * @param sfCommandOptionSet $optionSet A setOptionSet object
  32. */
  33. public function __construct(sfCommandArgumentSet $argumentSet = null, sfCommandOptionSet $optionSet = null)
  34. {
  35. if (null === $argumentSet)
  36. {
  37. $argumentSet = new sfCommandArgumentSet();
  38. }
  39. $this->setArgumentSet($argumentSet);
  40. if (null === $optionSet)
  41. {
  42. $optionSet = new sfCommandOptionSet();
  43. }
  44. $this->setOptionSet($optionSet);
  45. }
  46. /**
  47. * Sets the argument set.
  48. *
  49. * @param sfCommandArgumentSet $argumentSet A sfCommandArgumentSet object
  50. */
  51. public function setArgumentSet(sfCommandArgumentSet $argumentSet)
  52. {
  53. $this->argumentSet = $argumentSet;
  54. }
  55. /**
  56. * Gets the argument set.
  57. *
  58. * @return sfCommandArgumentSet A sfCommandArgumentSet object
  59. */
  60. public function getArgumentSet()
  61. {
  62. return $this->argumentSet;
  63. }
  64. /**
  65. * Sets the option set.
  66. *
  67. * @param sfCommandOptionSet $optionSet A sfCommandOptionSet object
  68. */
  69. public function setOptionSet(sfCommandOptionSet $optionSet)
  70. {
  71. $this->optionSet = $optionSet;
  72. }
  73. /**
  74. * Gets the option set.
  75. *
  76. * @return sfCommandOptionSet A sfCommandOptionSet object
  77. */
  78. public function getOptionSet()
  79. {
  80. return $this->optionSet;
  81. }
  82. /**
  83. * Processes command line arguments.
  84. *
  85. * @param mixed $arguments A string or an array of command line parameters
  86. */
  87. public function process($arguments = null)
  88. {
  89. if (null === $arguments)
  90. {
  91. $arguments = $_SERVER['argv'];
  92. // we strip command line program
  93. if (isset($arguments[0]) && '-' != $arguments[0][0])
  94. {
  95. array_shift($arguments);
  96. }
  97. }
  98. else if (!is_array($arguments))
  99. {
  100. // hack to split arguments with spaces : --test="with some spaces"
  101. $arguments = preg_replace('/(\'|")(.+?)\\1/e', "str_replace(' ', '=PLACEHOLDER=', '\\2')", $arguments);
  102. $arguments = preg_split('/\s+/', $arguments);
  103. $arguments = str_replace('=PLACEHOLDER=', ' ', $arguments);
  104. }
  105. $this->arguments = $arguments;
  106. $this->optionValues = $this->optionSet->getDefaults();
  107. $this->argumentValues = $this->argumentSet->getDefaults();
  108. $this->parsedArgumentValues = array();
  109. $this->errors = array();
  110. while (!in_array($argument = array_shift($this->arguments), array('', null)))
  111. {
  112. if ('--' == $argument)
  113. {
  114. // stop options parsing
  115. $this->parsedArgumentValues = array_merge($this->parsedArgumentValues, $this->arguments);
  116. break;
  117. }
  118. if ('--' == substr($argument, 0, 2))
  119. {
  120. $this->parseLongOption(substr($argument, 2));
  121. }
  122. else if ('-' == $argument[0])
  123. {
  124. $this->parseShortOption(substr($argument, 1));
  125. }
  126. else
  127. {
  128. $this->parsedArgumentValues[] = $argument;
  129. }
  130. }
  131. $position = 0;
  132. foreach ($this->argumentSet->getArguments() as $argument)
  133. {
  134. if (array_key_exists($position, $this->parsedArgumentValues))
  135. {
  136. if ($argument->isArray())
  137. {
  138. $this->argumentValues[$argument->getName()] = array_slice($this->parsedArgumentValues, $position);
  139. break;
  140. }
  141. else
  142. {
  143. $this->argumentValues[$argument->getName()] = $this->parsedArgumentValues[$position];
  144. }
  145. }
  146. ++$position;
  147. }
  148. $this->arguments = $arguments;
  149. if (count($this->parsedArgumentValues) < $this->argumentSet->getArgumentRequiredCount())
  150. {
  151. $this->errors[] = 'Not enough arguments.';
  152. }
  153. else if (count($this->parsedArgumentValues) > $this->argumentSet->getArgumentCount())
  154. {
  155. $this->errors[] = sprintf('Too many arguments ("%s" given).', implode(' ', $this->parsedArgumentValues));
  156. }
  157. }
  158. /**
  159. * Returns true if the current command line options validate the argument and option sets.
  160. *
  161. * @return true if there are some validation errors, false otherwise
  162. */
  163. public function isValid()
  164. {
  165. return count($this->errors) ? false : true;
  166. }
  167. /**
  168. * Gets the current errors.
  169. *
  170. * @return array An array of errors
  171. */
  172. public function getErrors()
  173. {
  174. return $this->errors;
  175. }
  176. /**
  177. * Returns the argument values.
  178. *
  179. * @return array An array of argument values
  180. */
  181. public function getArgumentValues()
  182. {
  183. return $this->argumentValues;
  184. }
  185. /**
  186. * Returns the argument value for a given argument name.
  187. *
  188. * @param string $name The argument name
  189. *
  190. * @return mixed The argument value
  191. */
  192. public function getArgumentValue($name)
  193. {
  194. if (!$this->argumentSet->hasArgument($name))
  195. {
  196. throw new sfCommandException(sprintf('The "%s" argument does not exist.', $name));
  197. }
  198. return $this->argumentValues[$name];
  199. }
  200. /**
  201. * Returns the options values.
  202. *
  203. * @return array An array of option values
  204. */
  205. public function getOptionValues()
  206. {
  207. return $this->optionValues;
  208. }
  209. /**
  210. * Returns the option value for a given option name.
  211. *
  212. * @param string $name The option name
  213. *
  214. * @return mixed The option value
  215. */
  216. public function getOptionValue($name)
  217. {
  218. if (!$this->optionSet->hasOption($name))
  219. {
  220. throw new sfCommandException(sprintf('The "%s" option does not exist.', $name));
  221. }
  222. return $this->optionValues[$name];
  223. }
  224. /**
  225. * Parses a short option.
  226. *
  227. * @param string $argument The option argument
  228. */
  229. protected function parseShortOption($argument)
  230. {
  231. // short option can be aggregated like in -vd (== -v -d)
  232. for ($i = 0, $count = strlen($argument); $i < $count; $i++)
  233. {
  234. $shortcut = $argument[$i];
  235. $value = true;
  236. if (!$this->optionSet->hasShortcut($shortcut))
  237. {
  238. $this->errors[] = sprintf('The option "-%s" does not exist.', $shortcut);
  239. continue;
  240. }
  241. $option = $this->optionSet->getOptionForShortcut($shortcut);
  242. // required argument?
  243. if ($option->isParameterRequired())
  244. {
  245. if ($i + 1 < strlen($argument))
  246. {
  247. $value = substr($argument, $i + 1);
  248. $this->setOption($option, $value);
  249. break;
  250. }
  251. else
  252. {
  253. // take next element as argument (if it doesn't start with a -)
  254. if (count($this->arguments) && $this->arguments[0][0] != '-')
  255. {
  256. $value = array_shift($this->arguments);
  257. $this->setOption($option, $value);
  258. break;
  259. }
  260. else
  261. {
  262. $this->errors[] = sprintf('Option "-%s" requires an argument', $shortcut);
  263. $value = null;
  264. }
  265. }
  266. }
  267. else if ($option->isParameterOptional())
  268. {
  269. if (substr($argument, $i + 1) != '')
  270. {
  271. $value = substr($argument, $i + 1);
  272. }
  273. else
  274. {
  275. // take next element as argument (if it doesn't start with a -)
  276. if (count($this->arguments) && $this->arguments[0][0] != '-')
  277. {
  278. $value = array_shift($this->arguments);
  279. }
  280. else
  281. {
  282. $value = $option->getDefault();
  283. }
  284. }
  285. $this->setOption($option, $value);
  286. break;
  287. }
  288. $this->setOption($option, $value);
  289. }
  290. }
  291. /**
  292. * Parses a long option.
  293. *
  294. * @param string $argument The option argument
  295. */
  296. protected function parseLongOption($argument)
  297. {
  298. if (false !== strpos($argument, '='))
  299. {
  300. list($name, $value) = explode('=', $argument, 2);
  301. if (!$this->optionSet->hasOption($name))
  302. {
  303. $this->errors[] = sprintf('The "--%s" option does not exist.', $name);
  304. return;
  305. }
  306. $option = $this->optionSet->getOption($name);
  307. if (!$option->acceptParameter())
  308. {
  309. $this->errors[] = sprintf('Option "--%s" does not take an argument.', $name);
  310. $value = true;
  311. }
  312. }
  313. else
  314. {
  315. $name = $argument;
  316. if (!$this->optionSet->hasOption($name))
  317. {
  318. $this->errors[] = sprintf('The "--%s" option does not exist.', $name);
  319. return;
  320. }
  321. $option = $this->optionSet->getOption($name);
  322. if ($option->isParameterRequired())
  323. {
  324. $this->errors[] = sprintf('Option "--%s" requires an argument.', $name);
  325. }
  326. $value = $option->acceptParameter() ? $option->getDefault() : true;
  327. }
  328. $this->setOption($option, $value);
  329. }
  330. public function setOption(sfCommandOption $option, $value)
  331. {
  332. if ($option->isArray())
  333. {
  334. $this->optionValues[$option->getName()][] = $value;
  335. }
  336. else
  337. {
  338. $this->optionValues[$option->getName()] = $value;
  339. }
  340. }
  341. }