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 Application
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  * Base class for a Joomla! command line application.
14  *
15  * @package Joomla.Platform
16  * @subpackage Application
17  * @since 11.4
18  */
20 {
21  /**
22  * @var JRegistry The application configuration object.
23  * @since 11.1
24  */
25  protected $config;
26 
27  /**
28  * @var JApplicationCli The application instance.
29  * @since 11.1
30  */
31  protected static $instance;
32 
33  /**
34  * Class constructor.
35  *
36  * @param mixed $input An optional argument to provide dependency injection for the application's
37  * input object. If the argument is a JInputCli object that object will become
38  * the application's input object, otherwise a default input object is created.
39  * @param mixed $config An optional argument to provide dependency injection for the application's
40  * config object. If the argument is a JRegistry object that object will become
41  * the application's config object, otherwise a default config object is created.
42  * @param mixed $dispatcher An optional argument to provide dependency injection for the application's
43  * event dispatcher. If the argument is a JEventDispatcher object that object will become
44  * the application's event dispatcher, if it is null then the default event dispatcher
45  * will be created based on the application's loadDispatcher() method.
46  *
47  * @see JApplicationBase::loadDispatcher()
48  * @since 11.1
49  */
50  public function __construct(JInputCli $input = null, JRegistry $config = null, JEventDispatcher $dispatcher = null)
51  {
52  // Close the application if we are not executed from the command line.
53  // @codeCoverageIgnoreStart
54  if (!defined('STDOUT') || !defined('STDIN') || !isset($_SERVER['argv']))
55  {
56  $this->close();
57  }
58  // @codeCoverageIgnoreEnd
59 
60  // If a input object is given use it.
61  if ($input instanceof JInput)
62  {
63  $this->input = $input;
64  }
65  // Create the input based on the application logic.
66  else
67  {
68  if (class_exists('JInput'))
69  {
70  $this->input = new JInputCLI;
71  }
72  }
73 
74  // If a config object is given use it.
75  if ($config instanceof JRegistry)
76  {
77  $this->config = $config;
78  }
79  // Instantiate a new configuration object.
80  else
81  {
82  $this->config = new JRegistry;
83  }
84 
85  $this->loadDispatcher($dispatcher);
86 
87  // Load the configuration object.
88  $this->loadConfiguration($this->fetchConfigurationData());
89 
90  // Set the execution datetime and timestamp;
91  $this->set('execution.datetime', gmdate('Y-m-d H:i:s'));
92  $this->set('execution.timestamp', time());
93 
94  // Set the current directory.
95  $this->set('cwd', getcwd());
96  }
97 
98  /**
99  * Returns a property of the object or the default value if the property is not set.
100  *
101  * @param string $key The name of the property.
102  * @param mixed $default The default value (optional) if none is set.
103  *
104  * @return mixed The value of the configuration.
105  *
106  * @since 11.3
107  */
108  public function get($key, $default = null)
109  {
110  return $this->config->get($key, $default);
111  }
112 
113  /**
114  * Returns a reference to the global JApplicationCli object, only creating it if it doesn't already exist.
115  *
116  * This method must be invoked as: $cli = JApplicationCli::getInstance();
117  *
118  * @param string $name The name (optional) of the JApplicationCli class to instantiate.
119  *
120  * @return JApplicationCli
121  *
122  * @since 11.1
123  */
124  public static function getInstance($name = null)
125  {
126  // Only create the object if it doesn't exist.
127  if (empty(self::$instance))
128  {
129  if (class_exists($name) && (is_subclass_of($name, 'JApplicationCli')))
130  {
131  self::$instance = new $name;
132  }
133  else
134  {
135  self::$instance = new JApplicationCli;
136  }
137  }
138 
139  return self::$instance;
140  }
141 
142  /**
143  * Execute the application.
144  *
145  * @return void
146  *
147  * @since 11.1
148  */
149  public function execute()
150  {
151  // Trigger the onBeforeExecute event.
152  $this->triggerEvent('onBeforeExecute');
153 
154  // Perform application routines.
155  $this->doExecute();
156 
157  // Trigger the onAfterExecute event.
158  $this->triggerEvent('onAfterExecute');
159  }
160 
161  /**
162  * Load an object or array into the application configuration object.
163  *
164  * @param mixed $data Either an array or object to be loaded into the configuration object.
165  *
166  * @return JApplicationCli Instance of $this to allow chaining.
167  *
168  * @since 11.1
169  */
170  public function loadConfiguration($data)
171  {
172  // Load the data into the configuration object.
173  if (is_array($data))
174  {
175  $this->config->loadArray($data);
176  }
177  elseif (is_object($data))
178  {
179  $this->config->loadObject($data);
180  }
181 
182  return $this;
183  }
184 
185  /**
186  * Write a string to standard output.
187  *
188  * @param string $text The text to display.
189  * @param boolean $nl True (default) to append a new line at the end of the output string.
190  *
191  * @return JApplicationCli Instance of $this to allow chaining.
192  *
193  * @codeCoverageIgnore
194  * @since 11.1
195  */
196  public function out($text = '', $nl = true)
197  {
198  fwrite(STDOUT, $text . ($nl ? "\n" : null));
199 
200  return $this;
201  }
202 
203  /**
204  * Get a value from standard input.
205  *
206  * @return string The input string from standard input.
207  *
208  * @codeCoverageIgnore
209  * @since 11.1
210  */
211  public function in()
212  {
213  return rtrim(fread(STDIN, 8192), "\n");
214  }
215 
216  /**
217  * Modifies a property of the object, creating it if it does not already exist.
218  *
219  * @param string $key The name of the property.
220  * @param mixed $value The value of the property to set (optional).
221  *
222  * @return mixed Previous value of the property
223  *
224  * @since 11.3
225  */
226  public function set($key, $value = null)
227  {
228  $previous = $this->config->get($key);
229  $this->config->set($key, $value);
230 
231  return $previous;
232  }
233 
234  /**
235  * Method to load a PHP configuration class file based on convention and return the instantiated data object. You
236  * will extend this method in child classes to provide configuration data from whatever data source is relevant
237  * for your specific application.
238  *
239  * @param string $file The path and filename of the configuration file. If not provided, configuration.php
240  * in JPATH_BASE will be used.
241  * @param string $class The class name to instantiate.
242  *
243  * @return mixed Either an array or object to be loaded into the configuration object.
244  *
245  * @since 11.1
246  */
247  protected function fetchConfigurationData($file = '', $class = 'JConfig')
248  {
249  // Instantiate variables.
250  $config = array();
251 
252  if (empty($file) && defined('JPATH_BASE'))
253  {
254  $file = JPATH_BASE . '/configuration.php';
255 
256  // Applications can choose not to have any configuration data
257  // by not implementing this method and not having a config file.
258  if (!file_exists($file))
259  {
260  $file = '';
261  }
262  }
263 
264  if (!empty($file))
265  {
266  JLoader::register($class, $file);
267 
268  if (class_exists($class))
269  {
270  $config = new $class;
271  }
272  else
273  {
274  throw new RuntimeException('Configuration class does not exist.');
275  }
276  }
277 
278  return $config;
279  }
280 
281  /**
282  * Method to run the application routines. Most likely you will want to instantiate a controller
283  * and execute it, or perform some sort of task directly.
284  *
285  * @return void
286  *
287  * @codeCoverageIgnore
288  * @since 11.3
289  */
290  protected function doExecute()
291  {
292  // Your application routines go here.
293  }
294 }