-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileTrait.php
More file actions
218 lines (174 loc) · 4.37 KB
/
Copy pathFileTrait.php
File metadata and controls
218 lines (174 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
declare(strict_types=1);
namespace ADT\Files\Entities;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Column;
use Exception;
trait FileTrait
{
#[ORM\Column(type: 'string')]
protected string $originalName;
#[ORM\Column(type: 'string', nullable: true)]
protected ?string $filename = null;
#[ORM\Column(type: 'integer', nullable: true)]
protected ?int $size = null;
#[Column(nullable: true)]
protected ?string $hash = null;
#[Column(nullable: true)]
protected ?string $mimeType = null;
#[Column(nullable: false)]
protected DateTimeImmutable $createdAt;
#[Column(nullable: false, options: ['default' => 0])]
protected bool $isPrivate = false;
protected ?string $temporaryFile = null;
protected ?string $temporaryContent = null;
protected ?string $stream = null;
protected string $path;
protected string $url;
/** @var callable */
protected $onAfterSave;
/** @var callable */
protected $onAfterDelete;
protected bool $ignoreMissingFile = false;
public function __construct()
{
$this->createdAt = new DateTimeImmutable();
}
public function getPath(): string
{
$this->filename; // intentionally, because of lazy ghost objects https://github.com/doctrine/DoctrineBundle/issues/1651#issuecomment-1684297751
return $this->path . '/' . $this->filename;
}
public function getContents(): string
{
$path = $this->getPath(); // intentionally, because of lazy ghost objects https://github.com/doctrine/DoctrineBundle/issues/1651#issuecomment-1684297751
if ($this->ignoreMissingFile) {
return (string) @file_get_contents($path);
}
return file_get_contents($path);
}
public function setBaseDirectoryPath(string $path): self
{
$this->path = $path;
return $this;
}
/**
* @throws Exception
*/
public function getUrl(): string
{
$this->filename; // intentionally, because of lazy ghost objects https://github.com/doctrine/DoctrineBundle/issues/1651#issuecomment-1684297751
if (!$this->url) {
throw new Exception('Url is not set.');
}
return $this->url . '/' . $this->filename;
}
public function setBaseDirectoryUrl(string $url): self
{
$this->url = $url;
return $this;
}
/**
* @throws Exception
*/
public function setTemporaryFile(string $temporaryFile, string $originalName): self
{
if (!is_file($temporaryFile)) {
throw new Exception($temporaryFile . ' is not a file.');
}
$this->temporaryFile = $temporaryFile;
$this->originalName = $originalName;
return $this;
}
public function getTemporaryFile(): ?string
{
return $this->temporaryFile;
}
public function setTemporaryContent(string $content, string $originalName): self
{
$this->temporaryContent = $content;
$this->originalName = $originalName;
return $this;
}
public function getTemporaryContent(): ?string
{
return $this->temporaryContent;
}
/**
* @throws Exception
*/
public function setStream(string $stream, string $originalName): self
{
if (!@fopen($stream, 'r')) {
throw new Exception($stream. ' is not a stream.');
}
$this->stream = $stream;
$this->originalName = $originalName;
return $this;
}
public function getStream(): ?string
{
return $this->stream;
}
public function isValid(): bool
{
return !is_null($this->temporaryFile) || !is_null($this->temporaryContent) || !is_null($this->stream);
}
public function setOnAfterSave(callable $callback): self
{
$this->onAfterSave = $callback;
return $this;
}
public function getOnAfterSave(): ?callable
{
return $this->onAfterSave;
}
public function setFilename(string $filename): self
{
$this->filename = $filename;
return $this;
}
public function getFilename(): string
{
return $this->filename;
}
public function getOriginalName(): string
{
return $this->originalName;
}
public function getOnAfterDelete(): ?callable
{
return $this->onAfterDelete;
}
public function getSize(): int
{
return $this->size;
}
public function getHash(): ?string
{
return $this->hash;
}
public function getMimeType(): ?string
{
return $this->mimeType;
}
public function setMimeType(string $mimeType): self
{
$this->mimeType = $mimeType;
return $this;
}
public function ignoreMissingFile(): void
{
$this->ignoreMissingFile = true;
}
public function getIsPrivate(): bool
{
return $this->isPrivate;
}
public function setIsPrivate(bool $isPrivate): self
{
$this->isPrivate = $isPrivate;
return $this;
}
}