/libraries/joomla/application/cli.php

https://bitbucket.org/pastor399/newcastleunifc · PHP · 294 lines · 118 code · 30 blank · 146 comment · 15 complexity · d22fe52c9e4e2e90c186d2cc175f9fb5 MD5 · raw file

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