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

/src/frapi/tests/phpunit/PHPUnit/Util/Printer.php

https://github.com/Martin1982/IBMessagingWorkshopServer
PHP | 211 lines | 84 code | 19 blank | 108 comment | 20 complexity | eb2e12cd4c35d3de9ad9a2a81379eb1a MD5 | raw file
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @category Testing
  38. * @package PHPUnit
  39. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  40. * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. * @version SVN: $Id: Printer.php 4930 2009-06-06 08:18:48Z sb $
  43. * @link http://www.phpunit.de/
  44. * @since File available since Release 2.0.0
  45. */
  46. require_once 'PHPUnit/Util/Filter.php';
  47. PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
  48. /**
  49. * Utility class that can print to STDOUT or write to a file.
  50. *
  51. * @category Testing
  52. * @package PHPUnit
  53. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  54. * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
  55. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  56. * @version Release: @package_version@
  57. * @link http://www.phpunit.de/
  58. * @since Class available since Release 2.0.0
  59. * @abstract
  60. */
  61. abstract class PHPUnit_Util_Printer
  62. {
  63. /**
  64. * If TRUE, flush output after every write.
  65. *
  66. * @var boolean
  67. */
  68. protected $autoFlush = FALSE;
  69. /**
  70. * @var resource
  71. */
  72. protected $out;
  73. /**
  74. * @var string
  75. */
  76. protected $outTarget;
  77. /**
  78. * @var boolean
  79. */
  80. protected $printsHTML = FALSE;
  81. /**
  82. * Constructor.
  83. *
  84. * @param mixed $out
  85. * @throws InvalidArgumentException
  86. */
  87. public function __construct($out = NULL)
  88. {
  89. if ($out !== NULL) {
  90. if (is_string($out)) {
  91. if (strpos($out, 'socket://') === 0) {
  92. $out = explode(':', str_replace('socket://', '', $out));
  93. if (sizeof($out) != 2) {
  94. throw new InvalidArgumentException;
  95. }
  96. $this->out = fsockopen($out[0], $out[1]);
  97. } else {
  98. $this->out = fopen($out, 'wt');
  99. }
  100. $this->outTarget = $out;
  101. } else {
  102. $this->out = $out;
  103. }
  104. }
  105. }
  106. /**
  107. * Flush buffer, optionally tidy up HTML, and close output.
  108. *
  109. */
  110. public function flush()
  111. {
  112. if ($this->out !== NULL) {
  113. fclose($this->out);
  114. }
  115. if ($this->printsHTML === TRUE && $this->outTarget !== NULL &&
  116. strpos($this->outTarget, 'php://') !== 0 &&
  117. strpos($this->outTarget, 'socket://') !== 0 &&
  118. extension_loaded('tidy')) {
  119. file_put_contents(
  120. $this->outTarget,
  121. tidy_repair_file(
  122. $this->outTarget, array('indent' => TRUE, 'wrap' => 0), 'utf8'
  123. )
  124. );
  125. }
  126. }
  127. /**
  128. * Performs a safe, incremental flush.
  129. *
  130. * Do not confuse this function with the flush() function of this class,
  131. * since the flush() function may close the file being written to, rendering
  132. * the current object no longer usable.
  133. *
  134. * @since Method available since Release 3.3.0
  135. */
  136. public function incrementalFlush()
  137. {
  138. if ($this->out !== NULL) {
  139. fflush($this->out);
  140. } else {
  141. flush();
  142. }
  143. }
  144. /**
  145. * @param string $buffer
  146. */
  147. public function write($buffer)
  148. {
  149. if ($this->out !== NULL) {
  150. fwrite($this->out, $buffer);
  151. if ($this->autoFlush) {
  152. $this->incrementalFlush();
  153. }
  154. } else {
  155. if (PHP_SAPI != 'cli') {
  156. $buffer = htmlspecialchars($buffer);
  157. }
  158. print $buffer;
  159. if ($this->autoFlush) {
  160. $this->incrementalFlush();
  161. }
  162. }
  163. }
  164. /**
  165. * Check auto-flush mode.
  166. *
  167. * @return boolean
  168. * @since Method available since Release 3.3.0
  169. */
  170. public function getAutoFlush()
  171. {
  172. return $this->autoFlush;
  173. }
  174. /**
  175. * Set auto-flushing mode.
  176. *
  177. * If set, *incremental* flushes will be done after each write. This should
  178. * not be confused with the different effects of this class' flush() method.
  179. *
  180. * @param boolean $autoFlush
  181. * @since Method available since Release 3.3.0
  182. */
  183. public function setAutoFlush($autoFlush)
  184. {
  185. if (is_bool($autoFlush)) {
  186. $this->autoFlush = $autoFlush;
  187. } else {
  188. throw new InvalidArgumentException;
  189. }
  190. }
  191. }
  192. ?>