1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Filesystem |
5
|
|
|
* |
6
|
|
|
* Platine Filesystem is file system abstraction layer extendable by adapters |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2020 Platine Filesystem |
11
|
|
|
* Copyright (c) 2019 Alex Sivka |
12
|
|
|
* |
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
15
|
|
|
* in the Software without restriction, including without limitation the rights |
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
18
|
|
|
* furnished to do so, subject to the following conditions: |
19
|
|
|
* |
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
21
|
|
|
* copies or substantial portions of the Software. |
22
|
|
|
* |
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
29
|
|
|
* SOFTWARE. |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @file File.php |
34
|
|
|
* |
35
|
|
|
* The local file class |
36
|
|
|
* |
37
|
|
|
* @package Platine\Filesystem\Adapter\Local |
38
|
|
|
* @author Platine Developers Team |
39
|
|
|
* @copyright Copyright (c) 2020 |
40
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
41
|
|
|
* @link https://www.platine-php.com |
42
|
|
|
* @version 1.0.0 |
43
|
|
|
* @filesource |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
declare(strict_types=1); |
47
|
|
|
|
48
|
|
|
namespace Platine\Filesystem\Adapter\Local; |
49
|
|
|
|
50
|
|
|
use Platine\Filesystem\DirectoryInterface; |
51
|
|
|
use Platine\Filesystem\FileInterface; |
52
|
|
|
use Platine\Stdlib\Helper\Path; |
53
|
|
|
use RuntimeException; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @class File |
57
|
|
|
* @package Platine\Filesystem\Adapter\Local |
58
|
|
|
*/ |
59
|
|
|
class File extends AbstractLocal implements FileInterface |
60
|
|
|
{ |
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function append(string $content): self |
65
|
|
|
{ |
66
|
|
|
file_put_contents($this->path, $content, FILE_APPEND); |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function copyTo( |
75
|
|
|
string|DirectoryInterface $directory, |
76
|
|
|
int $mode = 0775 |
77
|
|
|
): FileInterface { |
78
|
|
|
if (is_string($directory)) { |
79
|
|
|
$directory = $this->adapter->directory($directory); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $directory->createFile( |
83
|
|
|
$this->getName(), |
84
|
|
|
$this->read(), |
85
|
|
|
$mode |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function create(string $path, string $content = '', int $mode = 0775): FileInterface |
93
|
|
|
{ |
94
|
|
|
$file = $this->adapter->file($path)->write($content); |
95
|
|
|
$file->chmod($mode); |
96
|
|
|
|
97
|
|
|
return $file; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function delete(): self |
104
|
|
|
{ |
105
|
|
|
unlink($this->path); |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
|
|
public function getExtension(): string |
114
|
|
|
{ |
115
|
|
|
return pathinfo($this->path, PATHINFO_EXTENSION); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
public function getMime(): string |
122
|
|
|
{ |
123
|
|
|
$mime = mime_content_type($this->path); |
124
|
|
|
if (!$mime || $mime === 'application/octet-stream' || $mime === 'inode/x-empty') { |
125
|
|
|
$mime = Path::getMimeByExtension($this->getExtension()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $mime; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
|
|
public function getType(): string |
135
|
|
|
{ |
136
|
|
|
return 'file'; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
|
|
public function checksum(): string |
143
|
|
|
{ |
144
|
|
|
$res = md5_file($this->path); |
145
|
|
|
if ($res === false) { |
146
|
|
|
throw new RuntimeException(sprintf('can not get checksum of file [%s]', $this->path)); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $res; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* {@inheritdoc} |
154
|
|
|
*/ |
155
|
|
|
public function read(): string |
156
|
|
|
{ |
157
|
|
|
return (string) file_get_contents($this->path); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritdoc} |
162
|
|
|
*/ |
163
|
|
|
public function write(string $content): self |
164
|
|
|
{ |
165
|
|
|
file_put_contents($this->path, $content); |
166
|
|
|
|
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|