PageRenderTime 22ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Front_End/vendor/phpunit/phpunit/src/Util/Printer.php

https://gitlab.com/Sigpot/AirSpot
PHP | 172 lines | 88 code | 18 blank | 66 comment | 23 complexity | bf8cd1d0ff033ee7b42c193be897dcf0 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of PHPUnit.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Utility class that can print to STDOUT or write to a file.
  12. *
  13. * @since Class available since Release 2.0.0
  14. */
  15. class PHPUnit_Util_Printer
  16. {
  17. /**
  18. * If true, flush output after every write.
  19. *
  20. * @var bool
  21. */
  22. protected $autoFlush = false;
  23. /**
  24. * @var resource
  25. */
  26. protected $out;
  27. /**
  28. * @var string
  29. */
  30. protected $outTarget;
  31. /**
  32. * @var bool
  33. */
  34. protected $printsHTML = false;
  35. /**
  36. * Constructor.
  37. *
  38. * @param mixed $out
  39. *
  40. * @throws PHPUnit_Framework_Exception
  41. */
  42. public function __construct($out = null)
  43. {
  44. if ($out !== null) {
  45. if (is_string($out)) {
  46. if (strpos($out, 'socket://') === 0) {
  47. $out = explode(':', str_replace('socket://', '', $out));
  48. if (sizeof($out) != 2) {
  49. throw new PHPUnit_Framework_Exception;
  50. }
  51. $this->out = fsockopen($out[0], $out[1]);
  52. } else {
  53. if (strpos($out, 'php://') === false &&
  54. !is_dir(dirname($out))) {
  55. mkdir(dirname($out), 0777, true);
  56. }
  57. $this->out = fopen($out, 'wt');
  58. }
  59. $this->outTarget = $out;
  60. } else {
  61. $this->out = $out;
  62. }
  63. }
  64. }
  65. /**
  66. * Flush buffer, optionally tidy up HTML, and close output if it's not to a php stream
  67. */
  68. public function flush()
  69. {
  70. if ($this->out && strncmp($this->outTarget, 'php://', 6) !== 0) {
  71. fclose($this->out);
  72. }
  73. if ($this->printsHTML === true &&
  74. $this->outTarget !== null &&
  75. strpos($this->outTarget, 'php://') !== 0 &&
  76. strpos($this->outTarget, 'socket://') !== 0 &&
  77. extension_loaded('tidy')) {
  78. file_put_contents(
  79. $this->outTarget,
  80. tidy_repair_file(
  81. $this->outTarget,
  82. array('indent' => true, 'wrap' => 0),
  83. 'utf8'
  84. )
  85. );
  86. }
  87. }
  88. /**
  89. * Performs a safe, incremental flush.
  90. *
  91. * Do not confuse this function with the flush() function of this class,
  92. * since the flush() function may close the file being written to, rendering
  93. * the current object no longer usable.
  94. *
  95. * @since Method available since Release 3.3.0
  96. */
  97. public function incrementalFlush()
  98. {
  99. if ($this->out) {
  100. fflush($this->out);
  101. } else {
  102. flush();
  103. }
  104. }
  105. /**
  106. * @param string $buffer
  107. */
  108. public function write($buffer)
  109. {
  110. if ($this->out) {
  111. fwrite($this->out, $buffer);
  112. if ($this->autoFlush) {
  113. $this->incrementalFlush();
  114. }
  115. } else {
  116. if (PHP_SAPI != 'cli' && PHP_SAPI != 'phpdbg') {
  117. $buffer = htmlspecialchars($buffer);
  118. }
  119. print $buffer;
  120. if ($this->autoFlush) {
  121. $this->incrementalFlush();
  122. }
  123. }
  124. }
  125. /**
  126. * Check auto-flush mode.
  127. *
  128. * @return bool
  129. *
  130. * @since Method available since Release 3.3.0
  131. */
  132. public function getAutoFlush()
  133. {
  134. return $this->autoFlush;
  135. }
  136. /**
  137. * Set auto-flushing mode.
  138. *
  139. * If set, *incremental* flushes will be done after each write. This should
  140. * not be confused with the different effects of this class' flush() method.
  141. *
  142. * @param bool $autoFlush
  143. *
  144. * @since Method available since Release 3.3.0
  145. */
  146. public function setAutoFlush($autoFlush)
  147. {
  148. if (is_bool($autoFlush)) {
  149. $this->autoFlush = $autoFlush;
  150. } else {
  151. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
  152. }
  153. }
  154. }