PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/App/Library/vendor/doctrine/common/lib/Doctrine/Common/Util/Debug.php

https://gitlab.com/aleksbenmaza/PPE_NEW
PHP | 197 lines | 98 code | 35 blank | 64 comment | 14 complexity | 357a595a1b9389605e0276297ee8b5b7 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\Collections\Collection;
  21. use Doctrine\Common\Persistence\Proxy;
  22. /**
  23. * Static class containing most used debug methods.
  24. *
  25. * @link www.doctrine-project.org
  26. * @since 2.0
  27. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  28. * @author Jonathan Wage <jonwage@gmail.com>
  29. * @author Roman Borschel <roman@code-factory.org>
  30. * @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
  31. */
  32. final class Debug
  33. {
  34. /**
  35. * Private constructor (prevents instantiation).
  36. */
  37. private function __construct()
  38. {
  39. }
  40. /**
  41. * Prints a dump of the public, protected and private properties of $var.
  42. *
  43. * @link http://xdebug.org/
  44. *
  45. * @param mixed $var The variable to dump.
  46. * @param integer $maxDepth The maximum nesting level for object properties.
  47. * @param boolean $stripTags Whether output should strip HTML tags.
  48. * @param boolean $echo Send the dumped value to the output buffer
  49. *
  50. * @return string
  51. */
  52. public static function dump($var, $maxDepth = 2, $stripTags = true, $echo = true)
  53. {
  54. $html = ini_get('html_errors');
  55. if ($html !== true) {
  56. ini_set('html_errors', true);
  57. }
  58. if (extension_loaded('xdebug')) {
  59. ini_set('xdebug.var_display_max_depth', $maxDepth);
  60. }
  61. $var = self::export($var, $maxDepth);
  62. ob_start();
  63. var_dump($var);
  64. $dump = ob_get_contents();
  65. ob_end_clean();
  66. $dumpText = ($stripTags ? strip_tags(html_entity_decode($dump)) : $dump);
  67. ini_set('html_errors', $html);
  68. if ($echo) {
  69. echo $dumpText;
  70. }
  71. return $dumpText;
  72. }
  73. /**
  74. * @param mixed $var
  75. * @param int $maxDepth
  76. *
  77. * @return mixed
  78. */
  79. public static function export($var, $maxDepth)
  80. {
  81. $return = null;
  82. $isObj = is_object($var);
  83. if ($var instanceof Collection) {
  84. $var = $var->toArray();
  85. }
  86. if (! $maxDepth) {
  87. return is_object($var) ? get_class($var)
  88. : (is_array($var) ? 'Array(' . count($var) . ')' : $var);
  89. }
  90. if (is_array($var)) {
  91. $return = [];
  92. foreach ($var as $k => $v) {
  93. $return[$k] = self::export($v, $maxDepth - 1);
  94. }
  95. return $return;
  96. }
  97. if (! $isObj) {
  98. return $var;
  99. }
  100. $return = new \stdclass();
  101. if ($var instanceof \DateTimeInterface) {
  102. $return->__CLASS__ = get_class($var);
  103. $return->date = $var->format('c');
  104. $return->timezone = $var->getTimezone()->getName();
  105. return $return;
  106. }
  107. $return->__CLASS__ = ClassUtils::getClass($var);
  108. if ($var instanceof Proxy) {
  109. $return->__IS_PROXY__ = true;
  110. $return->__PROXY_INITIALIZED__ = $var->__isInitialized();
  111. }
  112. if ($var instanceof \ArrayObject || $var instanceof \ArrayIterator) {
  113. $return->__STORAGE__ = self::export($var->getArrayCopy(), $maxDepth - 1);
  114. }
  115. return self::fillReturnWithClassAttributes($var, $return, $maxDepth);
  116. }
  117. /**
  118. * Fill the $return variable with class attributes
  119. *
  120. * @param object $var
  121. * @param stdClass $return
  122. * @param int $maxDepth
  123. *
  124. * @return mixed
  125. */
  126. private static function fillReturnWithClassAttributes($var, \stdClass $return, $maxDepth)
  127. {
  128. $reflClass = ClassUtils::newReflectionObject($var);
  129. $parsedAttributes = array();
  130. do {
  131. $currentClassName = $reflClass->getName();
  132. foreach ($reflClass->getProperties() as $reflProperty) {
  133. $attributeKey = $reflProperty->isPrivate() ? $currentClassName . '#' : '';
  134. $attributeKey .= $reflProperty->getName();
  135. if (isset($parsedAttributes[$attributeKey])) {
  136. continue;
  137. }
  138. $parsedAttributes[$attributeKey] = true;
  139. $name =
  140. $reflProperty->getName()
  141. . ($return->__CLASS__ !== $currentClassName || $reflProperty->isPrivate() ? ':' . $currentClassName : '')
  142. . ($reflProperty->isPrivate() ? ':private' : '')
  143. . ($reflProperty->isProtected() ? ':protected' : '')
  144. ;
  145. $reflProperty->setAccessible(true);
  146. $return->$name = self::export($reflProperty->getValue($var), $maxDepth - 1);
  147. }
  148. } while ($reflClass = $reflClass->getParentClass());
  149. return $return;
  150. }
  151. /**
  152. * Returns a string representation of an object.
  153. *
  154. * @param object $obj
  155. *
  156. * @return string
  157. */
  158. public static function toString($obj)
  159. {
  160. return method_exists($obj, '__toString') ? (string) $obj : get_class($obj) . '@' . spl_object_hash($obj);
  161. }
  162. }