PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/application/input/cli.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip-alpes
PHP | 147 lines | 65 code | 21 blank | 61 comment | 20 complexity | 3c0a1a5aeca5ccd0434f43bf14d205eb MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT, LGPL-3.0, LGPL-2.0, JSON
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Application
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. jimport('joomla.application.input');
  11. /**
  12. * Joomla! Input CLI Class
  13. *
  14. * @package Joomla.Platform
  15. * @subpackage Application
  16. * @since 11.1
  17. */
  18. class JInputCLI extends JInput
  19. {
  20. /**
  21. * The executable that was called to run the CLI script.
  22. *
  23. * @var string
  24. * @since 11.1
  25. */
  26. public $executable;
  27. /**
  28. * The additional arguments passed to the script that are not associated
  29. * with a specific argument name.
  30. *
  31. * @var array
  32. * @since 11.1
  33. */
  34. public $args = array();
  35. /**
  36. * Constructor.
  37. *
  38. * @param array $source Source data (Optional, default is $_REQUEST)
  39. * @param array $options Array of configuration parameters (Optional)
  40. *
  41. * @since 11.1
  42. */
  43. public function __construct(array $source = null, array $options = array())
  44. {
  45. if (isset($options['filter']))
  46. {
  47. $this->filter = $options['filter'];
  48. }
  49. else
  50. {
  51. $this->filter = JFilterInput::getInstance();
  52. }
  53. // Get the command line options
  54. $this->parseArguments();
  55. // Set the options for the class.
  56. $this->options = $options;
  57. }
  58. /**
  59. * Initialise the options and arguments
  60. *
  61. * @return void
  62. *
  63. * @since 11.1
  64. */
  65. protected function parseArguments()
  66. {
  67. // Get the list of argument values from the environment.
  68. $args = $_SERVER['argv'];
  69. // Set the path used for program execution and remove it form the program arguments.
  70. $this->executable = array_shift($args);
  71. // We use a for loop because in some cases we need to look ahead.
  72. for ($i = 0; $i < count($args); $i++)
  73. {
  74. // Get the current argument to analyze.
  75. $arg = $args[$i];
  76. // First let's tackle the long argument case. eg. --foo
  77. if (strlen($arg) > 2 && substr($arg, 0, 2) == '--')
  78. {
  79. // Attempt to split the thing over equals so we can get the key/value pair if an = was used.
  80. $arg = substr($arg, 2);
  81. $parts = explode('=', $arg);
  82. $this->data[$parts[0]] = true;
  83. // Does not have an =, so let's look ahead to the next argument for the value.
  84. if (count($parts) == 1 && isset($args[$i + 1]) && preg_match('/^--?.+/', $args[$i + 1]) == 0)
  85. {
  86. $this->data[$parts[0]] = $args[$i + 1];
  87. // Since we used the next argument, increment the counter so we don't use it again.
  88. $i++;
  89. }
  90. // We have an equals sign so take the second "part" of the argument as the value.
  91. elseif (count($parts) == 2)
  92. {
  93. $this->data[$parts[0]] = $parts[1];
  94. }
  95. }
  96. // Next let's see if we are dealing with a "bunch" of short arguments. eg. -abc
  97. elseif (strlen($arg) > 2 && $arg[0] == '-')
  98. {
  99. // For each of these arguments set the value to TRUE since the flag has been set.
  100. for ($j = 1; $j < strlen($arg); $j++)
  101. {
  102. $this->data[$arg[$j]] = true;
  103. }
  104. }
  105. // OK, so it isn't a long argument or bunch of short ones, so let's look and see if it is a single
  106. // short argument. eg. -h
  107. elseif (strlen($arg) == 2 && $arg[0] == '-')
  108. {
  109. // Go ahead and set the value to TRUE and if we find a value later we'll overwrite it.
  110. $this->data[$arg[1]] = true;
  111. // Let's look ahead to see if the next argument is a "value". If it is, use it for this value.
  112. if (isset($args[$i + 1]) && preg_match('/^--?.+/', $args[$i + 1]) == 0)
  113. {
  114. $this->data[$arg[1]] = $args[$i + 1];
  115. // Since we used the next argument, increment the counter so we don't use it again.
  116. $i++;
  117. }
  118. }
  119. // Last but not least, we don't have a key/value based argument so just add it to the arguments list.
  120. else
  121. {
  122. $this->args[] = $arg;
  123. }
  124. }
  125. }
  126. }