Skip to content

Commit

Permalink
changed HasMailAddress and ShouldQueue interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ericyzhu committed Oct 22, 2020
1 parent 34e1884 commit c3fa2d4
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 64 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ public function build()

## 发送邮件

若要发送邮件,使用 `Mail` 辅助类的 `to` 方法。`to` 方法接受邮件地址、邮件地址数组以及实现 `HasMailAddressInterface` 接口的实例或实例集合。如果传递一个邮件地址数组,那么它必须是包含字符串地址的一位数组或是包含具有 `email``name` 键的数组的二维数组,mailer 在设置收件人时会自动设置。一旦指定了收件人,就可以将 `Mailable` 类实例传递给 `send` 方法:
若要发送邮件,使用 `Mail` 辅助类的 `to` 方法。`to` 方法接受邮件地址、邮件地址数组以及实现 `HyperfExt/Contract/HasMailAddress` 接口的实例或实例集合。如果传递一个邮件地址数组,那么它必须是包含字符串地址的一位数组或是包含具有 `email``name` 键的数组的二维数组,mailer 在设置收件人时会自动设置。一旦指定了收件人,就可以将 `Mailable` 类实例传递给 `send` 方法:

```php
<?php
Expand Down Expand Up @@ -598,13 +598,13 @@ Mail::to($request->user())
#### 默认使用队列

如果你希望你的邮件类始终使用队列,您可以给邮件类 `HyperfExt\Mail\Contracts\ShouldQueueInterface` 接口,现在即使你调用了 `send` 方法,邮件依旧使用队列的方式发送。另外,如果需要将邮件推送到指定队列,可以设置在邮件类中设置 `queue` 属性。
如果你希望你的邮件类始终使用队列,您可以给邮件类 `HyperfExt\Contract\ShouldQueue` 接口,现在即使你调用了 `send` 方法,邮件依旧使用队列的方式发送。另外,如果需要将邮件推送到指定队列,可以设置在邮件类中设置 `queue` 属性。

```php
use HyperfExt\Mail\Contracts\ShouldQueueInterface;
use HyperfExt\Contract\ShouldQueue;
use HyperfExt\Mail\Mailable;

