[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Input/ -> Cli.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2011 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Input;
  11  
  12  use Joomla\CMS\Filter\InputFilter;
  13  
  14  // phpcs:disable PSR1.Files.SideEffects
  15  \defined('JPATH_PLATFORM') or die;
  16  // phpcs:enable PSR1.Files.SideEffects
  17  
  18  /**
  19   * Joomla! Input CLI Class
  20   *
  21   * @since       1.7.0
  22   * @deprecated  5.0  Use the `joomla/console` package instead
  23   */
  24  class Cli extends Input
  25  {
  26      /**
  27       * The executable that was called to run the CLI script.
  28       *
  29       * @var    string
  30       * @since  1.7.0
  31       * @deprecated  5.0  Use the `joomla/console` package instead
  32       */
  33      public $executable;
  34  
  35      /**
  36       * The additional arguments passed to the script that are not associated
  37       * with a specific argument name.
  38       *
  39       * @var    array
  40       * @since  1.7.0
  41       * @deprecated  5.0  Use the `joomla/console` package instead
  42       */
  43      public $args = array();
  44  
  45      /**
  46       * Constructor.
  47       *
  48       * @param   array  $source   Source data (Optional, default is $_REQUEST)
  49       * @param   array  $options  Array of configuration parameters (Optional)
  50       *
  51       * @since   1.7.0
  52       * @deprecated  5.0  Use the `joomla/console` package instead
  53       */
  54      public function __construct(array $source = null, array $options = array())
  55      {
  56          if (isset($options['filter'])) {
  57              $this->filter = $options['filter'];
  58          } else {
  59              $this->filter = InputFilter::getInstance();
  60          }
  61  
  62          // Get the command line options
  63          $this->parseArguments();
  64  
  65          // Set the options for the class.
  66          $this->options = $options;
  67      }
  68  
  69      /**
  70       * Method to serialize the input.
  71       *
  72       * @return  string  The serialized input.
  73       *
  74       * @since   3.0.0
  75       * @deprecated  5.0  Use the `joomla/console` package instead
  76       */
  77      public function serialize()
  78      {
  79          // Load all of the inputs.
  80          $this->loadAllInputs();
  81  
  82          // Remove $_ENV and $_SERVER from the inputs.
  83          $inputs = $this->inputs;
  84          unset($inputs['env']);
  85          unset($inputs['server']);
  86  
  87          // Serialize the executable, args, options, data, and inputs.
  88          return serialize(array($this->executable, $this->args, $this->options, $this->data, $inputs));
  89      }
  90  
  91      /**
  92       * Method to unserialize the input.
  93       *
  94       * @param   string  $input  The serialized input.
  95       *
  96       * @return  Input  The input object.
  97       *
  98       * @since   3.0.0
  99       * @deprecated  5.0  Use the `joomla/console` package instead
 100       */
 101      public function unserialize($input)
 102      {
 103          // Unserialize the executable, args, options, data, and inputs.
 104          list($this->executable, $this->args, $this->options, $this->data, $this->inputs) = unserialize($input);
 105  
 106          // Load the filter.
 107          if (isset($this->options['filter'])) {
 108              $this->filter = $this->options['filter'];
 109          } else {
 110              $this->filter = InputFilter::getInstance();
 111          }
 112      }
 113  
 114      /**
 115       * Initialise the options and arguments
 116       *
 117       * Not supported: -abc c-value
 118       *
 119       * @return  void
 120       *
 121       * @since   1.7.0
 122       * @deprecated  5.0  Use the `joomla/console` package instead
 123       */
 124      protected function parseArguments()
 125      {
 126          $argv = $_SERVER['argv'];
 127  
 128          $this->executable = array_shift($argv);
 129  
 130          $out = array();
 131  
 132          for ($i = 0, $j = \count($argv); $i < $j; $i++) {
 133              $arg = $argv[$i];
 134  
 135              // --foo --bar=baz
 136              if (substr($arg, 0, 2) === '--') {
 137                  $eqPos = strpos($arg, '=');
 138  
 139                  // --foo
 140                  if ($eqPos === false) {
 141                      $key = substr($arg, 2);
 142  
 143                      // --foo value
 144                      if ($i + 1 < $j && $argv[$i + 1][0] !== '-') {
 145                          $value = $argv[$i + 1];
 146                          $i++;
 147                      } else {
 148                          $value = $out[$key] ?? true;
 149                      }
 150  
 151                      $out[$key] = $value;
 152                  } else {
 153                      // --bar=baz
 154                      $key = substr($arg, 2, $eqPos - 2);
 155                      $value = substr($arg, $eqPos + 1);
 156                      $out[$key] = $value;
 157                  }
 158              } elseif (substr($arg, 0, 1) === '-') {
 159              // -k=value -abc
 160              // -k=value
 161                  if (substr($arg, 2, 1) === '=') {
 162                      $key = substr($arg, 1, 1);
 163                      $value = substr($arg, 3);
 164                      $out[$key] = $value;
 165                  } else // -abc
 166                  {
 167                      $chars = str_split(substr($arg, 1));
 168  
 169                      foreach ($chars as $char) {
 170                          $key = $char;
 171                          $value = $out[$key] ?? true;
 172                          $out[$key] = $value;
 173                      }
 174  
 175                      // -a a-value
 176                      if ((\count($chars) === 1) && ($i + 1 < $j) && ($argv[$i + 1][0] !== '-')) {
 177                          $out[$key] = $argv[$i + 1];
 178                          $i++;
 179                      }
 180                  }
 181              } else {
 182                  // Plain-arg
 183                  $this->args[] = $arg;
 184              }
 185          }
 186  
 187          $this->data = $out;
 188      }
 189  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer