/protected/components/ezcomponents/Debug/src/interfaces/stacktrace_iterator.php

https://github.com/kamarulismail/kamarul-playground · PHP · 250 lines · 75 code · 17 blank · 158 comment · 1 complexity · 1903fbfcc4d3a53bbb282af4b81152d7 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the ezcDebug class.
  4. *
  5. * @package Debug
  6. * @version 1.2.1
  7. * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
  8. * @license http://ez.no/licenses/new_bsd New BSD License
  9. */
  10. /**
  11. * Base iterator class to wrap stack traces.
  12. *
  13. * This class provides a basis for stack trace iterators that are stored for
  14. * each call to {@link ezcDebug::log()} if {@link ezcDebugOptions::$stackTrace}
  15. * is switched on or the specific parameter is set. The stack trace iterator
  16. * needs to ensure, that the created stack trace is converted to the format
  17. * understandable by the {@link ezcDebugFormatter} as defined in {@link
  18. * ezcDebugStacktraceIterator::unifyStackElement()}.
  19. *
  20. * @package Debug
  21. * @version 1.2.1
  22. */
  23. abstract class ezcDebugStacktraceIterator implements Iterator, ArrayAccess, Countable
  24. {
  25. /**
  26. * Raw stack trace as an array.
  27. *
  28. * @var array
  29. */
  30. private $stackTrace;
  31. /**
  32. * Options.
  33. *
  34. * @var ezcDebugOptions
  35. */
  36. protected $options;
  37. /**
  38. * Creates a new stack trace iterator.
  39. *
  40. * Calls {@link ezcDebugStacktraceIterator::prepare()} internally to
  41. * prepare the stack trace before storing it.
  42. *
  43. * @param mixed $stackTrace
  44. * @param int $removeElements
  45. * @param ezcDebugOptions $options
  46. * @return void
  47. */
  48. public final function __construct( $stackTrace, $removeElements = 2, ezcDebugOptions $options )
  49. {
  50. $this->options = $options;
  51. $this->stackTrace = $this->prepare( $stackTrace, $removeElements );
  52. }
  53. /**
  54. * Unifies a stack element for being returned to the formatter.
  55. *
  56. * This method ensures that an element of the stack trace conforms to the
  57. * format expected by a {@link ezcDebugOutputFormatter}. The format is
  58. * defined as follows:
  59. *
  60. * <code>
  61. * array(
  62. * 'file' => '<fullpathtofile>',
  63. * 'line' => <lineno>,
  64. * 'function' => '<functionname>',
  65. * 'class' => '<classname>',
  66. * 'params' => array(
  67. * <param_no> => '<paramvalueinfo>',
  68. * <param_no> => '<paramvalueinfo>',
  69. * <param_no> => '<paramvalueinfo>',
  70. * ...
  71. * )
  72. * )
  73. * </code>
  74. *
  75. * @param mixed $stackElement
  76. * @return array As described above.
  77. */
  78. protected abstract function unifyStackElement( $stackElement );
  79. /**
  80. * Prepares the stack trace for being stored in the iterator instance.
  81. *
  82. * This method is called by {@link
  83. * ezcDebugStacktraceIterator::__construct()} before the stack trace is
  84. * stored in the corresponding property. The given array can be manipulated
  85. * as needed to prepare the trace and the array to store internally must be
  86. * returned. The basic implementation removes $removeElements number of
  87. * elements from the start of the trace array and reduces the array to
  88. * {@link ezcDebugOptions::$stackTraceDepth} elements.
  89. *
  90. * @param array $stackTrace
  91. * @param int $removeElements
  92. */
  93. protected function prepare( $stackTrace, $removeElements )
  94. {
  95. return array_slice(
  96. $stackTrace,
  97. $removeElements,
  98. ( ( $elementCount = $this->options->stackTraceDepth ) === 0 ? null : $elementCount )
  99. );
  100. }
  101. /**
  102. * Returns the currently selected element of the iterator.
  103. *
  104. * This method is part of the Iterator interface.
  105. *
  106. * @return mixed
  107. */
  108. public final function current()
  109. {
  110. return $this->unifyStackElement(
  111. current( $this->stackTrace )
  112. );
  113. }
  114. /**
  115. * Returns the key of the currently selected element of the iterator.
  116. *
  117. * This method is part of the Iterator interface.
  118. *
  119. * @return mixed
  120. */
  121. public final function key()
  122. {
  123. return key( $this->stackTrace );
  124. }
  125. /**
  126. * Advances the iterator to the next element.
  127. *
  128. * This method is part of the Iterator interface.
  129. *
  130. * @return mixed
  131. */
  132. public final function next()
  133. {
  134. return next( $this->stackTrace );
  135. }
  136. /**
  137. * Resets the iterator to the first element.
  138. *
  139. * This method is part of the Iterator interface.
  140. *
  141. * @return mixed
  142. */
  143. public final function rewind()
  144. {
  145. return reset( $this->stackTrace );
  146. }
  147. /**
  148. * Returns if the iterator is on a valid element or at the end.
  149. *
  150. * This method is part of the Iterator interface.
  151. *
  152. * @return bool
  153. */
  154. public final function valid()
  155. {
  156. return ( current( $this->stackTrace ) !== false );
  157. }
  158. /**
  159. * Returns if the given offset exists.
  160. *
  161. * This method is part of the ArrayAccess interface.
  162. *
  163. * @param mixed $offset
  164. * @return bool
  165. */
  166. public final function offsetExists( $offset )
  167. {
  168. return array_key_exists( $offset, $this->stackTrace );
  169. }
  170. /**
  171. * Returns the value assigned to the given offset.
  172. *
  173. * This method is part of the ArrayAccess interface.
  174. *
  175. * @param mixed $offset
  176. * @return mixed
  177. */
  178. public final function offsetGet( $offset )
  179. {
  180. if ( !$this->offsetExists( $offset ) )
  181. {
  182. throw new ezcBaseValueException(
  183. 'offset',
  184. $offset,
  185. 'valid offset'
  186. );
  187. }
  188. return $this->unifyStackElement( $this->stackTrace[$offset] );
  189. }
  190. /**
  191. * It is not allowed to use this method with this iterator.
  192. *
  193. * This method is part of the ArrayAccess interface.
  194. *
  195. * @throws ezcDebugException
  196. * @param mixed $offset
  197. * @param mixed $value
  198. * @return void
  199. */
  200. public final function offsetSet( $offset, $value )
  201. {
  202. throw new ezcDebugOperationNotPermittedException(
  203. 'setting values via ArrayAccess'
  204. );
  205. }
  206. /**
  207. * It is not allowed to use this method with this iterator.
  208. *
  209. * This method is part of the ArrayAccess interface.
  210. *
  211. * @throws ezcDebugException
  212. * @param mixed $offset
  213. * @return void
  214. */
  215. public final function offsetUnset( $offset )
  216. {
  217. throw new ezcDebugOperationNotPermittedException(
  218. 'unsetting values via ArrayAccess'
  219. );
  220. }
  221. /**
  222. * Returns the number of elements in the iterator.
  223. *
  224. * This method is part of the Countable interface.
  225. *
  226. * @return int
  227. */
  228. public final function count()
  229. {
  230. return count( $this->stackTrace );
  231. }
  232. }
  233. ?>