PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/iti-note-mvc-php/vendor/doctrine/common/lib/Doctrine/Common/Util/Debug.php

https://bitbucket.org/alessandro-aglietti/itis-leonardo-da-vinci
PHP | 142 lines | 71 code | 19 blank | 52 comment | 14 complexity | 693d87111579c70a854e77d701463816 MD5 | raw file
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Util;
  20. use Doctrine\Common\Persistence\Proxy;
  21. /**
  22. * Static class containing most used debug methods.
  23. *
  24. * @link www.doctrine-project.org
  25. * @since 2.0
  26. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  27. * @author Jonathan Wage <jonwage@gmail.com>
  28. * @author Roman Borschel <roman@code-factory.org>
  29. * @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
  30. */
  31. final class Debug
  32. {
  33. /**
  34. * Private constructor (prevents instantiation).
  35. */
  36. private function __construct()
  37. {
  38. }
  39. /**
  40. * Prints a dump of the public, protected and private properties of $var.
  41. *
  42. * @link http://xdebug.org/
  43. *
  44. * @param mixed $var The variable to dump.
  45. * @param integer $maxDepth The maximum nesting level for object properties.
  46. * @param boolean $stripTags Whether output should strip HTML tags.
  47. */
  48. public static function dump($var, $maxDepth = 2, $stripTags = true)
  49. {
  50. ini_set('html_errors', 'On');
  51. if (extension_loaded('xdebug')) {
  52. ini_set('xdebug.var_display_max_depth', $maxDepth);
  53. }
  54. $var = self::export($var, $maxDepth++);
  55. ob_start();
  56. var_dump($var);
  57. $dump = ob_get_contents();
  58. ob_end_clean();
  59. echo ($stripTags ? strip_tags(html_entity_decode($dump)) : $dump);
  60. ini_set('html_errors', 'Off');
  61. }
  62. /**
  63. * @param mixed $var
  64. * @param int $maxDepth
  65. *
  66. * @return mixed
  67. */
  68. public static function export($var, $maxDepth)
  69. {
  70. $return = null;
  71. $isObj = is_object($var);
  72. if ($isObj && in_array('Doctrine\Common\Collections\Collection', class_implements($var))) {
  73. $var = $var->toArray();
  74. }
  75. if ($maxDepth) {
  76. if (is_array($var)) {
  77. $return = array();
  78. foreach ($var as $k => $v) {
  79. $return[$k] = self::export($v, $maxDepth - 1);
  80. }
  81. } else if ($isObj) {
  82. $return = new \stdclass();
  83. if ($var instanceof \DateTime) {
  84. $return->__CLASS__ = "DateTime";
  85. $return->date = $var->format('c');
  86. $return->timezone = $var->getTimeZone()->getName();
  87. } else {
  88. $reflClass = ClassUtils::newReflectionObject($var);
  89. $return->__CLASS__ = ClassUtils::getClass($var);
  90. if ($var instanceof Proxy) {
  91. $return->__IS_PROXY__ = true;
  92. $return->__PROXY_INITIALIZED__ = $var->__isInitialized();
  93. }
  94. if ($var instanceof \ArrayObject || $var instanceof \ArrayIterator) {
  95. $return->__STORAGE__ = self::export($var->getArrayCopy(), $maxDepth - 1);
  96. }
  97. foreach ($reflClass->getProperties() as $reflProperty) {
  98. $name = $reflProperty->getName();
  99. $reflProperty->setAccessible(true);
  100. $return->$name = self::export($reflProperty->getValue($var), $maxDepth - 1);
  101. }
  102. }
  103. } else {
  104. $return = $var;
  105. }
  106. } else {
  107. $return = is_object($var) ? get_class($var)
  108. : (is_array($var) ? 'Array(' . count($var) . ')' : $var);
  109. }
  110. return $return;
  111. }
  112. /**
  113. * Returns a string representation of an object.
  114. *
  115. * @param object $obj
  116. *
  117. * @return string
  118. */
  119. public static function toString($obj)
  120. {
  121. return method_exists($obj, '__toString') ? (string) $obj : get_class($obj) . '@' . spl_object_hash($obj);
  122. }
  123. }