-
Notifications
You must be signed in to change notification settings - Fork 11.1k
/
Attachment.php
155 lines (136 loc) · 3.58 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
<?php
namespace Illuminate\Mail;
use Closure;
use Illuminate\Container\Container;
use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory;
use Illuminate\Support\Traits\Macroable;
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 in-memory data.
*
* @param \Closure $data
* @param string $name
* @return static
*/
public static function fromData(Closure $data, $name)
{
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));
$dataStrategy(fn () => $storage->get($path), $attachment);
});
}
/**
* Set the attached file's filename.
*
* @param string $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
* @return mixed
*/
public function attachTo($mail)
{
return $this->attachWith(
fn ($path) => $mail->attach($path, ['as' => $this->as, 'mime' => $this->mime]),
fn ($data) => $mail->attachData($data(), $this->as, ['mime' => $this->mime])
);
}
}