ExtendedFileStream.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. include_once 'phing/system/io/PhingFile.php';
  3. /**
  4. * $Id: ExtendedFileStream.php 905 2010-10-05 16:28:03Z mrook $
  5. *
  6. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  7. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  8. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  9. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  10. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  11. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  12. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  13. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  14. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  15. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  16. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  17. *
  18. * This software consists of voluntary contributions made by many individuals
  19. * and is licensed under the LGPL. For more information please see
  20. * <http://phing.info>.
  21. */
  22. /**
  23. * Extended file stream wrapper class which auto-creates directories
  24. *
  25. * @author Michiel Rook <michiel.rook@gmail.com>
  26. * @version $Id: ExtendedFileStream.php 905 2010-10-05 16:28:03Z mrook $
  27. * @package phing.util
  28. */
  29. class ExtendedFileStream
  30. {
  31. private $fp = NULL;
  32. static function registerStream()
  33. {
  34. if (!in_array("efile", stream_get_wrappers()))
  35. {
  36. stream_wrapper_register("efile", "ExtendedFileStream");
  37. }
  38. }
  39. static function unregisterStream()
  40. {
  41. stream_wrapper_unregister("efile");
  42. }
  43. private function createDirectories($path)
  44. {
  45. $f = new PhingFile($path);
  46. if (!$f->exists()) {
  47. $f->mkdirs();
  48. }
  49. }
  50. function stream_open($path, $mode, $options, &$opened_path)
  51. {
  52. /** Small fix for Windows */
  53. if ($path[8] == DIRECTORY_SEPARATOR)
  54. {
  55. $filepath = substr($path, 7);
  56. }
  57. else
  58. {
  59. $filepath = substr($path, 8);
  60. }
  61. $this->createDirectories(dirname($filepath));
  62. $this->fp = fopen($filepath, $mode);
  63. return true;
  64. }
  65. function stream_close()
  66. {
  67. fclose($this->fp);
  68. $this->fp = NULL;
  69. }
  70. function stream_read($count)
  71. {
  72. return fread($this->fp, $count);
  73. }
  74. function stream_write($data)
  75. {
  76. return fwrite($this->fp, $data);
  77. }
  78. function stream_eof()
  79. {
  80. return feof($this->fp);
  81. }
  82. function stream_tell()
  83. {
  84. return ftell($this->fp);
  85. }
  86. function stream_seek($offset, $whence)
  87. {
  88. return fseek($this->fp, $offset, $whence);
  89. }
  90. function stream_flush()
  91. {
  92. return fflush($this->fp);
  93. }
  94. function stream_stat()
  95. {
  96. return fstat($this->fp);
  97. }
  98. function unlink($path)
  99. {
  100. return FALSE;
  101. }
  102. function rename($path_from, $path_to)
  103. {
  104. return FALSE;
  105. }
  106. function mkdir($path, $mode, $options)
  107. {
  108. return FALSE;
  109. }
  110. function rmdir($path, $options)
  111. {
  112. return FALSE;
  113. }
  114. };