1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the `liip/LiipImagineBundle` project. |
5
|
|
|
* |
6
|
|
|
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Liip\ImagineBundle\File; |
13
|
|
|
|
14
|
|
|
use Liip\ImagineBundle\Exception\File\FileOperationException; |
15
|
|
|
use Liip\ImagineBundle\File\Lock\LockInvokable; |
16
|
|
|
use SR\Interpreter\Interpreter; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @internal |
20
|
|
|
* |
21
|
|
|
* @author Rob Frawley 2nd <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
abstract class AbstractFilePath extends AbstractFileBlob |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var \SplFileInfo|null |
27
|
|
|
*/ |
28
|
|
|
protected $file; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function __toString(): string |
34
|
|
|
{ |
35
|
|
|
return $this->hasFile() ? $this->getFile()->getPathname() : ''; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public function getFile(): ?\SplFileInfo |
42
|
|
|
{ |
43
|
|
|
return $this->file; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function hasFile(): bool |
50
|
|
|
{ |
51
|
|
|
return null !== $this->file; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function fileExists(): bool |
58
|
|
|
{ |
59
|
|
|
return $this->hasFile() && file_exists($this->getFile()->getPathname()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function isFileReadable(): bool |
66
|
|
|
{ |
67
|
|
|
return $this->fileExists() && is_readable($this->getFile()->getPathname()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function isFileWritable(): bool |
74
|
|
|
{ |
75
|
|
|
return ($this->fileExists() && is_writable($this->getFile()->getPathname())) |
76
|
|
|
|| (!$this->fileExists() && $this->hasFile() && is_writable($this->getFile()->getPath())); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return self |
81
|
|
|
*/ |
82
|
|
|
public function remove(): self |
83
|
|
|
{ |
84
|
|
|
LockInvokable::blocking($this, function (): void { |
85
|
|
|
if ($this->fileExists() && (false === $this->isFileWritable() || false === @unlink($this->getFile()->getPathname()))) { |
86
|
|
|
throw new FileOperationException( |
87
|
|
|
'Failed to remove file "%s": %s', $this->file->getPathname(), Interpreter::error()->text() |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
}); |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string|null |
97
|
|
|
*/ |
98
|
|
|
protected function doGetContents(): ?string |
99
|
|
|
{ |
100
|
|
|
if (!$this->hasFile()) { |
101
|
|
|
return null; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return LockInvokable::blocking($this, function (): ?string { |
105
|
|
|
if (false !== $contents = @file_get_contents($this->getFile()->getPathname())) { |
106
|
|
|
return $contents; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return null; |
110
|
|
|
}); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $contents |
115
|
|
|
* @param bool $append |
116
|
|
|
* |
117
|
|
|
* @throws FileOperationException |
118
|
|
|
*/ |
119
|
|
|
protected function doSetContents(string $contents, bool $append): void |
120
|
|
|
{ |
121
|
|
|
LockInvokable::blocking($this, function () use ($contents, $append): void { |
122
|
|
|
self::makePathIfNotExists($this->getFile()->getPath()); |
123
|
|
|
self::dumpContentsForFile($this->getFile()->getPathname(), $contents, $append); |
124
|
|
|
}); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $path |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
protected static function makePathIfNotExists(string $path): string |
133
|
|
|
{ |
134
|
|
|
if (false === is_dir($path) && false === @mkdir($path, 0777, true) && false === is_dir($path)) { |
135
|
|
|
throw new FileOperationException( |
136
|
|
|
'Failed to create file "%s": %s', $path, Interpreter::error()->text() |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if (false !== $real = @realpath($path)) { |
141
|
|
|
return $real; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $path; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $file |
149
|
|
|
* @param string $contents |
150
|
|
|
* @param bool $append |
151
|
|
|
*/ |
152
|
|
|
private static function dumpContentsForFile(string $file, string $contents, bool $append): void |
153
|
|
|
{ |
154
|
|
|
if (false === @file_put_contents($file, $contents, $append ? FILE_APPEND : 0)) { |
155
|
|
|
throw new FileOperationException( |
156
|
|
|
'Failed to write contents of "%s": %s.', $file, Interpreter::error()->text() |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|