JOOMLA中国
  • Joomla中国首页
  • 社区
  • 教程
  • 应用市场
  • B计划
Joomla! Framework TM
  • Namespace
  • Class
  • Tree
  • Deprecated

Namespaces

  • Composer
    • Autoload
  • Joomla
    • Application
      • Cli
        • Output
          • Processor
      • Web
    • Data
    • DI
      • Exception
    • Event
    • Filter
    • Input
    • Ldap
    • Registry
      • Format
    • Session
      • Storage
    • String
    • Uri
    • Utilities
  • None
  • PasswordCompat
    • binary
  • PHP
  • Psr
    • Log
  • Symfony
    • Component
      • Yaml
        • Exception
    • Polyfill
      • Util

Classes

  • Cli
  • Cookie
  • Files
  • Input
  • Json
  1 <?php
  2 /**
  3  * Part of the Joomla Framework Input Package
  4  *
  5  * @copyright  Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
  6  * @license    GNU General Public License version 2 or later; see LICENSE
  7  */
  8 
  9 namespace Joomla\Input;
 10 
 11 use Joomla\Filter;
 12 
 13 /**
 14  * Joomla! Input CLI Class
 15  *
 16  * @since  1.0
 17  */
 18 class Cli extends Input
 19 {
 20     /**
 21      * The executable that was called to run the CLI script.
 22      *
 23      * @var    string
 24      * @since  1.0
 25      */
 26     public $executable;
 27 
 28     /**
 29      * The additional arguments passed to the script that are not associated
 30      * with a specific argument name.
 31      *
 32      * @var    array
 33      * @since  1.0
 34      */
 35     public $args = array();
 36 
 37     /**
 38      * Constructor.
 39      *
 40      * @param   array  $source   Source data (Optional, default is $_REQUEST)
 41      * @param   array  $options  Array of configuration parameters (Optional)
 42      *
 43      * @since   1.0
 44      */
 45     public function __construct(array $source = null, array $options = array())
 46     {
 47         if (isset($options['filter']))
 48         {
 49             $this->filter = $options['filter'];
 50         }
 51         else
 52         {
 53             $this->filter = new Filter\InputFilter;
 54         }
 55 
 56         // Get the command line options
 57         $this->parseArguments();
 58 
 59         // Set the options for the class.
 60         $this->options = $options;
 61     }
 62 
 63     /**
 64      * Method to serialize the input.
 65      *
 66      * @return  string  The serialized input.
 67      *
 68      * @since   1.0
 69      */
 70     public function serialize()
 71     {
 72         // Load all of the inputs.
 73         $this->loadAllInputs();
 74 
 75         // Remove $_ENV and $_SERVER from the inputs.
 76         $inputs = $this->inputs;
 77         unset($inputs['env']);
 78         unset($inputs['server']);
 79 
 80         // Serialize the executable, args, options, data, and inputs.
 81         return serialize(array($this->executable, $this->args, $this->options, $this->data, $inputs));
 82     }
 83 
 84     /**
 85      * Gets a value from the input data.
 86      *
 87      * @param   string  $name     Name of the value to get.
 88      * @param   mixed   $default  Default value to return if variable does not exist.
 89      * @param   string  $filter   Filter to apply to the value.
 90      *
 91      * @return  mixed  The filtered input value.
 92      *
 93      * @since   1.0
 94      */
 95     public function get($name, $default = null, $filter = 'string')
 96     {
 97         return parent::get($name, $default, $filter);
 98     }
 99 
100     /**
101      * Method to unserialize the input.
102      *
103      * @param   string  $input  The serialized input.
104      *
105      * @return  Input  The input object.
106      *
107      * @since   1.0
108      */
109     public function unserialize($input)
110     {
111         // Unserialize the executable, args, options, data, and inputs.
112         list($this->executable, $this->args, $this->options, $this->data, $this->inputs) = unserialize($input);
113 
114         // Load the filter.
115         if (isset($this->options['filter']))
116         {
117             $this->filter = $this->options['filter'];
118         }
119         else
120         {
121             $this->filter = new Filter\InputFilter;
122         }
123     }
124 
125     /**
126      * Initialise the options and arguments
127      *
128      * Not supported: -abc c-value
129      *
130      * @return  void
131      *
132      * @since   1.0
133      */
134     protected function parseArguments()
135     {
136         $argv = $_SERVER['argv'];
137 
138         $this->executable = array_shift($argv);
139 
140         $out = array();
141 
142         for ($i = 0, $j = count($argv); $i < $j; $i++)
143         {
144             $arg = $argv[$i];
145 
146             // --foo --bar=baz
147             if (substr($arg, 0, 2) === '--')
148             {
149                 $eqPos = strpos($arg, '=');
150 
151                 // --foo
152                 if ($eqPos === false)
153                 {
154                     $key = substr($arg, 2);
155 
156                     // --foo value
157                     if ($i + 1 < $j && $argv[$i + 1][0] !== '-')
158                     {
159                         $value          = $argv[$i + 1];
160                         $i++;
161                     }
162                     else
163                     {
164                         $value          = isset($out[$key]) ? $out[$key] : true;
165                     }
166                     $out[$key]          = $value;
167                 }
168 
169                 // --bar=baz
170                 else
171                 {
172                     $key                = substr($arg, 2, $eqPos - 2);
173                     $value              = substr($arg, $eqPos + 1);
174                     $out[$key]          = $value;
175                 }
176             }
177 
178             // -k=value -abc
179             else if (substr($arg, 0, 1) === '-')
180             {
181                 // -k=value
182                 if (substr($arg, 2, 1) === '=')
183                 {
184                     $key                = substr($arg, 1, 1);
185                     $value              = substr($arg, 3);
186                     $out[$key]          = $value;
187                 }
188                 // -abc
189                 else
190                 {
191                     $chars              = str_split(substr($arg, 1));
192 
193                     foreach ($chars as $char)
194                     {
195                         $key            = $char;
196                         $value          = isset($out[$key]) ? $out[$key] : true;
197                         $out[$key]      = $value;
198                     }
199 
200                     // -a a-value
201                     if ((count($chars) === 1) && ($i + 1 < $j) && ($argv[$i + 1][0] !== '-'))
202                     {
203                         $out[$key]      = $argv[$i + 1];
204                         $i++;
205                     }
206                 }
207             }
208 
209             // plain-arg
210             else
211             {
212                 $this->args[] = $arg;
213             }
214         }
215 
216         $this->data = $out;
217     }
218 }
219 
Joomla! Framework TM API documentation generated by ApiGen 2.8.0
Joomla!® and Joomla! Framework™ are trademarks of Open Source Matters, Inc. in the United States and other countries.