Commit | Line | Data |
---|---|---|
158bd3ca | 1 | <?php |
a9229942 | 2 | |
158bd3ca | 3 | namespace wcf\system\io; |
a9229942 | 4 | |
158bd3ca TD |
5 | use wcf\system\exception\SystemException; |
6 | ||
7 | /** | |
8 | * The File class handles all file operations. | |
a9229942 | 9 | * |
158bd3ca TD |
10 | * Example: |
11 | * using php functions: | |
12 | * $fp = fopen('filename', 'wb'); | |
13 | * fwrite($fp, '...'); | |
14 | * fclose($fp); | |
a9229942 | 15 | * |
158bd3ca TD |
16 | * using this class: |
17 | * $file = new File('filename'); | |
18 | * $file->write('...'); | |
19 | * $file->close(); | |
a9229942 TD |
20 | * |
21 | * @author Marcel Werk | |
22 | * @copyright 2001-2019 WoltLab GmbH | |
23 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
a9229942 TD |
24 | * |
25 | * @method bool close() | |
26 | * @method bool eof() | |
27 | * @method int filesize() | |
28 | * @method string gets($length = null) | |
29 | * @method resource open($mode, $use_include_path = false, $context = null) | |
30 | * @method int puts($string, $length = null) alias of `write` | |
31 | * @method string read($length) | |
32 | * @method int seek($offset, $whence = SEEK_SET) | |
33 | * @method array stat() | |
34 | * @method int tell() | |
35 | * @method bool touch($time = 0, $atime = 0) note: default value of `$time` actually is `time()` | |
36 | * @method int write($string, $length = null) | |
158bd3ca | 37 | */ |
a9229942 TD |
38 | class File |
39 | { | |
40 | /** | |
41 | * file pointer resource | |
42 | * @var resource | |
43 | */ | |
44 | protected $resource; | |
45 | ||
46 | /** | |
47 | * filename | |
48 | * @var string | |
49 | */ | |
50 | protected $filename = ''; | |
51 | ||
52 | /** | |
53 | * Opens a new file. | |
54 | * | |
55 | * @param string $filename | |
56 | * @param string $mode | |
57 | * @param array $options | |
58 | * @throws SystemException | |
59 | */ | |
60 | public function __construct($filename, $mode = 'wb', $options = []) | |
61 | { | |
62 | $this->filename = $filename; | |
63 | if (!empty($options)) { | |
64 | $context = \stream_context_create($options); | |
65 | $this->resource = \fopen($filename, $mode, false, $context); | |
66 | } else { | |
67 | $this->resource = \fopen($filename, $mode); | |
68 | } | |
69 | if ($this->resource === false) { | |
70 | throw new SystemException('Can not open file ' . $filename); | |
71 | } | |
72 | } | |
73 | ||
74 | /** | |
75 | * Calls the specified function on the open file. | |
76 | * Do not call this function directly. Use $file->write('') instead. | |
77 | * | |
78 | * @param string $function | |
79 | * @param array $arguments | |
80 | * @return mixed | |
81 | * @throws SystemException | |
82 | */ | |
83 | public function __call($function, $arguments) | |
84 | { | |
85 | if (\function_exists('f' . $function)) { | |
86 | \array_unshift($arguments, $this->resource); | |
87 | ||
88 | return \call_user_func_array('f' . $function, $arguments); | |
89 | } elseif (\function_exists($function)) { | |
90 | \array_unshift($arguments, $this->filename); | |
91 | ||
92 | return \call_user_func_array($function, $arguments); | |
93 | } else { | |
94 | throw new SystemException('Can not call file method ' . $function); | |
95 | } | |
96 | } | |
dcb3a44c | 97 | } |