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