/Xapp/Console.php

https://github.com/catx23/xapp-php · PHP · 313 lines · 172 code · 24 blank · 117 comment · 13 complexity · 4beb54e0ec444e791b4eba4eb9942c69 MD5 · raw file

  1. <?php
  2. /**
  3. * @version 0.1.0
  4. * @link http://www.xapp-studio.com
  5. * @author XApp-Studio.com support@xapp-studio.com
  6. * @license : GPL v2. http://www.gnu.org/licenses/gpl-2.0.html
  7. */
  8. defined('XAPP') || require_once(dirname(__FILE__) . '/../Core/core.php');
  9. xapp_import('xapp.Xapp.Error');
  10. /**
  11. * Console class
  12. *
  13. * @package Xapp
  14. * @class Xapp_Cli
  15. * @error 109
  16. * @author Frank Mueller <support@xapp-studio.com>
  17. */
  18. class Xapp_Console implements Xapp_Singleton_Interface
  19. {
  20. /**
  21. * property that contains instance of third party console class instance
  22. * like "firephp" or "chromephp" since this class acts only as a wrapper
  23. * for these console implementations
  24. *
  25. * @var ChromePhp|FirePHP|null
  26. */
  27. public $console = null;
  28. /**
  29. * contains the driver string on which this class has been instantiated
  30. * e.g. "firephp" to load the firephp external class
  31. *
  32. * @var null|string
  33. */
  34. protected $_driver = null;
  35. /**
  36. * contains the static singleton instance of this class
  37. *
  38. * @var null|Xapp_Console
  39. */
  40. protected static $_instance = null;
  41. /**
  42. * map of valid actions or log types
  43. *
  44. * @var array
  45. */
  46. protected static $_typeMap = array
  47. (
  48. 'log' => 'log',
  49. 'warn' => 'warn',
  50. 'info' => 'info',
  51. 'error' => 'error',
  52. 'dump' => 'dump',
  53. 'trace' => 'trace',
  54. 'group' => 'group',
  55. 'ungroup' => 'ungroup',
  56. 'ini' => 'ini'
  57. );
  58. /**
  59. * class constructor will include the external console class, initialize it and store
  60. * its instance as $console property. the class can not be instantiated other
  61. * then using singleton method create. the loaded console class will be init and map
  62. * with its log types to this classed log types.
  63. *
  64. * @error 10901
  65. * @param string $driver expects the driver string to load
  66. * @param array $options expects an driver dependent option array
  67. * @throws Xapp_Error
  68. */
  69. protected function __construct($driver, Array $options = array())
  70. {
  71. $this->_driver = strtolower(trim($driver));
  72. switch($this->_driver)
  73. {
  74. case 'firephp':
  75. xapp_import('firephp.firephp-core.*');
  76. if(sizeof(ob_list_handlers()) === 0)
  77. {
  78. ob_start();
  79. }
  80. $this->console = FirePHP::init();
  81. $this->console->setOptions($options);
  82. self::$_typeMap = array_merge(self::$_typeMap, array
  83. (
  84. 'log' => FirePHP::LOG,
  85. 'warn' => FirePHP::WARN,
  86. 'info' => FirePHP::INFO,
  87. 'error' => FirePHP::ERROR,
  88. 'dump' => FirePHP::DUMP,
  89. 'trace' => FirePHP::TRACE,
  90. 'group' => FirePHP::GROUP_START,
  91. 'ungroup' => FirePHP::GROUP_END,
  92. ));
  93. break;
  94. case 'chromephp':
  95. xapp_import('xapp.Ext.ChromePhp');
  96. $this->console = ChromePhp::getInstance();
  97. if(!isset($options[ChromePhp::BACKTRACE_LEVEL]))
  98. {
  99. $options[ChromePhp::BACKTRACE_LEVEL] = 2;
  100. }
  101. $this->console->addSettings($options);
  102. self::$_typeMap = array_merge(self::$_typeMap, array
  103. (
  104. 'log' => ChromePhp::LOG,
  105. 'warn' => ChromePhp::WARN,
  106. 'info' => ChromePhp::INFO,
  107. 'error' => ChromePhp::ERROR,
  108. 'dump' => ChromePhp::INFO,
  109. 'trace' => ChromePhp::INFO,
  110. 'group' => ChromePhp::GROUP,
  111. 'ungroup' => ChromePhp::GROUP_END
  112. ));
  113. break;
  114. default:
  115. throw new Xapp_Error(xapp_sprintf(_("xapp console driver: %s not supported"), $driver), 1090101);
  116. }
  117. }
  118. /**
  119. * creates singleton instance and passes all options to class constructor
  120. *
  121. * @see Xapp_Console::__construct
  122. * @error 10902
  123. * @param null|string $driver expects the driver string to load
  124. * @param array $options expects an driver dependent option array
  125. * @return null|Xapp_Console
  126. */
  127. public static function instance($driver = null, Array $options = array())
  128. {
  129. if(self::$_instance === null)
  130. {
  131. self::$_instance = new self($driver, $options);
  132. }
  133. return self::$_instance;
  134. }
  135. /**
  136. * shortcut function to log a warning to console
  137. *
  138. * @error 10903
  139. * @param null|mixed $message expects the message of any type to send to console
  140. * @param null|string $label expects the optional label to describe the first parameter
  141. * @param array $options expects optional parameters
  142. * @return void
  143. */
  144. public function warn($message = null, $label = null, Array $options = array())
  145. {
  146. $this->log($message, $label, 'warn', $options);
  147. }
  148. /**
  149. * shortcut function to log a info to console
  150. *
  151. * @error 10904
  152. * @param null|mixed $message expects the message of any type to send to console
  153. * @param null|string $label expects the optional label to describe the first parameter
  154. * @param array $options expects optional parameters
  155. * @return void
  156. */
  157. public function info($message = null, $label = null, Array $options = array())
  158. {
  159. $this->log($message, $label, 'info', $options);
  160. }
  161. /**
  162. * shortcut function to log a error to console
  163. *
  164. * @error 10905
  165. * @param null|mixed $message expects the message of any type to send to console
  166. * @param null|string $label expects the optional label to describe the first parameter
  167. * @param array $options expects optional parameters
  168. * @return void
  169. */
  170. public function error($message = null, $label = null, Array $options = array())
  171. {
  172. $this->log($message, $label, 'error', $options);
  173. }
  174. /**
  175. * shortcut function to dump an object to console
  176. *
  177. * @error 10906
  178. * @param null|mixed $message expects the message of any type to send to console
  179. * @param null|string $label expects the optional label to describe the first parameter
  180. * @param array $options expects optional parameters
  181. * @return void
  182. */
  183. public function dump($message = null, $label = null, Array $options = array())
  184. {
  185. $this->log($message, $label, 'dump', $options);
  186. }
  187. /**
  188. * shortcut function to dump all current ini values to console with optional option
  189. * to only send values for a certain extension in first parameter $message according
  190. * to phps ini_get_all sections see php help page.
  191. *
  192. * @error 10907
  193. * @param null|mixed $message expects the message of any type to send to console
  194. * @param null|string $label expects the optional label to describe the first parameter
  195. * @param array $options expects optional parameters
  196. * @return void
  197. */
  198. public function ini($message = null, $label = null, Array $options = array())
  199. {
  200. $tmp = array();
  201. if($message !== null)
  202. {
  203. $ini = @ini_get_all(strtolower((string)$message));
  204. }else{
  205. $ini = ini_get_all();
  206. }
  207. if($ini)
  208. {
  209. foreach($ini as $k => $v)
  210. {
  211. if($v['local_value'] != $v['global_value'])
  212. {
  213. $tmp[$k] = $v['local_value'];
  214. }else{
  215. $tmp[$k] = $v['global_value'];
  216. }
  217. }
  218. $this->log($tmp, 'php ini values:', 'dump');
  219. }
  220. }
  221. /**
  222. * log to console directly with this method passing only the first required parameter and to change
  223. * the log type the third parameter according to allowed log types. pass a lable for second parameter
  224. * to describe the message send to console.
  225. *
  226. * @error 10908
  227. * @param null|mixed $mixed expects the message of any type to send to console
  228. * @param null|string $label expects the optional label to describe the first parameter
  229. * @param string $type expects the log type - see log type array
  230. * @param array $options expects optional parameters
  231. * @return void
  232. * @throws Xapp_Error
  233. */
  234. public function log($mixed = null, $label = null, $type = 'info', Array $options = array())
  235. {
  236. $type = strtolower((string)$type);
  237. if(array_key_exists($type, self::$_typeMap))
  238. {
  239. if($type === 'ini')
  240. {
  241. $this->ini($mixed, $label, $options);
  242. }
  243. if($label !== null)
  244. {
  245. $label = trim(trim($label), ':') . ':';
  246. }
  247. switch($this->_driver)
  248. {
  249. case 'chromephp':
  250. switch($type)
  251. {
  252. case ($type === 'ungroup' || $mixed === null):
  253. $this->console->groupEnd();
  254. break;
  255. case 'group':
  256. $this->console->group($mixed);
  257. break;
  258. case 'trace':
  259. $this->console->log((string)$label, $mixed, 'info');
  260. break;
  261. default:
  262. $this->console->log((string)$label, $mixed, self::$_typeMap[$type]);
  263. }
  264. break;
  265. case 'firephp':
  266. switch($type)
  267. {
  268. case ($type === 'ungroup' || $mixed === null):
  269. $this->console->groupEnd();
  270. break;
  271. case 'group':
  272. $this->console->group($mixed, $options);
  273. break;
  274. case 'trace':
  275. $this->console->trace($label);
  276. break;
  277. case 'dump':
  278. $this->console->dump($label,$options);
  279. break;
  280. default:
  281. $this->console->$type($mixed, (string)$label, $options);
  282. }
  283. break;
  284. }
  285. }else{
  286. throw new Xapp_Error("xapp console log type: $type not supported", 1090801);
  287. }
  288. }
  289. }