1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 12:54:05 +00:00

Move MailManager extension to MailTenancyBootstrapper

This commit is contained in:
lukinovec 2022-11-24 13:22:26 +01:00
parent d5e0e34087
commit d83cd1ddb4
3 changed files with 13 additions and 8 deletions

View file

@ -4,9 +4,12 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Bootstrappers;
use Illuminate\Mail\MailManager;
use Illuminate\Config\Repository;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;
use Illuminate\Foundation\Application;
use Stancl\Tenancy\TenancyMailManager;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
class MailTenancyBootstrapper implements TenancyBootstrapper
{
@ -33,7 +36,7 @@ class MailTenancyBootstrapper implements TenancyBootstrapper
],
];
public function __construct(protected Repository $config)
public function __construct(protected Repository $config, protected Application $app)
{
static::$mailer ??= $config->get('mail.default');
static::$credentialsMap = array_merge(static::$credentialsMap, static::$mapPresets[static::$mailer] ?? []);
@ -41,6 +44,12 @@ class MailTenancyBootstrapper implements TenancyBootstrapper
public function bootstrap(Tenant $tenant): void
{
// Use custom mail manager that resolves the mailers specified in its $tenantMailers static property
// Instead of getting the cached mailers from the $mailers property
$this->app->extend(MailManager::class, function (MailManager $mailManager) {
return new TenancyMailManager($this->app);
});
$this->setConfig($tenant);
}

View file

@ -70,12 +70,6 @@ class TenancyServiceProvider extends ServiceProvider
return new Commands\Seed($app['db']);
});
// Use custom mail manager that resolves the mailers specified in its $tenantMailers static property
// Instead of getting the cached mailers from the $mailers property
$this->app->extend(MailManager::class, function (MailManager $mailManager) {
return new TenancyMailManager($this->app);
});
$this->app->bind('globalCache', function ($app) {
return new CacheManager($app);
});

View file

@ -19,6 +19,8 @@ beforeEach(function() {
});
test('tenancy swaps the MailManager singleton for an instance of TenancyMailManager', function() {
tenancy()->initialize(Tenant::create());
expect(app(MailManager::class))->toBeInstanceOf(TenancyMailManager::class);
});