PageRenderTime 24ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/output/Cli.php

https://github.com/mamayoleksandr/xtbackup
PHP | 287 lines | 187 code | 30 blank | 70 comment | 16 complexity | e9e25c7b0fefde82790330e71edd8a5a MD5 | raw file
  1. <?php
  2. require_once 'output/Blackhole.php';
  3. class Output_Cli extends Output_Blackhole
  4. {
  5. /**
  6. * ID value of last started job
  7. *
  8. * @var int
  9. */
  10. protected $_lastJobId = 0;
  11. /**
  12. * Level of verbosity
  13. *
  14. * @var int
  15. */
  16. protected $_verbosity;
  17. protected $_startTime = 0;
  18. /**
  19. * Constructor
  20. *
  21. * @param array $options configuration options
  22. */
  23. public function __construct($options)
  24. {
  25. // merge options with default options
  26. $options = Core_Engine::array_merge_recursive_distinct(self::getConfigOptions(CfgPart::DEFAULTS), $options);
  27. // remember configuration options
  28. $this->_options = $options;
  29. $this->_verbosity = Output_Stack::verbosityToConst($options['verbosity']); // make copy for faster access
  30. if (false===$this->_verbosity) {
  31. throw new Core_StopException(
  32. "Value of option verbosity is '{$options['verbosity']}' which is not allowed.",
  33. "cliConstruct"
  34. );
  35. }
  36. // make sure that all output is directly sent to console
  37. ob_implicit_flush();
  38. }
  39. /**
  40. * First output
  41. *
  42. * @return void
  43. */
  44. public function welcome()
  45. {
  46. $ver = "* v".Core_Engine::getVersion();
  47. $ver .= str_repeat(" ", 34-strlen($ver))."*";
  48. fputs(STDOUT, "***********************************".PHP_EOL);
  49. fputs(STDOUT, "* xtBackup *".PHP_EOL);
  50. fputs(STDOUT, $ver.PHP_EOL);
  51. fputs(STDOUT, "* sponsored by xtmotion.com, 2011 *".PHP_EOL);
  52. fputs(STDOUT, "***********************************".PHP_EOL);
  53. fputs(STDOUT, PHP_EOL);
  54. }
  55. /**
  56. * Last output
  57. *
  58. * @param $returnEx Core_StopException|bool
  59. *
  60. * @return void
  61. */
  62. public function finish($returnEx)
  63. {
  64. if (false===$returnEx) {
  65. fputs(STDOUT, "done.".PHP_EOL);
  66. } else {
  67. fputs(STDERR, "error.".PHP_EOL);
  68. }
  69. }
  70. public function showHelp($hint=false)
  71. {
  72. if ($hint) {
  73. fputs(STDOUT, PHP_EOL."$hint".PHP_EOL.PHP_EOL);
  74. }
  75. }
  76. /**
  77. * Log message with priority
  78. *
  79. * @param int $priority use Zend_Log priorities
  80. * @param string $message Message to log, multiple values supported
  81. *
  82. * @return void
  83. * @throws Zend_Log_Exception
  84. */
  85. public function log($priority, $message)
  86. {
  87. $args = func_get_args();
  88. $priority = array_shift($args);
  89. // check verbosity setting
  90. if ($this->_verbosity<$priority) {
  91. return;
  92. }
  93. // build output string
  94. $msg = array_shift($args);
  95. if (count($args)>0) {
  96. $msg = vsprintf($msg, $args);
  97. }
  98. // decorate output
  99. switch ($priority) {
  100. case Output_Stack::WARNING:
  101. $msg = "WARNING: ".$msg;
  102. break;
  103. case Output_Stack::ERROR:
  104. $msg = "ERROR: ".$msg;
  105. break;
  106. case Output_Stack::CRITICAL:
  107. $msg = "CRITICAL: ".$msg;
  108. break;
  109. }
  110. // decide about output stream and process
  111. if ($priority>=Output_Stack::WARNING) {
  112. fputs(STDOUT, $msg.PHP_EOL);
  113. } else {
  114. fputs(STDERR, $msg.PHP_EOL);
  115. }
  116. }
  117. public function logDebug()
  118. {
  119. $args = func_get_args();
  120. array_unshift($args, Output_Stack::DEBUG);
  121. call_user_func_array(array($this, 'log'), $args);
  122. }
  123. public function logError()
  124. {
  125. $args = func_get_args();
  126. array_unshift($args, Output_Stack::ERROR);
  127. call_user_func_array(array($this, 'log'), $args);
  128. }
  129. public function logCritical()
  130. {
  131. $args = func_get_args();
  132. array_unshift($args, Output_Stack::CRITICAL);
  133. call_user_func_array(array($this, 'log'), $args);
  134. }
  135. public function logNotice()
  136. {
  137. $args = func_get_args();
  138. array_unshift($args, Output_Stack::NOTICE);
  139. call_user_func_array(array($this, 'log'), $args);
  140. }
  141. public function logWarning()
  142. {
  143. $args = func_get_args();
  144. array_unshift($args, Output_Stack::WARNING);
  145. call_user_func_array(array($this, 'log'), $args);
  146. }
  147. /**
  148. * Start a new job and return pointer to it
  149. *
  150. * @param string $msg message to show
  151. * @param array $params multiple values substituted into $msg (@see vsprintf)
  152. *
  153. * @return int
  154. */
  155. public function jobStart($msg, $params=array())
  156. {
  157. // TODO has to exists in Output_Stack and maintain jobId
  158. $params = func_get_args();
  159. // init new job
  160. $job = ++$this->_lastJobId;
  161. $this->_jobs[$job] = array('count'=>0, 'step'=>1, 'level'=>count($this->_jobs));
  162. $msg = array_shift($params);
  163. if (count($params)>0) {
  164. $msg = vsprintf($msg, $params);
  165. }
  166. fputs(STDOUT, "(job $job) start: ".$msg.PHP_EOL);
  167. return $job;
  168. }
  169. /**
  170. * Signal job end
  171. *
  172. * @param int $job job id
  173. * @param string $msg message to show
  174. * @param array $params multiple values substituted into $msg (@see vsprintf)
  175. *
  176. * @return void
  177. */
  178. public function jobEnd($job, $msg, $params=array())
  179. {
  180. $params = func_get_args();
  181. $job = array_shift($params);
  182. $msg = array_shift($params);
  183. if (count($params)>0) {
  184. // substitute variables in message
  185. $msg = vsprintf($msg, $params);
  186. }
  187. // new line needed if dots were printed out
  188. $count = &$this->_jobs[$job]['count'];
  189. $step = &$this->_jobs[$job]['step'];
  190. if ($count) {
  191. if ($count % $step != 0) {
  192. // output dot for not completed batch
  193. fputs(STDOUT, ".");
  194. }
  195. fputs(STDOUT, PHP_EOL);
  196. }
  197. // show messsage
  198. fputs(STDOUT, "(job $job) end: ".$msg.PHP_EOL);
  199. // remove this job from stack
  200. unset($this->_jobs[$job]);
  201. }
  202. public function jobStep($job=null)
  203. {
  204. $count = &$this->_jobs[$job]['count'];
  205. $step = &$this->_jobs[$job]['step'];
  206. $count++;
  207. if ($count % $step == 0) {
  208. // mostly we don't want to show progress on each step
  209. fputs(STDOUT, ".");
  210. }
  211. }
  212. public function jobSetProgressStep($job, $step)
  213. {
  214. $this->_jobs[$job]['step'] = $step;
  215. }
  216. public function mark()
  217. {
  218. // TODO remove or implement elsewhere (Output_Stack ?)
  219. $mtime = microtime();
  220. $mtime = explode(" ",$mtime);
  221. $mtime = $mtime[1] + $mtime[0];
  222. $this->_startTime = $mtime;
  223. }
  224. public function time()
  225. {
  226. // TODO remove or implement elsewhere (Output_Stack ?)
  227. $mtime = microtime();
  228. $mtime = explode(" ",$mtime);
  229. $mtime = $mtime[1] + $mtime[0];
  230. $totaltime = ($mtime - $this->_startTime);
  231. return number_format($totaltime, 5);
  232. }
  233. static public function getConfigOptions($part=null)
  234. {
  235. $opt = array(
  236. CfgPart::DEFAULTS=>array(
  237. 'verbosity'=>'debug',
  238. 'progress'=>true,
  239. ),
  240. CfgPart::DESCRIPTIONS=>array(
  241. 'verbosity'=><<<TXT
  242. What level of information do you want to see in output.
  243. Possible values: CRITICAL, ERROR, WARNING, NOTICE, DEBUG',
  244. TXT
  245. ,
  246. 'progress'=>"Should progress be visually presented in output.",
  247. ),
  248. CfgPart::REQUIRED=>array()
  249. );
  250. if (is_null($part)) {
  251. return $opt;
  252. } else {
  253. return array_key_exists($part, $opt) ? $opt[$part] : array();
  254. }
  255. }
  256. }