PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/techniconline/kmc
PHP | 174 lines | 90 code | 18 blank | 66 comment | 21 complexity | e29adc220351a652e6fbf17324567206 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. ) {
  56. mkdir(dirname($out), 0777, true);
  57. }
  58. $this->out = fopen($out, 'wt');
  59. }
  60. $this->outTarget = $out;
  61. } else {
  62. $this->out = $out;
  63. }
  64. }
  65. }
  66. /**
  67. * Flush buffer, optionally tidy up HTML, and close output if it's not to a php stream
  68. */
  69. public function flush()
  70. {
  71. if ($this->out && strncmp($this->outTarget, 'php://', 6) !== 0) {
  72. fclose($this->out);
  73. }
  74. if ($this->printsHTML === true &&
  75. $this->outTarget !== null &&
  76. strpos($this->outTarget, 'php://') !== 0 &&
  77. strpos($this->outTarget, 'socket://') !== 0 &&
  78. extension_loaded('tidy')
  79. ) {
  80. file_put_contents(
  81. $this->outTarget,
  82. tidy_repair_file(
  83. $this->outTarget,
  84. array('indent' => true, 'wrap' => 0),
  85. 'utf8'
  86. )
  87. );
  88. }
  89. }
  90. /**
  91. * Performs a safe, incremental flush.
  92. *
  93. * Do not confuse this function with the flush() function of this class,
  94. * since the flush() function may close the file being written to, rendering
  95. * the current object no longer usable.
  96. *
  97. * @since Method available since Release 3.3.0
  98. */
  99. public function incrementalFlush()
  100. {
  101. if ($this->out) {
  102. fflush($this->out);
  103. } else {
  104. flush();
  105. }
  106. }
  107. /**
  108. * @param string $buffer
  109. */
  110. public function write($buffer)
  111. {
  112. if ($this->out) {
  113. fwrite($this->out, $buffer);
  114. if ($this->autoFlush) {
  115. $this->incrementalFlush();
  116. }
  117. } else {
  118. if (PHP_SAPI != 'cli') {
  119. $buffer = htmlspecialchars($buffer);
  120. }
  121. print $buffer;
  122. if ($this->autoFlush) {
  123. $this->incrementalFlush();
  124. }
  125. }
  126. }
  127. /**
  128. * Check auto-flush mode.
  129. *
  130. * @return bool
  131. *
  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. *
  146. * @since Method available since Release 3.3.0
  147. */
  148. public function setAutoFlush($autoFlush)
  149. {
  150. if (is_bool($autoFlush)) {
  151. $this->autoFlush = $autoFlush;
  152. } else {
  153. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
  154. }
  155. }
  156. }