PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/jplatform/joomla/application/cli.php

https://github.com/ot2sen/Molajo
PHP | 258 lines | 88 code | 26 blank | 144 comment | 7 complexity | 220a6fdfd20b73681cba7243717028d4 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. * @since 11.1
  72. */
  73. public static function & getInstance()
  74. {
  75. // Only create the object if it doesn't exist.
  76. if (empty(self::$instance)) {
  77. self::$instance = new JCli();
  78. }
  79. return self::$instance;
  80. }
  81. /**
  82. * Execute the application.
  83. *
  84. * @return void
  85. *
  86. * @since 11.1
  87. */
  88. public function execute()
  89. {
  90. $this->close();
  91. }
  92. /**
  93. * Exit the application.
  94. *
  95. * @param integer $code Exit code.
  96. *
  97. * @return void
  98. *
  99. * @since 11.1
  100. */
  101. public function close($code = 0)
  102. {
  103. exit($code);
  104. }
  105. /**
  106. * Load an object or array into the application configuration object.
  107. *
  108. * @param mixed $data Either an array or object to be loaded into the configuration object.
  109. *
  110. * @return void
  111. *
  112. * @since 11.1
  113. */
  114. public function loadConfiguration($data)
  115. {
  116. // Load the data into the configuration object.
  117. if (is_array($data)) {
  118. $this->config->loadArray($data);
  119. }
  120. elseif (is_object($data)) {
  121. $this->config->loadObject($data);
  122. }
  123. }
  124. /**
  125. * Write a string to standard output.
  126. *
  127. * @param string $text The text to display.
  128. * @param boolean $nl True to append a new line at the end of the output string.
  129. *
  130. * @return void
  131. *
  132. * @since 11.1
  133. */
  134. public function out($text = '', $nl = true)
  135. {
  136. fwrite(STDOUT, $text.($nl ? "\n" : null));
  137. }
  138. /**
  139. * Get a value from standard input.
  140. *
  141. * @return string The input string from standard input.
  142. *
  143. * @since 11.1
  144. */
  145. public function in()
  146. {
  147. return rtrim(fread(STDIN, 8192), "\n");
  148. }
  149. /**
  150. * Registers a handler to a particular event group.
  151. *
  152. * @param string $event The event name.
  153. * @param callback $handler The handler, a function or an instance of a event object.
  154. *
  155. * @return void
  156. *
  157. * @since 11.1
  158. */
  159. function registerEvent($event, $handler)
  160. {
  161. JDispatcher::getInstance()->register($event, $handler);
  162. }
  163. /**
  164. * Calls all handlers associated with an event group.
  165. *
  166. * @param string $event The event name.
  167. * @param array $args An array of arguments.
  168. *
  169. * @return array An array of results from each function call.
  170. *
  171. * @since 11.1
  172. */
  173. function triggerEvent($event, $args = null)
  174. {
  175. return JDispatcher::getInstance()->trigger($event, $args);
  176. }
  177. /**
  178. * Returns a property of the object or the default value if the property is not set.
  179. *
  180. * @param string $key The name of the property
  181. * @param mixed $default The default value if none is set.
  182. *
  183. * @return mixed The value of the configuration.
  184. *
  185. * @since 11.1
  186. */
  187. public function get($key, $default = null)
  188. {
  189. return $this->config->get($key, $default);
  190. }
  191. /**
  192. * Modifies a property of the object, creating it if it does not already exist.
  193. *
  194. * @param string $key The name of the property
  195. * @param mixed $value The value of the property to set
  196. *
  197. * @return mixed Previous value of the property
  198. *
  199. * @since 11.1
  200. */
  201. public function set($key, $value = null)
  202. {
  203. $previous = $this->config->get($key);
  204. $this->config->set($key, $value);
  205. return $previous;
  206. }
  207. /**
  208. * Method to load a PHP configuration class file based on convention and return the instantiated data object. You
  209. * will extend this method in child classes to provide configuration data from whatever data source is relevant
  210. * for your specific application.
  211. *
  212. * @return mixed Either an array or object to be loaded into the configuration object.
  213. *
  214. * @since 11.1
  215. */
  216. protected function fetchConfigurationData()
  217. {
  218. // Set the configuration file name.
  219. $file = JPATH_BASE.'/configuration.php';
  220. // Import the configuration file.
  221. if (!is_file($file)) {
  222. return false;
  223. }
  224. require_once $file;
  225. // Instantiate the configuration object.
  226. if (!class_exists('JConfig')) {
  227. return false;
  228. }
  229. $config = new JConfig();
  230. return $config;
  231. }
  232. }