-
Notifications
You must be signed in to change notification settings - Fork 27
/
Attachment.php
203 lines (180 loc) · 5.12 KB
/
Attachment.php
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
<?php
namespace Illuminate\Mail;
use Closure;
use Illuminate\Container\Container;
use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory;
use Illuminate\Support\Traits\Macroable;
use RuntimeException;
class Attachment
{
use Macroable;
/**
* The attached file's filename.
*
* @var string|null
*/
public $as;
/**
* The attached file's mime type.
*
* @var string|null
*/
public $mime;
/**
* A callback that attaches the attachment to the mail message.
*
* @var \Closure
*/
protected $resolver;
/**
* Create a mail attachment.
*
* @param \Closure $resolver
* @return void
*/
private function __construct(Closure $resolver)
{
$this->resolver = $resolver;
}
/**
* Create a mail attachment from a path.
*
* @param string $path
* @return static
*/
public static function fromPath($path)
{
return new static(fn ($attachment, $pathStrategy) => $pathStrategy($path, $attachment));
}
/**
* Create a mail attachment from a URL.
*
* @param string $url
* @return static
*/
public static function fromUrl($url)
{
return static::fromPath($url);
}
/**
* Create a mail attachment from in-memory data.
*
* @param \Closure $data
* @param string|null $name
* @return static
*/
public static function fromData(Closure $data, $name = null)
{
return (new static(
fn ($attachment, $pathStrategy, $dataStrategy) => $dataStrategy($data, $attachment)
))->as($name);
}
/**
* Create a mail attachment from a file in the default storage disk.
*
* @param string $path
* @return static
*/
public static function fromStorage($path)
{
return static::fromStorageDisk(null, $path);
}
/**
* Create a mail attachment from a file in the specified storage disk.
*
* @param string|null $disk
* @param string $path
* @return static
*/
public static function fromStorageDisk($disk, $path)
{
return new static(function ($attachment, $pathStrategy, $dataStrategy) use ($disk, $path) {
$storage = Container::getInstance()->make(
FilesystemFactory::class
)->disk($disk);
$attachment
->as($attachment->as ?? basename($path))
->withMime($attachment->mime ?? $storage->mimeType($path));
return $dataStrategy(fn () => $storage->get($path), $attachment);
});
}
/**
* Set the attached file's filename.
*
* @param string|null $name
* @return $this
*/
public function as($name)
{
$this->as = $name;
return $this;
}
/**
* Set the attached file's mime type.
*
* @param string $mime
* @return $this
*/
public function withMime($mime)
{
$this->mime = $mime;
return $this;
}
/**
* Attach the attachment with the given strategies.
*
* @param \Closure $pathStrategy
* @param \Closure $dataStrategy
* @return mixed
*/
public function attachWith(Closure $pathStrategy, Closure $dataStrategy)
{
return ($this->resolver)($this, $pathStrategy, $dataStrategy);
}
/**
* Attach the attachment to a built-in mail type.
*
* @param \Illuminate\Mail\Mailable|\Illuminate\Mail\Message|\Illuminate\Notifications\Messages\MailMessage $mail
* @param array $options
* @return mixed
*/
public function attachTo($mail, $options = [])
{
return $this->attachWith(
fn ($path) => $mail->attach($path, [
'as' => $options['as'] ?? $this->as,
'mime' => $options['mime'] ?? $this->mime,
]),
function ($data) use ($mail, $options) {
$options = [
'as' => $options['as'] ?? $this->as,
'mime' => $options['mime'] ?? $this->mime,
];
if ($options['as'] === null) {
throw new RuntimeException('Attachment requires a filename to be specified.');
}
return $mail->attachData($data(), $options['as'], ['mime' => $options['mime']]);
}
);
}
/**
* Determine if the given attachment is equivalent to this attachment.
*
* @param \Illuminate\Mail\Attachment $attachment
* @param array $options
* @return bool
*/
public function isEquivalent(Attachment $attachment, $options = [])
{
return with([
'as' => $options['as'] ?? $attachment->as,
'mime' => $options['mime'] ?? $attachment->mime,
], fn ($options) => $this->attachWith(
fn ($path) => [$path, ['as' => $this->as, 'mime' => $this->mime]],
fn ($data) => [$data(), ['as' => $this->as, 'mime' => $this->mime]],
) === $attachment->attachWith(
fn ($path) => [$path, $options],
fn ($data) => [$data(), $options],
));
}
}