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