|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Funivan\Cs\Fs; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @author Ivan Shcherbak <[email protected]> 2016 |
|
7
|
|
|
*/ |
|
8
|
|
|
class File { |
|
9
|
|
|
|
|
10
|
|
|
const STATUS_UNKNOWN = 0; |
|
11
|
|
|
|
|
12
|
|
|
const STATUS_ADDED = 1; |
|
13
|
|
|
|
|
14
|
|
|
const STATUS_COPIED = 2; |
|
15
|
|
|
|
|
16
|
|
|
const STATUS_MODIFIED = 3; |
|
17
|
|
|
|
|
18
|
|
|
const STATUS_RENAMED = 4; |
|
19
|
|
|
|
|
20
|
|
|
const STATUS_DELETED = 5; |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
private static $statusMap = [ |
|
27
|
|
|
self::STATUS_UNKNOWN => 'unknown', |
|
28
|
|
|
self::STATUS_ADDED => 'added', |
|
29
|
|
|
self::STATUS_COPIED => 'copied', |
|
30
|
|
|
self::STATUS_MODIFIED => 'modified', |
|
31
|
|
|
self::STATUS_RENAMED => 'renamed', |
|
32
|
|
|
self::STATUS_DELETED => 'deleted', |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The full path to the file |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
private $path; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The files status |
|
44
|
|
|
* |
|
45
|
|
|
* @var int |
|
46
|
|
|
*/ |
|
47
|
|
|
private $status; |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var FileContent |
|
52
|
|
|
*/ |
|
53
|
|
|
private $content; |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Initializes a new instance of the File class. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $status |
|
60
|
|
|
* @param string $filePath |
|
61
|
|
|
*/ |
|
62
|
74 |
|
public function __construct($filePath, $status) { |
|
63
|
74 |
|
if (!isset(self::$statusMap[$status])) { |
|
64
|
|
|
throw new \InvalidArgumentException('Invalid status'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
74 |
|
$this->status = (int) $status; |
|
68
|
74 |
|
$this->path = $filePath; |
|
69
|
74 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getName() { |
|
76
|
|
|
return basename($this->path); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getExtension() { |
|
84
|
|
|
return pathinfo($this->path, PATHINFO_EXTENSION); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getStatus() { |
|
92
|
|
|
return $this->status; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getStatusName() { |
|
100
|
|
|
return self::$statusMap[$this->status]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Return empty mime type on deleted file |
|
106
|
|
|
* |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getMimeType() { |
|
110
|
|
|
if ($this->status === self::STATUS_DELETED) { |
|
111
|
|
|
return ''; |
|
112
|
|
|
} |
|
113
|
|
|
return finfo_file(finfo_open(FILEINFO_MIME), $this->path); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
6 |
|
public function getPath() { |
|
121
|
6 |
|
return $this->path; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return FileContent |
|
127
|
|
|
*/ |
|
128
|
74 |
|
public function getContent() { |
|
129
|
|
|
|
|
130
|
74 |
|
if ($this->content === null) { |
|
131
|
74 |
|
$this->content = new FileContent(file_get_contents($this->path)); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
74 |
|
return $this->content; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Save content to the file |
|
140
|
|
|
* Perform this action only if content was changed |
|
141
|
|
|
* @void |
|
142
|
|
|
*/ |
|
143
|
|
|
public function save() { |
|
144
|
|
|
$content = $this->getContent(); |
|
145
|
|
|
if ($content->isChanged()) { |
|
146
|
|
|
file_put_contents($this->path, $content->get()); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|