Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Mail;
4
 
5
use Closure;
6
use Illuminate\Container\Container;
7
use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory;
8
use Illuminate\Support\Traits\Macroable;
9
 
10
class Attachment
11
{
12
    use Macroable;
13
 
14
    /**
15
     * The attached file's filename.
16
     *
17
     * @var string|null
18
     */
19
    public $as;
20
 
21
    /**
22
     * The attached file's mime type.
23
     *
24
     * @var string|null
25
     */
26
    public $mime;
27
 
28
    /**
29
     * A callback that attaches the attachment to the mail message.
30
     *
31
     * @var \Closure
32
     */
33
    protected $resolver;
34
 
35
    /**
36
     * Create a mail attachment.
37
     *
38
     * @param  \Closure  $resolver
39
     * @return void
40
     */
41
    private function __construct(Closure $resolver)
42
    {
43
        $this->resolver = $resolver;
44
    }
45
 
46
    /**
47
     * Create a mail attachment from a path.
48
     *
49
     * @param  string  $path
50
     * @return static
51
     */
52
    public static function fromPath($path)
53
    {
54
        return new static(fn ($attachment, $pathStrategy) => $pathStrategy($path, $attachment));
55
    }
56
 
57
    /**
58
     * Create a mail attachment from in-memory data.
59
     *
60
     * @param  \Closure  $data
61
     * @param  string  $name
62
     * @return static
63
     */
64
    public static function fromData(Closure $data, $name)
65
    {
66
        return (new static(
67
            fn ($attachment, $pathStrategy, $dataStrategy) => $dataStrategy($data, $attachment)
68
        ))->as($name);
69
    }
70
 
71
    /**
72
     * Create a mail attachment from a file in the default storage disk.
73
     *
74
     * @param  string  $path
75
     * @return static
76
     */
77
    public static function fromStorage($path)
78
    {
79
        return static::fromStorageDisk(null, $path);
80
    }
81
 
82
    /**
83
     * Create a mail attachment from a file in the specified storage disk.
84
     *
85
     * @param  string|null  $disk
86
     * @param  string  $path
87
     * @return static
88
     */
89
    public static function fromStorageDisk($disk, $path)
90
    {
91
        return new static(function ($attachment, $pathStrategy, $dataStrategy) use ($disk, $path) {
92
            $storage = Container::getInstance()->make(
93
                FilesystemFactory::class
94
            )->disk($disk);
95
 
96
            $attachment
97
                ->as($attachment->as ?? basename($path))
98
                ->withMime($attachment->mime ?? $storage->mimeType($path));
99
 
100
            return $dataStrategy(fn () => $storage->get($path), $attachment);
101
        });
102
    }
103
 
104
    /**
105
     * Set the attached file's filename.
106
     *
107
     * @param  string  $name
108
     * @return $this
109
     */
110
    public function as($name)
111
    {
112
        $this->as = $name;
113
 
114
        return $this;
115
    }
116
 
117
    /**
118
     * Set the attached file's mime type.
119
     *
120
     * @param  string  $mime
121
     * @return $this
122
     */
123
    public function withMime($mime)
124
    {
125
        $this->mime = $mime;
126
 
127
        return $this;
128
    }
129
 
130
    /**
131
     * Attach the attachment with the given strategies.
132
     *
133
     * @param  \Closure  $pathStrategy
134
     * @param  \Closure  $dataStrategy
135
     * @return mixed
136
     */
137
    public function attachWith(Closure $pathStrategy, Closure $dataStrategy)
138
    {
139
        return ($this->resolver)($this, $pathStrategy, $dataStrategy);
140
    }
141
 
142
    /**
143
     * Attach the attachment to a built-in mail type.
144
     *
145
     * @param  \Illuminate\Mail\Mailable|\Illuminate\Mail\Message|\Illuminate\Notifications\Messages\MailMessage  $mail
146
     * @return mixed
147
     */
148
    public function attachTo($mail)
149
    {
150
        return $this->attachWith(
151
            fn ($path) => $mail->attach($path, ['as' => $this->as, 'mime' => $this->mime]),
152
            fn ($data) => $mail->attachData($data(), $this->as, ['mime' => $this->mime])
153
        );
154
    }
155
 
156
    /**
157
     * Determine if the given attachment is equivalent to this attachment.
158
     *
159
     * @param  \Illuminate\Mail\Attachment  $attachment
160
     * @return bool
161
     */
162
    public function isEquivalent(Attachment $attachment)
163
    {
164
        return $this->attachWith(
165
            fn ($path) => [$path, ['as' => $this->as, 'mime' => $this->mime]],
166
            fn ($data) => [$data(), ['as' => $this->as, 'mime' => $this->mime]],
167
        ) === $attachment->attachWith(
168
            fn ($path) => [$path, ['as' => $attachment->as, 'mime' => $attachment->mime]],
169
            fn ($data) => [$data(), ['as' => $attachment->as, 'mime' => $attachment->mime]],
170
        );
171
    }
172
}