class OrderShipped extends Mailable implements ShouldQueueInterface
class OrderShipped extends Mailable implements ShouldQueue
{
/**
* 列队名称。
Expand Down Expand Up @@ -656,7 +656,7 @@ Mail::to($request->user())->locale('es')->send(

### 用户的个性化翻译

有时,应用程序会为每个用户存储不同的区域设置。通过在一个或多个模型上实现 `getPreferredLocale(): string` 方法,可以指示组件在发送邮件时使用此存储的区域设置:
有时,应用程序会为每个用户存储不同的区域设置。通过在一个或多个模型上实现 `HyperfExt/Contract/HasLocalePreference` 接口,可以指示组件在发送邮件时使用此存储的区域设置:

```php
class User extends Model
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"php": ">=7.2",
"ext-json": "*",
"ext-swoole": ">=4.5",
"hyperf-ext/contract": "^2.0",
"hyperf/async-queue": "^2.0",
"hyperf/command": "^2.0",
"hyperf/config": "^2.0",
Expand All @@ -40,7 +41,6 @@
"hyperf/event": "^2.0",
"hyperf/filesystem": "^2.0",
"hyperf/framework": "^2.0",
"hyperf/guzzle": "^2.0",
"hyperf/translation": "^2.0",
"hyperf/view": "^2.0",
"swiftmailer/swiftmailer": "^6.0"
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/stubs/mail.stub
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ declare(strict_types=1);
*/
namespace %NAMESPACE%;

use HyperfExt\Mail\Contracts\ShouldQueueInterface;
use HyperfExt\Contract\ShouldQueue;
use HyperfExt\Mail\Mailable;

class %CLASS% extends Mailable implements ShouldQueueInterface
class %CLASS% extends Mailable implements ShouldQueue
{
/**
* Create a new message instance.
Expand Down
24 changes: 0 additions & 24 deletions src/Contracts/HasMailAddressInterface.php

This file was deleted.

16 changes: 8 additions & 8 deletions src/Contracts/MailableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface MailableInterface
/**
* Set the sender of the message.
*
* @param \HyperfExt\Mail\Contracts\HasMailAddressInterface|string $address
* @param \HyperfExt\Contract\HasMailAddress|string $address
*
* @return $this
*/
Expand All @@ -24,7 +24,7 @@ public function from($address, ?string $name = null);
/**
* Set the "reply to" address of the message.
*
* @param \HyperfExt\Mail\Contracts\HasMailAddressInterface|string $address
* @param \HyperfExt\Contract\HasMailAddress|string $address
*
* @return $this
*/
Expand All @@ -33,7 +33,7 @@ public function replyTo($address, ?string $name = null);
/**
* Add the recipient of the message.
*
* @param array|\Hyperf\Utils\Collection|\HyperfExt\Mail\Contracts\HasMailAddressInterface|string $address
* @param array|\Hyperf\Utils\Collection|\HyperfExt\Contract\HasMailAddress|string $address
*
* @return $this
*/
Expand All @@ -42,14 +42,14 @@ public function cc($address, ?string $name = null);
/**
* Determine if the given recipient is set on the mailable.
*
* @param \HyperfExt\Mail\Contracts\HasMailAddressInterface|string $address
* @param \HyperfExt\Contract\HasMailAddress|string $address
*/
public function hasCc($address, ?string $name = null): bool;

/**
* Add the recipients of the message.
*
* @param array|\Hyperf\Utils\Collection|\HyperfExt\Mail\Contracts\HasMailAddressInterface|string $address
* @param array|\Hyperf\Utils\Collection|\HyperfExt\Contract\HasMailAddress|string $address
*
* @return $this
*/
Expand All @@ -58,14 +58,14 @@ public function bcc($address, ?string $name = null);
/**
* Determine if the given recipient is set on the mailable.
*
* @param \HyperfExt\Mail\Contracts\HasMailAddressInterface|string $address
* @param \HyperfExt\Contract\HasMailAddress|string $address
*/
public function hasBcc($address, ?string $name = null): bool;

/**
* Add the recipients of the message.
*
* @param array|\Hyperf\Utils\Collection|\HyperfExt\Mail\Contracts\HasMailAddressInterface|string $address
* @param array|\Hyperf\Utils\Collection|\HyperfExt\Contract\HasMailAddress|string $address
*
* @return $this
*/
Expand All @@ -74,7 +74,7 @@ public function to($address, ?string $name = null);
/**
* Determine if the given recipient is set on the mailable.
*
* @param \HyperfExt\Mail\Contracts\HasMailAddressInterface|string $address
* @param \HyperfExt\Contract\HasMailAddress|string $address
*/
public function hasTo($address, ?string $name = null): bool;

Expand Down
15 changes: 0 additions & 15 deletions src/Contracts/ShouldQueueInterface.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/MailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
use Hyperf\Logger\LoggerFactory;
use Hyperf\Utils\Arr;
use Hyperf\Utils\Str;
use HyperfExt\Contract\ShouldQueue;
use HyperfExt\Mail\Concerns\PendingMailable;
use HyperfExt\Mail\Contracts\MailableInterface;
use HyperfExt\Mail\Contracts\MailerInterface;
use HyperfExt\Mail\Contracts\MailManagerInterface;
use HyperfExt\Mail\Contracts\ShouldQueueInterface;
use InvalidArgumentException;
use Psr\Container\ContainerInterface;
use Swift_DependencyContainer;
Expand Down Expand Up @@ -100,7 +100,7 @@ public function get(string $name): MailerInterface
*/
public function send(MailableInterface $mailable)
{
return $mailable instanceof ShouldQueueInterface
return $mailable instanceof ShouldQueue
? $mailable->queue()
: $mailable->send($this);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use Hyperf\Utils\Str;
use Hyperf\Utils\Traits\ForwardsCalls;
use Hyperf\View\RenderInterface;
use HyperfExt\Mail\Contracts\HasMailAddressInterface;
use HyperfExt\Contract\HasMailAddress;
use HyperfExt\Mail\Contracts\MailableInterface;
use HyperfExt\Mail\Contracts\MailerInterface;
use HyperfExt\Mail\Contracts\MailManagerInterface;
Expand Down Expand Up @@ -489,7 +489,7 @@ protected function arrayizeAddress($address, ?string $name = null): array
'address' => $item['address'],
'name' => $item['name'] ?? null,
];
} elseif (is_string($item) or $item instanceof HasMailAddressInterface) {
} elseif (is_string($item) or $item instanceof HasMailAddress) {
$addresses[] = $this->normalizeRecipient($item);
}
}
Expand All @@ -502,11 +502,11 @@ protected function arrayizeAddress($address, ?string $name = null): array
/**
* Convert the given recipient into an object.
*
* @param HasMailAddressInterface|string $address
* @param HasMailAddress|string $address
*/
protected function normalizeRecipient($address, ?string $name = null): array
{
if ($address instanceof HasMailAddressInterface) {
if ($address instanceof HasMailAddress) {
$name = $address->getMailAddressDisplayName();
$address = $address->getMailAddress();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
namespace HyperfExt\Mail;

use Hyperf\Utils\Traits\Macroable;
use HyperfExt\Contract\ShouldQueue;
use HyperfExt\Mail\Concerns\PendingMailable;
use HyperfExt\Mail\Contracts\MailableInterface;
use HyperfExt\Mail\Contracts\MailerInterface;
use HyperfExt\Mail\Contracts\ShouldQueueInterface;
use HyperfExt\Mail\Events\MessageSending;
use HyperfExt\Mail\Events\MessageSent;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -189,7 +189,7 @@ public function sendNow(MailableInterface $mailable): ?array

public function send(MailableInterface $mailable)
{
return $mailable instanceof ShouldQueueInterface
return $mailable instanceof ShouldQueue
? $mailable->mailer($this->name)->queue()
: $mailable->mailer($this->name)->send($this);
}
Expand Down
3 changes: 2 additions & 1 deletion src/PendingMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace HyperfExt\Mail;

use Hyperf\Utils\ApplicationContext;
use HyperfExt\Contract\HasLocalePreference;
use HyperfExt\Mail\Contracts\MailableInterface;
use HyperfExt\Mail\Contracts\MailManagerInterface;

Expand Down Expand Up @@ -85,7 +86,7 @@ public function to($users)

if (! $this->locale &&
is_object($users) &&
method_exists($users, 'getPreferredLocale') &&
$users instanceof HasLocalePreference &&
is_string($locale = $users->getPreferredLocale())
) {
$this->locale($locale);
Expand Down

0 comments on commit c3fa2d4

Please sign in to comment.