1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
/**
21
 * Extended file stream wrapper class which auto-creates directories
22
 *
23
 * @author  Michiel Rook <mrook@php.net>
24
 * @package phing.util
25
 */
26
class ExtendedFileStream
27
{
28
    private $fp = null;
29

30 0
    public static function registerStream()
31
    {
32 0
        if (!in_array("efile", stream_get_wrappers())) {
33 0
            stream_wrapper_register("efile", "ExtendedFileStream");
34
        }
35
    }
36

37 0
    public static function unregisterStream()
38
    {
39 0
        stream_wrapper_unregister("efile");
40
    }
41

42
    /**
43
     * @param $path
44
     */
45 0
    private function createDirectories($path)
46
    {
47 0
        $f = new PhingFile($path);
48 0
        if (!$f->exists()) {
49 0
            $f->mkdirs();
50
        }
51
    }
52

53
    // @codingStandardsIgnoreStart
54
    /**
55
     * @param $path
56
     * @param $mode
57
     * @param $options
58
     * @param $opened_path
59
     * @return bool
60
     * @throws IOException
61
     */
62 0
    public function stream_open($path, $mode, $options, &$opened_path)
63
    {
64
        // if we're on Windows, urldecode() the path again
65 0
        if (FileSystem::getFileSystem()->getSeparator() == '\\') {
66 0
            $path = urldecode($path);
67
        }
68

69 0
        $filepath = substr($path, 8);
70

71 0
        $this->createDirectories(dirname($filepath));
72

73 0
        $this->fp = fopen($filepath, $mode);
74

75 0
        if (!$this->fp) {
76 0
            throw new BuildException("Unable to open stream for path {$path}");
77
        }
78

79 0
        return true;
80
    }
81

82 0
    public function stream_close()
83
    {
84 0
        fclose($this->fp);
85 0
        $this->fp = null;
86
    }
87

88
    /**
89
     * @param $count
90
     * @return string
91
     */
92 0
    public function stream_read($count)
93
    {
94 0
        return fread($this->fp, $count);
95
    }
96

97
    /**
98
     * @param $data
99
     * @return int
100
     */
101 0
    public function stream_write($data)
102
    {
103 0
        return fwrite($this->fp, $data);
104
    }
105

106
    /**
107
     * @return bool
108
     */
109 0
    public function stream_eof()
110
    {
111 0
        return feof($this->fp);
112
    }
113

114
    /**
115
     * @return int
116
     */
117 0
    public function stream_tell()
118
    {
119 0
        return ftell($this->fp);
120
    }
121

122
    /**
123
     * @param $offset
124
     * @param $whence
125
     * @return int
126
     */
127 0
    public function stream_seek($offset, $whence)
128
    {
129 0
        return fseek($this->fp, $offset, $whence);
130
    }
131

132
    /**
133
     * @return bool
134
     */
135 0
    public function stream_flush()
136
    {
137 0
        return fflush($this->fp);
138
    }
139

140
    /**
141
     * @return array
142
     */
143 0
    public function stream_stat()
144
    {
145 0
        return fstat($this->fp);
146
    }
147
    // @codingStandardsIgnoreEnd
148

149
    /**
150
     * @param $path
151
     * @return bool
152
     */
153 0
    public function unlink($path)
154
    {
155 0
        return false;
156
    }
157

158
    /**
159
     * @param $path_from
160
     * @param $path_to
161
     * @return bool
162
     */
163 0
    public function rename($path_from, $path_to)
164
    {
165 0
        return false;
166
    }
167

168
    /**
169
     * @param $path
170
     * @param $mode
171
     * @param $options
172
     * @return bool
173
     */
174 0
    public function mkdir($path, $mode, $options)
175
    {
176 0
        return false;
177
    }
178

179
    /**
180
     * @param $path
181
     * @param $options
182
     * @return bool
183
     */
184 0
    public function rmdir($path, $options)
185
    {
186 0
        return false;
187
    }
188
}

Read our documentation on viewing source code .

Loading