1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
1 |
|
ar_pinp::allow( 'ar_content_files'); |
4
|
1 |
|
ar_pinp::allow( 'ar_content_filesFile' ); |
5
|
|
|
|
6
|
|
|
class ar_content_files extends arBase { |
7
|
|
|
} |
8
|
|
|
|
9
|
|
|
interface ar_content_filesFileInterface { |
|
|
|
|
10
|
|
|
public function read( $length ); |
11
|
|
|
public function readfile(); |
12
|
|
|
public function write( $string, $length = null ); |
13
|
|
|
public function eof(); |
14
|
|
|
public function size(); |
15
|
|
|
public function getc(); |
16
|
|
|
public function gets($length = null); |
17
|
|
|
public function getContents( $offset = -1, $maxLen = null ); |
18
|
|
|
public function seek( $offset, $whence = SEEK_SET ); |
19
|
|
|
public function tell(); |
20
|
|
|
public function truncate( $size ); |
21
|
|
|
public function rewind(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
class ar_content_filesFile extends arBase implements ar_content_filesFileInterface { |
|
|
|
|
25
|
|
|
|
26
|
|
|
private $resource = null; |
27
|
|
|
|
28
|
14 |
|
public function __construct( $resource ) { |
29
|
14 |
|
$this->resource = $resource; |
30
|
14 |
|
} |
31
|
|
|
|
32
|
14 |
|
public function __destruct() { |
33
|
14 |
|
if ($this->resource) { |
34
|
14 |
|
fclose( $this->resource ); |
35
|
14 |
|
} |
36
|
14 |
|
} |
37
|
|
|
|
38
|
|
|
public function read( $length ) { |
39
|
|
|
return fread( $this->resource, $length ); |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
public function readfile() { |
43
|
1 |
|
return fpassthru( $this->resource ); |
44
|
|
|
} |
45
|
|
|
|
46
|
13 |
|
public function getContents( $length = -1, $offset = -1 ) { |
47
|
13 |
|
$curpos = ftell( $this->resource ); |
48
|
13 |
|
$res = stream_get_contents( $this->resource, $length, $offset ); |
49
|
13 |
|
fseek( $this->resource, $curpos ); |
50
|
13 |
|
return $res; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function write( $string, $length = null ) { |
54
|
|
|
return fwrite( $this->resource, (string) $string, $length ); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function eof() { |
58
|
|
|
return feof( $this->resource ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function size() { |
62
|
|
|
$curpos = ftell( $this->resource ); |
63
|
|
|
fseek( $this->resource, 0, SEEK_END ); |
64
|
|
|
$size = ftell( $this->resource ); |
65
|
|
|
fseek( $this->resource, $curpos ); |
66
|
|
|
return $size; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getc() { |
70
|
|
|
return fgetc( $this->resource ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getcsv( $length=0, $delimiter=',', $enclosure='"', $escape='\\' ) { |
74
|
|
|
return fgetcsv( $this->resource, $length, $delimiter, $enclosure, $escape ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function gets( $length = null ) { |
78
|
|
|
return fgets( $this->resource, $length ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function seek( $offset, $whence = SEEK_SET ) { |
82
|
|
|
return fseek( $this->resource, $offset, $whence ); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function tell() { |
86
|
|
|
return ftell( $this->resource ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function truncate( $size ) { |
90
|
|
|
return ftruncate( $this->resource, $size ); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function rewind() { |
94
|
|
|
return rewind( $this->resource ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function getResource() { |
98
|
|
|
return $this->resource; |
99
|
|
|
} |
100
|
|
|
|
101
|
3 |
|
public function getMetaData() { |
102
|
3 |
|
return stream_get_meta_data($this->resource); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.