/AIS/Debugger/Entry.php

https://github.com/snyderp/AIS_IMAP · PHP · 343 lines · 108 code · 43 blank · 192 comment · 8 complexity · 8f46f4211f7b82bc4e2debd9937e4fe7 MD5 · raw file

  1. <?php
  2. /**
  3. * This package contains a series of files that wraps around the functionality
  4. * provided by the PHP IMAP extension, and provide an OO interface for interacting
  5. * with IMAP mail servers, their messages, and those messages' attachments.
  6. *
  7. * This package was developed by AISLabs Inc. in Chicago, IL
  8. * and written by Peter Snyder.
  9. *
  10. * PHP Version 5
  11. *
  12. * @category Mail
  13. * @package AIS_IMAP
  14. * @author Peter Snyder <psnyder@aislabs.com>
  15. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  16. * @version GIT: <git_id>
  17. * @link http://aislabs.com/
  18. * @link https://github.com/snyderp/AIS_IMAP
  19. *
  20. */
  21. /**
  22. * A class that represents debugging information. Each instance of the class
  23. * contains some information about a potentially signficant event, such as
  24. * the file that contains the code that the event occured in, the relevant line
  25. * number, and a string describing the event.
  26. *
  27. * @category PHP
  28. * @package AIS_IMAP
  29. * @author Peter Snyder <psnyder@aislabs.com>
  30. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  31. * @link https://github.com/snyderp/AIS_IMAP
  32. *
  33. */
  34. class AIS_Debugger_Entry
  35. {
  36. /**
  37. * Reference to the root debugger object that is tracking this entry
  38. *
  39. * @var AIS_Debugger
  40. * @access protected
  41. */
  42. protected $debugger;
  43. /**
  44. * The name of the class, if one exists, that generated
  45. * the debugging information
  46. *
  47. * @var null|string
  48. * @access protected
  49. */
  50. protected $class;
  51. /**
  52. * The name of the file where the code that generated the
  53. * debugging information was built from
  54. *
  55. * @var null|string
  56. * @access protected
  57. */
  58. protected $file;
  59. /**
  60. * The line number where the debugging message was fired from
  61. *
  62. * @var null|int
  63. * @access protected
  64. */
  65. protected $line;
  66. /**
  67. * The name of the method from which the debugging message was fired.
  68. * If the debugging call was made outside of a object, will
  69. * include the function name, if possible
  70. *
  71. * @var null|string
  72. * @access protected
  73. */
  74. protected $method;
  75. /**
  76. * Application provided description of whats being debugged
  77. *
  78. * @var mixed
  79. * @access protected
  80. */
  81. protected $description;
  82. /**
  83. * The time, with microsecond granularity, that the message was debugged
  84. *
  85. * @var float
  86. * @access protected
  87. */
  88. protected $time;
  89. /**
  90. * Arbitrary context passed to the debugger associated with this entry
  91. *
  92. * @var mixed
  93. * @access protected
  94. */
  95. protected $arguments;
  96. /**
  97. * Conveince constructor that allows for setting the refernece to the
  98. * root debugger object, along with an optional string description
  99. * of the event being logged, at initialization.
  100. *
  101. * @param AIS_Debugger $debugger A reference to the root debugger object.
  102. * Usually there will only be one of these
  103. * per application.
  104. * @param string $description (default: false) An optional, human
  105. * readable description of the event being
  106. * logged.
  107. *
  108. * @return void
  109. */
  110. public function __construct($debugger, $description = false)
  111. {
  112. $this->debugger = $debugger;
  113. if ($description) {
  114. $this->setDescription($description);
  115. }
  116. }
  117. /**
  118. * Return a text description of the debugging message.
  119. *
  120. * @return string A human readable description of the event that was logged
  121. */
  122. public function summary()
  123. {
  124. $debugger = $this->debugger;
  125. if ( ! $debugger) {
  126. return $this->description();
  127. } else {
  128. $output_string = '';
  129. if ($index = $this->debugger->indexOfEntry($this)) {
  130. $output_string .= $index . '. ';
  131. }
  132. if ($this->debugger->getOption('code_level')) {
  133. $output_string .= sprintf(
  134. '%s::%s.%s - ',
  135. $this->className(),
  136. $this->method(),
  137. $this->line()
  138. );
  139. }
  140. if ($this->debugger->getOption('app_level')) {
  141. $output_string .= $this->description() . ' ';
  142. }
  143. if ($this->debugger->getOption('timing')) {
  144. $output_string .= '(' . $this->time() . ')';
  145. }
  146. return $output_string;
  147. }
  148. }
  149. // =======================
  150. // ! Getters and Setters
  151. // =======================
  152. /**
  153. * Returns a string of the name of the class that logged this event.
  154. *
  155. * @return string
  156. */
  157. public function className()
  158. {
  159. return $this->class;
  160. }
  161. /**
  162. * Returns a string of the file that contains the code that logged
  163. * this event.
  164. *
  165. * @return string
  166. */
  167. public function file()
  168. {
  169. return $this->file;
  170. }
  171. /**
  172. * Returns a string of the method that logged this event.
  173. *
  174. * @return string
  175. */
  176. public function method()
  177. {
  178. return $this->method;
  179. }
  180. /**
  181. * Returns a client provided, human readable description of this event.
  182. *
  183. * @return string
  184. */
  185. public function description()
  186. {
  187. return $this->description;
  188. }
  189. /**
  190. * Returns the time, in microseconds, when this event occured.
  191. *
  192. * @return string
  193. * @see http://php.net/manual/en/function.microtime.php
  194. */
  195. public function time()
  196. {
  197. return $this->time;
  198. }
  199. /**
  200. * Returns the line of the code that generated this event.
  201. *
  202. * @return int
  203. */
  204. public function line()
  205. {
  206. return $this->line;
  207. }
  208. /**
  209. * Returns an array of additional information, if any, was provided
  210. * along with this event
  211. *
  212. * @return array|null
  213. */
  214. public function arguments()
  215. {
  216. return $this->arguments;
  217. }
  218. // ===========
  219. // ! Setters
  220. // ===========
  221. /**
  222. * Sets the debugger instance that will collect and track this event.
  223. *
  224. * @param AIS_Debugger $debugger A reference to the root debugger object.
  225. * Usually there will only be one of these per application.
  226. *
  227. * @return AIS_Debugger_Entry a refernece to the current object, to all
  228. * for method chaining
  229. */
  230. public function setDebugger($debugger)
  231. {
  232. $this->debugger = $debugger;
  233. }
  234. /**
  235. * Stores an array of additional information / arguments that give context
  236. * to the event being logged (ex passed parameters, current user informaiton
  237. * etc.)
  238. *
  239. * @param array $arguments an array of additional information / arguments
  240. *
  241. * @return AIS_Debugger_Entry a refernece to the current object, to all
  242. * for method chaining
  243. */
  244. public function setArguments($arguments)
  245. {
  246. $this->arguments = $arguments;
  247. return $this;
  248. }
  249. /**
  250. * Application provided description of whats being debugged
  251. *
  252. * @param string $description (default: false) An optional, human
  253. * readable description of the event being logged.
  254. *
  255. * @return AIS_Debugger_Entry a refernece to the current object, to all
  256. * for method chaining
  257. */
  258. public function setDescription($description)
  259. {
  260. $this->description = $description;
  261. return $this;
  262. }
  263. /**
  264. * Populate the current debug item with information from an array
  265. * returned by debug_backtrace()
  266. *
  267. * @param array $backtrace an array of backtrace information
  268. *
  269. * @return AIS_Debugger_Entry a refernece to the current object, to all
  270. * for method chaining
  271. * @see http://php.net/manual/en/function.debug-backtrace.php
  272. */
  273. public function populateWithBacktrace($backtrace)
  274. {
  275. static $property_mapping = array(
  276. 'class' => 'class',
  277. 'function' => 'method',
  278. 'line' => 'line',
  279. 'file' => 'file',
  280. );
  281. foreach ($property_mapping as $backtrace_key => $local_property) {
  282. if (isset($backtrace[$backtrace_key])) {
  283. $this->$local_property = $backtrace[$backtrace_key];
  284. }
  285. }
  286. return $this;
  287. }
  288. /**
  289. * "Stamps" the current object with the current time, with microsecond
  290. * granularity.
  291. *
  292. * @return AIS_Debugger_Entry a refernece to the current object, to all
  293. * for method chaining
  294. */
  295. public function recordTime()
  296. {
  297. $this->time = microtime(true);
  298. return $this;
  299. }
  300. }