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

/cli/CliOut.php

http://github.com/Jaymon/Mingo
PHP | 251 lines | 118 code | 81 blank | 52 comment | 15 complexity | f21fd76222aead925c766f641abba01a MD5 | raw file
  1. <?php
  2. /**
  3. * handle command line stuff
  4. *
  5. * @version 0.1
  6. * @author Jay Marcyes {@link http://marcyes.com}
  7. * @since 6-8-10
  8. * @package cli_tools
  9. ******************************************************************************/
  10. class CliOut
  11. {
  12. /**
  13. * used for indent in functions like {@link aIter()} and {@link outInfo()}
  14. * @var string
  15. */
  16. protected $indent = "\t";
  17. protected $output = null;
  18. protected $timestamp_start = 0;
  19. protected $timestamp_stop = 0;
  20. /**
  21. * default constructor
  22. *
  23. * @param mixed $output what is going to be rendered to the screen
  24. */
  25. public function __construct($output = null,$timestamp_start = 0,$timestamp_stop = 0){
  26. $this->output = $output;
  27. $this->timestamp_start = $timestamp_start;
  28. $this->timestamp_stop = $timestamp_stop;
  29. }//method
  30. public function handle(){
  31. $output = $this->output;
  32. $count = 1;
  33. ///print_r($output); exit;
  34. if(is_array($output)){
  35. $count = count($output);
  36. $row_count = 1;
  37. if(is_array($output[0]) || is_object($output[0])){
  38. // render each row with formatting...
  39. foreach($output as $row){
  40. echo sprintf('*************************** %s. row ***************************%s',$row_count,PHP_EOL);
  41. echo $this->handleArray($row);
  42. echo PHP_EOL;
  43. $row_count++;
  44. }//foreach
  45. }else{
  46. // render each row without any formatting since they aren't objects or arrays...
  47. foreach($output as $row){
  48. echo sprintf("%s.\t%s%s",$row_count,$row,PHP_EOL);
  49. $row_count++;
  50. }//foreach
  51. }//if/else
  52. }else{
  53. echo sprintf('*************************** 1. row ***************************%s',PHP_EOL);
  54. if(is_bool($output)){
  55. if(empty($output)){
  56. echo sprintf('Query Failed with: %s',$this->handleDefaultVal($output));
  57. }else{
  58. echo 'Query Ok!';
  59. }//if/else
  60. }else{
  61. echo $this->handleDefaultVal($output);
  62. }//if/else
  63. echo PHP_EOL;
  64. }//if/else
  65. echo sprintf(
  66. '%s %s in set (%s)%s%s',
  67. $count,
  68. (($count > 1) || ($count < 1)) ? 'rows' : 'row',
  69. $this->handleRounding($this->timestamp_stop,$this->timestamp_start),
  70. PHP_EOL,
  71. PHP_EOL
  72. );
  73. return true;
  74. }//method
  75. /**
  76. * prints out the array or object
  77. *
  78. * @return string the array contents in nicely formatted string form
  79. */
  80. protected function handleArray($array){
  81. // canary...
  82. if(empty($array)){ return ''; }//if
  83. $ret_val = is_object($array)
  84. ? $this->handleObject($array)
  85. : $this->aIter($array,0);
  86. return $ret_val;
  87. }//method
  88. protected function aIter($array,$deep = 0){
  89. $ret_str = sprintf('Array (%s)%s(%s',count($array),PHP_EOL,PHP_EOL);
  90. foreach($array as $key => $val){
  91. $ret_str .= sprintf("\t[%s] => ",$key);
  92. if(is_object($val)){
  93. $ret_str .= trim($this->handleIndent($this->indent,$this->handleObject($val)));
  94. }else if($val instanceof __PHP_Incomplete_Class){
  95. // this fails the is_object check, this happens when a class fails unserialize...
  96. $ret_str .= '__PHP_Incomplete_Class (class failed unserialize because it is undefined)';
  97. ///$ret_str .= sprintf('%s Incomplete Class',$val->__PHP_Incomplete_Class_Name);
  98. }else if(is_array($val)){
  99. $ret_str .= trim($this->handleIndent($this->indent,$this->aIter($val,$deep + 1)));
  100. }else{
  101. $ret_str .= $this->handleDefaultVal($val);
  102. }//if/else if/else
  103. $ret_str .= PHP_EOL;
  104. }//foreach
  105. $prefix = str_repeat($this->indent,($deep > 1) ? 1 : $deep);
  106. return trim($this->handleIndent($prefix,sprintf('%s)',$ret_str))).PHP_EOL;
  107. }//method
  108. /**
  109. * output an object
  110. *
  111. * @param object $obj the object to output, this is different than outArray() and outVar() because
  112. * it can be called from {@link aIter()}
  113. * @return string the printValue of an object
  114. */
  115. protected function handleObject($obj,$out_object = false){
  116. $ret_str = '';
  117. if(method_exists($obj,'__toString')){
  118. $ret_str = get_class($obj).'::__toString() - '.$obj;
  119. }else{
  120. if($out_object){
  121. $ret_str = print_r($obj,1);
  122. }else{
  123. $ret_str = get_class($obj).' instance';
  124. }//if/else
  125. }//if/else
  126. return $ret_str;
  127. }//method
  128. /**
  129. * indent all the lines of $val with $indent
  130. *
  131. * @param string $indent something like a tab
  132. * @param string $val the string to indent
  133. * @return string indented string
  134. */
  135. protected function handleIndent($indent,$val){
  136. return preg_replace('#^(.)#mu',$indent.'\1',$val);
  137. }//method
  138. protected function handleDefaultVal($val){
  139. $ret_str = '';
  140. if(is_null($val)){
  141. $ret_str .= 'NULL';
  142. }else if(is_bool($val)){
  143. $ret_str .= $val ? 'TRUE' : 'FALSE';
  144. }else{
  145. $ret_str .= $val;
  146. }//if/else if/else
  147. return $ret_str;
  148. }//method
  149. /**
  150. * format the time
  151. *
  152. * @param float $timestamp_stop
  153. * @param float $timestamp_start
  154. * @param integer $round
  155. * @return string
  156. */
  157. protected function handleRounding($timestamp_stop,$timestamp_start,$round = 2){
  158. $time = $timestamp_stop - $timestamp_start;
  159. // first get the response in milliseconds (http://us2.php.net/manual/en/function.microtime.php#50962)
  160. $offset = 1000;
  161. $ret_str = round(($time * $offset),$round);
  162. // if the profile took longer than 100 ms, then round to seconds...
  163. if($ret_str > 100.00){
  164. $ret_str = round(($ret_str / $offset),$round).' s';
  165. }else{
  166. $ret_str .= ' ms';
  167. }//if/else
  168. return $ret_str;
  169. }//method
  170. }//class