Skip to content

Commit 8fb0558

Browse files
authored
Merge 6a6c4a7 into acc1ce8
2 parents acc1ce8 + 6a6c4a7 commit 8fb0558

File tree

14 files changed

+121
-44
lines changed

14 files changed

+121
-44
lines changed

config/mailbox.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
11
<?php
22

3+
use BeyondCode\Mailbox\Drivers\Log;
4+
use BeyondCode\Mailbox\Drivers\MailCare;
5+
use BeyondCode\Mailbox\Drivers\Mailgun;
6+
use BeyondCode\Mailbox\Drivers\Postmark;
7+
use BeyondCode\Mailbox\Drivers\SendGrid;
8+
use BeyondCode\Mailbox\InboundEmail;
9+
310
return [
411

5-
/*
12+
/**
613
* The driver to use when listening for incoming emails.
714
* It defaults to the mail driver that you are using.
8-
*
9-
* Supported drivers: "log", "mailgun", "sendgrid", "postmark"
1015
*/
1116
'driver' => env('MAILBOX_DRIVER', 'log'),
1217

18+
/**
19+
* Supported driver keys and what they map to. Can map to a class or a callback.
20+
* If class is used, it must implement DriverInterface.
21+
*/
22+
'supported_drivers' => [
23+
'log' => Log::class,
24+
'mailgun' => Mailgun::class,
25+
'send_grid' => SendGrid::class,
26+
'mail_care' => MailCare::class,
27+
'postmark' => Postmark::class,
28+
],
29+
1330
/*
1431
* The model class to use when converting an incoming email to a message.
1532
* It must extend the default model class
1633
*/
17-
'model' => \BeyondCode\Mailbox\InboundEmail::class,
34+
'model' => InboundEmail::class,
1835

1936
/*
2037
* The path for driver specific routes. This is where
@@ -23,7 +40,7 @@
2340
*
2441
* For example: /laravel-mailbox/sendgrid/
2542
*/
26-
'path' => 'laravel-mailbox',
43+
'route_prefix' => 'laravel-mailbox',
2744

2845
/*
2946
* The amount of days that incoming emails should be stored in your

docs/extending/_index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
title: Extending
3+
order: 4
4+
---

docs/extending/extending.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Extending the package
2+
3+
## Drivers
4+
5+
You can add your own drivers to extend the package stock configuration.
6+
7+
1. Publish the configuration with ``php artisan vendor:publish``.
8+
1. List of currently supported drivers is listed under ``supported_drivers`` config key.
9+
1. Create a new driver class, making sure it implements ``DriverInterface``
10+
1. ``register()`` method should provide all the necessary steps to register
11+
a driver. For this package, this is usually done to expose routes for callbacks
12+
dynamically, i.e.:
13+
14+
```
15+
public function register()
16+
{
17+
Route::prefix(config('mailbox.route_prefix'))->group(function () {
18+
Route::post('/mailgun/mime', MailgunController::class);
19+
});
20+
}
21+
```
22+
23+
1. Controller needs to call a mailbox, and provide an ``InboundEmail`` instance
24+
to it:
25+
26+
```
27+
public function __invoke(MailgunRequest $request)
28+
{
29+
Mailbox::callMailboxes($request->email());
30+
}
31+
```
32+
33+
1. Making a form request is optional but recommended. The exposed ``email()``
34+
method takes in input parameters and converts it to ``InboundEmail`` instance:
35+
36+
```
37+
public function email()
38+
{
39+
return InboundEmail::fromMessage($this->get('email'));
40+
}
41+
```

src/Drivers/MailCare.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class MailCare implements DriverInterface
99
{
1010
public function register()
1111
{
12-
Route::prefix(config('mailbox.path'))->group(function () {
12+
Route::prefix(config('mailbox.route_prefix'))->group(function () {
1313
Route::post('/mailcare', MailCareController::class);
1414
});
1515
}

src/Drivers/Mailgun.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Mailgun implements DriverInterface
99
{
1010
public function register()
1111
{
12-
Route::prefix(config('mailbox.path'))->group(function () {
12+
Route::prefix(config('mailbox.route_prefix'))->group(function () {
1313
Route::post('/mailgun/mime', MailgunController::class);
1414
});
1515
}

src/Drivers/Postmark.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Postmark implements DriverInterface
99
{
1010
public function register()
1111
{
12-
Route::prefix(config('mailbox.path'))->group(function () {
12+
Route::prefix(config('mailbox.route_prefix'))->group(function () {
1313
Route::post('/postmark', PostmarkController::class);
1414
});
1515
}

src/Drivers/SendGrid.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class SendGrid implements DriverInterface
99
{
1010
public function register()
1111
{
12-
Route::prefix(config('mailbox.path'))->group(function () {
12+
Route::prefix(config('mailbox.route_prefix'))->group(function () {
1313
Route::post('/sendgrid', SendGridController::class);
1414
});
1515
}

src/Facades/Mailbox.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace BeyondCode\Mailbox\Facades;
44

5+
use BeyondCode\Mailbox\Routing\Router;
56
use Illuminate\Support\Facades\Facade;
67

8+
/**
9+
* @see Router
10+
*/
711
class Mailbox extends Facade
812
{
913
protected static function getFacadeAccessor()

src/Http/Controllers/MailCareController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class MailCareController extends Controller
1010
{
1111
public function __construct()
1212
{
13-
$this->middleware('laravel-mailbox');
13+
$this->middleware('laravel-mailbox-auth');
1414
}
1515

1616
public function __invoke(MailCareRequest $request)

src/Http/Controllers/PostmarkController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class PostmarkController extends Controller
1010
{
1111
public function __construct()
1212
{
13-
$this->middleware('laravel-mailbox');
13+
$this->middleware('laravel-mailbox-auth');
1414
}
1515

1616
public function __invoke(PostmarkRequest $request)

src/Http/Controllers/SendGridController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class SendGridController extends Controller
1010
{
1111
public function __construct()
1212
{
13-
$this->middleware('laravel-mailbox');
13+
$this->middleware('laravel-mailbox-auth');
1414
}
1515

1616
public function __invoke(SendGridRequest $request)

src/Http/Requests/MailCareRequest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public function validator()
1717

1818
public function email()
1919
{
20-
return InboundEmail::fromMessage($this->get('email'));
20+
/** @var InboundEmail $modelClass */
21+
$modelClass = config('mailbox.model');
22+
23+
return $modelClass::fromMessage($this->get('email'));
2124
}
2225
}

src/MailboxManager.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,47 @@
22

33
namespace BeyondCode\Mailbox;
44

5-
use BeyondCode\Mailbox\Drivers\Log;
6-
use BeyondCode\Mailbox\Drivers\MailCare;
7-
use BeyondCode\Mailbox\Drivers\Mailgun;
8-
use BeyondCode\Mailbox\Drivers\Postmark;
9-
use BeyondCode\Mailbox\Drivers\SendGrid;
5+
use BeyondCode\Mailbox\Drivers\DriverInterface;
6+
use Closure;
7+
use Illuminate\Contracts\Container\Container;
108
use Illuminate\Support\Manager;
119

1210
class MailboxManager extends Manager
1311
{
14-
public function mailbox($name = null)
12+
/**
13+
* Create a new manager instance.
14+
*
15+
* @param Container $container
16+
* @return void
17+
*/
18+
public function __construct(Container $container)
1519
{
16-
return $this->driver($name);
17-
}
20+
parent::__construct($container);
1821

19-
public function createLogDriver()
20-
{
21-
return new Log;
22+
$this->registerDrivers();
2223
}
2324

24-
public function createMailgunDriver()
25+
public function getDefaultDriver()
2526
{
26-
return new Mailgun;
27+
return $this->container['config']['mailbox.driver'];
2728
}
2829

29-
public function createSendGridDriver()
30+
protected function registerDrivers(): void
3031
{
31-
return new SendGrid;
32-
}
32+
$supported = config('mailbox.supported_drivers');
3333

34-
public function createMailCareDriver()
35-
{
36-
return new MailCare;
37-
}
34+
foreach ($supported as $driver => $mappedTo) {
35+
$callback = is_callable($mappedTo) ?
36+
$mappedTo : $this->registerDriverCallable($mappedTo);
3837

39-
public function createPostmarkDriver()
40-
{
41-
return new Postmark;
38+
$this->extend($driver, $callback);
39+
}
4240
}
4341

44-
public function getDefaultDriver()
42+
protected function registerDriverCallable(string $driver): Closure
4543
{
46-
return $this->container['config']['mailbox.driver'];
44+
return function () use ($driver): DriverInterface {
45+
return new $driver;
46+
};
4747
}
4848
}

src/MailboxServiceProvider.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace BeyondCode\Mailbox;
44

5-
use BeyondCode\Mailbox\Facades\Mailbox;
5+
use BeyondCode\Mailbox\Drivers\DriverInterface;
66
use BeyondCode\Mailbox\Http\Middleware\MailboxBasicAuthentication;
77
use BeyondCode\Mailbox\Routing\Router;
88
use Illuminate\Support\Facades\Route;
@@ -25,7 +25,7 @@ public function boot()
2525
__DIR__.'/../config/mailbox.php' => config_path('mailbox.php'),
2626
], 'config');
2727

28-
Route::aliasMiddleware('laravel-mailbox', MailboxBasicAuthentication::class);
28+
Route::aliasMiddleware('laravel-mailbox-auth', MailboxBasicAuthentication::class);
2929

3030
$this->commands([
3131
Console\CleanEmails::class,
@@ -45,13 +45,21 @@ public function register()
4545
return new Router($this->app);
4646
});
4747

48-
$this->app->singleton(MailboxManager::class, function () {
49-
return new MailboxManager($this->app);
50-
});
48+
$this->app->singleton(MailboxManager::class);
5149
}
5250

5351
protected function registerDriver()
5452
{
55-
Mailbox::mailbox()->register();
53+
/**
54+
* @var $manager MailboxManager
55+
*/
56+
$manager = app(MailboxManager::class);
57+
58+
/**
59+
* @var $driver DriverInterface
60+
*/
61+
$driver = $manager->driver();
62+
63+
$driver->register();
5664
}
5765
}

0 commit comments

Comments
 (0)