/lib/Varien/Debug.php

https://github.com/seloshow/Magento-Pruebas · PHP · 189 lines · 118 code · 14 blank · 57 comment · 41 complexity · edd0fabca94882fcecdba9c6f5a25982 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@magentocommerce.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.magentocommerce.com for more information.
  20. *
  21. * @category Varien
  22. * @package Varien_Debug
  23. * @copyright Copyright (c) 2009 Irubin Consulting Inc. DBA Varien (http://www.varien.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. /**
  36. * Magento Root path
  37. *
  38. * @var string
  39. */
  40. protected static $_filePath;
  41. /**
  42. * Retrieve real root path with last directory separator
  43. *
  44. * @return string
  45. */
  46. public static function getRootPath()
  47. {
  48. if (is_null(self::$_filePath)) {
  49. if (defined('BP')) {
  50. self::$_filePath = BP;
  51. } else {
  52. self::$_filePath = dirname(dirname(__FILE__));
  53. }
  54. }
  55. return self::$_filePath;
  56. }
  57. /**
  58. * Prints or return a backtrace
  59. *
  60. * @param bool $return return or print
  61. * @param bool $html output in HTML format
  62. * @param bool $withArgs add short argumets of methods
  63. * @return string|bool
  64. */
  65. public static function backtrace($return = false, $html = true, $withArgs = true)
  66. {
  67. $trace = debug_backtrace();
  68. $out = '';
  69. if ($html) {
  70. $out .= '<pre>';
  71. }
  72. foreach ($trace as $i => $data) {
  73. // skip self
  74. if ($i == 0) {
  75. continue;
  76. }
  77. // prepare method argments
  78. $args = array();
  79. if (isset($data['args']) && $withArgs) {
  80. foreach ($data['args'] as $arg) {
  81. $args[] = self::_formatCalledArgument($arg);
  82. }
  83. }
  84. // prepare method's name
  85. if (isset($data['class']) && isset($data['function'])) {
  86. if (isset($data['object']) && get_class($data['object']) != $data['class']) {
  87. $className = get_class($data['object']) . '[' . $data['class'] . ']';
  88. } else {
  89. $className = $data['class'];
  90. }
  91. if (isset($data['object'])) {
  92. $className .= sprintf('#%s#', spl_object_hash($data['object']));
  93. }
  94. $methodName = sprintf('%s%s%s(%s)',
  95. $className,
  96. isset($data['type']) ? $data['type'] : '->',
  97. $data['function'],
  98. join(', ', $args)
  99. );
  100. } else if (isset($data['function'])) {
  101. $methodName = sprintf('%s(%s)', $data['function'], join(', ', $args));
  102. }
  103. if (isset($data['file'])) {
  104. $pos = strpos($data['file'], self::getRootPath());
  105. if ($pos !== false) {
  106. $data['file'] = substr($data['file'], strlen(self::getRootPath()) + 1);
  107. }
  108. $fileName = sprintf('%s:%d', $data['file'], $data['line']);
  109. } else {
  110. $fileName = false;
  111. }
  112. if ($fileName) {
  113. $out .= sprintf('#%d %s called at [%s]', $i, $methodName, $fileName);
  114. } else {
  115. $out .= sprintf('#%d %s', $i, $methodName);
  116. }
  117. $out .= "\n";
  118. }
  119. if ($html) {
  120. $out .= '</pre>';
  121. }
  122. if ($return) {
  123. return $out;
  124. } else {
  125. echo $out;
  126. return true;
  127. }
  128. }
  129. /**
  130. * Format argument in called method
  131. *
  132. * @param mixed $arg
  133. */
  134. protected static function _formatCalledArgument($arg)
  135. {
  136. $out = '';
  137. if (is_object($arg)) {
  138. $out .= sprintf("&%s#%s#", get_class($arg), spl_object_hash($arg));
  139. } else if (is_resource($arg)) {
  140. $out .= '#[' . get_resource_type($arg) . ']';
  141. } else if (is_array($arg)) {
  142. $isAssociative = false;
  143. $args = array();
  144. foreach ($arg as $k => $v) {
  145. if (!is_numeric($k)) {
  146. $isAssociative = true;
  147. }
  148. $args[$k] = self::_formatCalledArgument($v);
  149. }
  150. if ($isAssociative) {
  151. $arr = array();
  152. foreach ($args as $k => $v) {
  153. $arr[] = self::_formatCalledArgument($k) . ' => ' . $v;
  154. }
  155. $out .= 'array(' . join(', ', $arr) . ')';
  156. } else {
  157. $out .= 'array(' . join(', ', $args) . ')';
  158. }
  159. } else if (is_null($arg)) {
  160. $out .= 'NULL';
  161. } else if (is_numeric($arg) || is_float($arg)) {
  162. $out .= $arg;
  163. } else if (is_string($arg)) {
  164. if (strlen($arg) > 10) {
  165. $arg = substr($arg, 0, 10) . "...";
  166. }
  167. $arg = strtr($arg, array("\t" => '\t', "\r" => '\r', "\n" => '\n', "'" => '\\\''));
  168. $out .= "'" . $arg . "'";
  169. } else if (is_bool($arg)) {
  170. $out .= $arg === true ? 'true' : 'false';
  171. }
  172. return $out;
  173. }
  174. }