PageRenderTime 46ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/application/cli.php

https://github.com/chalosalvador/GDS
PHP | 259 lines | 88 code | 26 blank | 145 comment | 7 complexity | 39b6a3385d3e9c290133a10b5bcaa6fa MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Application
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 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. jimport('joomla.application.applicationexception');
  11. jimport('joomla.application.input.cli');
  12. jimport('joomla.event.dispatcher');
  13. jimport('joomla.log.log');
  14. jimport('joomla.registry.registry');
  15. /**
  16. * Base class for a Joomla! command line application.
  17. *
  18. * @package Joomla.Platform
  19. * @subpackage Application
  20. * @since 11.1
  21. */
  22. class JCli
  23. {
  24. /**
  25. * @var JInput The application input object.
  26. * @since 11.1
  27. */
  28. public $input;
  29. /**
  30. * @var JRegistry The application configuration object.
  31. * @since 11.1
  32. */
  33. protected $config;
  34. /**
  35. * @var JCli The application instance.
  36. * @since 11.1
  37. */
  38. protected static $instance;
  39. /**
  40. * Class constructor.
  41. *
  42. * @return void
  43. *
  44. * @since 11.1
  45. */
  46. protected function __construct()
  47. {
  48. // Close the application if we are not executed from the command line.
  49. if (!defined('STDOUT') || !defined('STDIN') || !isset($_SERVER['argv'])) {
  50. $this->close();
  51. }
  52. // Get the command line options
  53. $this->input = new JInputCli();
  54. // Create the registry with a default namespace of config
  55. $this->config = new JRegistry();
  56. // Load the configuration object.
  57. $this->loadConfiguration($this->fetchConfigurationData());
  58. // Set the execution datetime and timestamp;
  59. $this->set('execution.datetime', gmdate('Y-m-d H:i:s'));
  60. $this->set('execution.timestamp', time());
  61. // Set the current directory.
  62. $this->set('cwd', getcwd());
  63. }
  64. /**
  65. * Returns a reference to the global JCli object, only creating it if it
  66. * doesn't already exist.
  67. *
  68. * This method must be invoked as: $cli = JCli::getInstance();
  69. *
  70. * @return JCli A JCli object
  71. *
  72. * @since 11.1
  73. */
  74. public static function & getInstance()
  75. {
  76. // Only create the object if it doesn't exist.
  77. if (empty(self::$instance)) {
  78. self::$instance = new JCli();
  79. }
  80. return self::$instance;
  81. }
  82. /**
  83. * Execute the application.
  84. *
  85. * @return void
  86. *
  87. * @since 11.1
  88. */
  89. public function execute()
  90. {
  91. $this->close();
  92. }
  93. /**
  94. * Exit the application.
  95. *
  96. * @param integer $code Exit code.
  97. *
  98. * @return void
  99. *
  100. * @since 11.1
  101. */
  102. public function close($code = 0)
  103. {
  104. exit($code);
  105. }
  106. /**
  107. * Load an object or array into the application configuration object.
  108. *
  109. * @param mixed $data Either an array or object to be loaded into the configuration object.
  110. *
  111. * @return void
  112. *
  113. * @since 11.1
  114. */
  115. public function loadConfiguration($data)
  116. {
  117. // Load the data into the configuration object.
  118. if (is_array($data)) {
  119. $this->config->loadArray($data);
  120. }
  121. elseif (is_object($data)) {
  122. $this->config->loadObject($data);
  123. }
  124. }
  125. /**
  126. * Write a string to standard output.
  127. *
  128. * @param string $text The text to display.
  129. * @param boolean $nl True to append a new line at the end of the output string.
  130. *
  131. * @return void
  132. *
  133. * @since 11.1
  134. */
  135. public function out($text = '', $nl = true)
  136. {
  137. fwrite(STDOUT, $text.($nl ? "\n" : null));
  138. }
  139. /**
  140. * Get a value from standard input.
  141. *
  142. * @return string The input string from standard input.
  143. *
  144. * @since 11.1
  145. */
  146. public function in()
  147. {
  148. return rtrim(fread(STDIN, 8192), "\n");
  149. }
  150. /**
  151. * Registers a handler to a particular event group.
  152. *
  153. * @param string $event The event name.
  154. * @param callback $handler The handler, a function or an instance of a event object.
  155. *
  156. * @return void
  157. *
  158. * @since 11.1
  159. */
  160. function registerEvent($event, $handler)
  161. {
  162. JDispatcher::getInstance()->register($event, $handler);
  163. }
  164. /**
  165. * Calls all handlers associated with an event group.
  166. *
  167. * @param string $event The event name.
  168. * @param array $args An array of arguments.
  169. *
  170. * @return array An array of results from each function call.
  171. *
  172. * @since 11.1
  173. */
  174. function triggerEvent($event, $args = null)
  175. {
  176. return JDispatcher::getInstance()->trigger($event, $args);
  177. }
  178. /**
  179. * Returns a property of the object or the default value if the property is not set.
  180. *
  181. * @param string $key The name of the property
  182. * @param mixed $default The default value if none is set.
  183. *
  184. * @return mixed The value of the configuration.
  185. *
  186. * @since 11.1
  187. */
  188. public function get($key, $default = null)
  189. {
  190. return $this->config->get($key, $default);
  191. }
  192. /**
  193. * Modifies a property of the object, creating it if it does not already exist.
  194. *
  195. * @param string $key The name of the property
  196. * @param mixed $value The value of the property to set
  197. *
  198. * @return mixed Previous value of the property
  199. *
  200. * @since 11.1
  201. */
  202. public function set($key, $value = null)
  203. {
  204. $previous = $this->config->get($key);
  205. $this->config->set($key, $value);
  206. return $previous;
  207. }
  208. /**
  209. * Method to load a PHP configuration class file based on convention and return the instantiated data object. You
  210. * will extend this method in child classes to provide configuration data from whatever data source is relevant
  211. * for your specific application.
  212. *
  213. * @return mixed Either an array or object to be loaded into the configuration object.
  214. *
  215. * @since 11.1
  216. */
  217. protected function fetchConfigurationData()
  218. {
  219. // Set the configuration file name.
  220. $file = JPATH_BASE.'/configuration.php';
  221. // Import the configuration file.
  222. if (!is_file($file)) {
  223. return false;
  224. }
  225. require_once $file;
  226. // Instantiate the configuration object.
  227. if (!class_exists('JConfig')) {
  228. return false;
  229. }
  230. $config = new JConfig();
  231. return $config;
  232. }
  233. }