/git/src/TQ/Git/Cli/CallResult.php

https://github.com/WPsites/WPide · PHP · 226 lines · 75 code · 22 blank · 129 comment · 2 complexity · 00c096d3886875bdc0ce53b4fec1dc7f MD5 · raw file

  1. <?php
  2. /*
  3. * Copyright (C) 2011 by TEQneers GmbH & Co. KG
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. /**
  24. * Git Streamwrapper for PHP
  25. *
  26. * @category TQ
  27. * @package TQ_Git
  28. * @subpackage Cli
  29. * @copyright Copyright (C) 2011 by TEQneers GmbH & Co. KG
  30. */
  31. /**
  32. * @namespace
  33. */
  34. namespace TQ\Git\Cli;
  35. /**
  36. * The result of a CLI call - provides access to stdout, stderror and the return code
  37. *
  38. * @author Stefan Gehrig <gehrigteqneers.de>
  39. * @category TQ
  40. * @package TQ_Git
  41. * @subpackage Cli
  42. * @copyright Copyright (C) 2011 by TEQneers GmbH & Co. KG
  43. */
  44. class CallResult
  45. {
  46. /**
  47. * The stdout stream
  48. *
  49. * @var resource
  50. */
  51. protected $stdOut;
  52. /**
  53. * True if there is a stdout
  54. *
  55. * @var boolean
  56. */
  57. protected $hasStdOut;
  58. /**
  59. * The stderr stream
  60. *
  61. * @var resource
  62. */
  63. protected $stdErr;
  64. /**
  65. * True if there is a stderr
  66. *
  67. * @var boolean
  68. */
  69. protected $hasStdErr;
  70. /**
  71. * The return code
  72. *
  73. * @var integer
  74. */
  75. protected $returnCode;
  76. /**
  77. * Reference to the call that resulted in this result
  78. *
  79. * @var Call
  80. */
  81. protected $cliCall;
  82. /**
  83. * Creates a new result container for a CLI call
  84. *
  85. * @param Call $cliCall Reference to the call that resulted in this result
  86. * @param resource $stdOut The stdout stream
  87. * @param resource $stdErr The stderr stream
  88. * @param integer $returnCode The return code
  89. */
  90. public function __construct(Call $cliCall, $stdOut, $stdErr, $returnCode)
  91. {
  92. // @todo is there a better way to determine if a stream contains data?
  93. fseek($stdOut, 0, SEEK_END);
  94. $hasStdOut = (ftell($stdOut) > 0);
  95. fseek($stdOut, 0, SEEK_SET);
  96. // @todo is there a better way to determine if a stream contains data?
  97. fseek($stdErr, 0, SEEK_END);
  98. $hasStdErr = (ftell($stdErr) > 0);
  99. fseek($stdErr, 0, SEEK_SET);
  100. $this->cliCall = $cliCall;
  101. $this->stdOut = $stdOut;
  102. $this->hasStdOut = $hasStdOut;
  103. $this->stdErr = $stdErr;
  104. $this->hasStdErr = $hasStdErr;
  105. $this->returnCode = (int)$returnCode;
  106. }
  107. /**
  108. * Destructor closes the result and the internal stream resources
  109. */
  110. public function __destruct()
  111. {
  112. $this->close();
  113. }
  114. /**
  115. * Returns the reference to the call that resulted in this result
  116. *
  117. * @return Call
  118. */
  119. public function getCliCall()
  120. {
  121. return $this->cliCall;
  122. }
  123. /**
  124. * Returns the stdout stream
  125. *
  126. * @return resource
  127. */
  128. public function getStdOutStream()
  129. {
  130. return $this->stdOut;
  131. }
  132. /**
  133. * Returns the contents of stdout
  134. *
  135. * @return string
  136. */
  137. public function getStdOut()
  138. {
  139. fseek($this->stdOut, 0, SEEK_SET);
  140. return rtrim(stream_get_contents($this->stdOut));
  141. }
  142. /**
  143. * Returns true if the call resulted in stdout to be populated
  144. *
  145. * @return boolean
  146. */
  147. public function hasStdOut()
  148. {
  149. return $this->hasStdOut;
  150. }
  151. /**
  152. * Returns the stderr stream
  153. *
  154. * @return resource
  155. */
  156. public function getStdErrStream()
  157. {
  158. return $this->stdErr;
  159. }
  160. /**
  161. * Returns the contents of stderr
  162. *
  163. * @return string
  164. */
  165. public function getStdErr()
  166. {
  167. fseek($this->stdErr, 0, SEEK_SET);
  168. return rtrim(stream_get_contents($this->stdErr));
  169. }
  170. /**
  171. * Returns true if the call resulted in stderr to be populated
  172. *
  173. * @return boolean
  174. */
  175. public function hasStdErr()
  176. {
  177. return $this->hasStdErr;
  178. }
  179. /**
  180. * Returns the return code
  181. *
  182. * @return integer
  183. */
  184. public function getReturnCode()
  185. {
  186. return $this->returnCode;
  187. }
  188. /**
  189. * Closes the call result and the internal stream resources
  190. *
  191. * Prevents further usage
  192. */
  193. public function close()
  194. {
  195. if ($this->stdOut !== null) {
  196. fclose($this->stdOut);
  197. $this->stdOut = null;
  198. }
  199. if ($this->stdErr !== null) {
  200. fclose($this->stdErr);
  201. $this->stdErr = null;
  202. }
  203. }
  204. }