PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

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