PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/lib/Varien/Debug.php

https://bitbucket.org/mkrasuski/magento-ce
PHP | 204 lines | 123 code | 15 blank | 66 comment | 41 complexity | 5e30a0e05bfbd2a41b0ade3d076702ff MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magento.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category Varien
  22. * @package Varien_Debug
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Varien Debug methods
  28. *
  29. * @category Varien
  30. * @package Varien_Debug
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Varien_Debug
  34. {
  35. public static $argLength = 16;
  36. /**
  37. * Magento Root path
  38. *
  39. * @var string
  40. */
  41. protected static $_filePath;
  42. /**
  43. * Retrieve real root path with last directory separator
  44. *
  45. * @return string
  46. */
  47. public static function getRootPath()
  48. {
  49. if (is_null(self::$_filePath)) {
  50. if (defined('BP')) {
  51. self::$_filePath = BP;
  52. } else {
  53. self::$_filePath = dirname(dirname(__FILE__));
  54. }
  55. }
  56. return self::$_filePath;
  57. }
  58. /**
  59. * Prints or return a backtrace
  60. *
  61. * @param bool $return return or print
  62. * @param bool $html output in HTML format
  63. * @param bool $withArgs add short argumets of methods
  64. * @return string|bool
  65. */
  66. public static function backtrace($return = false, $html = true, $withArgs = true)
  67. {
  68. $trace = debug_backtrace();
  69. return self::trace($trace, $return, $html, $withArgs);
  70. }
  71. /**
  72. * Prints or return a trace
  73. *
  74. * @param array $trace trace array
  75. * @param bool $return return or print
  76. * @param bool $html output in HTML format
  77. * @param bool $withArgs add short argumets of methods
  78. * @return string|bool
  79. */
  80. public static function trace(array $trace, $return = false, $html = true, $withArgs = true)
  81. {
  82. $out = '';
  83. if ($html) {
  84. $out .= '<pre>';
  85. }
  86. foreach ($trace as $i => $data) {
  87. // skip self
  88. if ($i == 0) {
  89. continue;
  90. }
  91. // prepare method argments
  92. $args = array();
  93. if (isset($data['args']) && $withArgs) {
  94. foreach ($data['args'] as $arg) {
  95. $args[] = self::_formatCalledArgument($arg);
  96. }
  97. }
  98. // prepare method's name
  99. if (isset($data['class']) && isset($data['function'])) {
  100. if (isset($data['object']) && get_class($data['object']) != $data['class']) {
  101. $className = get_class($data['object']) . '[' . $data['class'] . ']';
  102. } else {
  103. $className = $data['class'];
  104. }
  105. if (isset($data['object'])) {
  106. $className .= sprintf('#%s#', spl_object_hash($data['object']));
  107. }
  108. $methodName = sprintf('%s%s%s(%s)',
  109. $className,
  110. isset($data['type']) ? $data['type'] : '->',
  111. $data['function'],
  112. join(', ', $args)
  113. );
  114. } else if (isset($data['function'])) {
  115. $methodName = sprintf('%s(%s)', $data['function'], join(', ', $args));
  116. }
  117. if (isset($data['file'])) {
  118. $pos = strpos($data['file'], self::getRootPath());
  119. if ($pos !== false) {
  120. $data['file'] = substr($data['file'], strlen(self::getRootPath()) + 1);
  121. }
  122. $fileName = sprintf('%s:%d', $data['file'], $data['line']);
  123. } else {
  124. $fileName = false;
  125. }
  126. if ($fileName) {
  127. $out .= sprintf('#%d %s called at [%s]', $i, $methodName, $fileName);
  128. } else {
  129. $out .= sprintf('#%d %s', $i, $methodName);
  130. }
  131. $out .= "\n";
  132. }
  133. if ($html) {
  134. $out .= '</pre>';
  135. }
  136. if ($return) {
  137. return $out;
  138. } else {
  139. echo $out;
  140. return true;
  141. }
  142. }
  143. /**
  144. * Format argument in called method
  145. *
  146. * @param mixed $arg
  147. */
  148. protected static function _formatCalledArgument($arg)
  149. {
  150. $out = '';
  151. if (is_object($arg)) {
  152. $out .= sprintf("&%s#%s#", get_class($arg), spl_object_hash($arg));
  153. } else if (is_resource($arg)) {
  154. $out .= '#[' . get_resource_type($arg) . ']';
  155. } else if (is_array($arg)) {
  156. $isAssociative = false;
  157. $args = array();
  158. foreach ($arg as $k => $v) {
  159. if (!is_numeric($k)) {
  160. $isAssociative = true;
  161. }
  162. $args[$k] = self::_formatCalledArgument($v);
  163. }
  164. if ($isAssociative) {
  165. $arr = array();
  166. foreach ($args as $k => $v) {
  167. $arr[] = self::_formatCalledArgument($k) . ' => ' . $v;
  168. }
  169. $out .= 'array(' . join(', ', $arr) . ')';
  170. } else {
  171. $out .= 'array(' . join(', ', $args) . ')';
  172. }
  173. } else if (is_null($arg)) {
  174. $out .= 'NULL';
  175. } else if (is_numeric($arg) || is_float($arg)) {
  176. $out .= $arg;
  177. } else if (is_string($arg)) {
  178. if (strlen($arg) > self::$argLength) {
  179. $arg = substr($arg, 0, self::$argLength) . "...";
  180. }
  181. $arg = strtr($arg, array("\t" => '\t', "\r" => '\r', "\n" => '\n', "'" => '\\\''));
  182. $out .= "'" . $arg . "'";
  183. } else if (is_bool($arg)) {
  184. $out .= $arg === true ? 'true' : 'false';
  185. }
  186. return $out;
  187. }
  188. }