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

Delete TenancyMailManager, update tests

This commit is contained in:
lukinovec 2022-12-06 09:33:19 +01:00
parent cf5bffea25
commit fcd71f0b63
4 changed files with 51 additions and 78 deletions

View file

@ -9,7 +9,6 @@ use Illuminate\Foundation\Application;
use Illuminate\Mail\MailManager;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\TenancyMailManager;
class MailTenancyBootstrapper implements TenancyBootstrapper
{
@ -48,9 +47,7 @@ class MailTenancyBootstrapper implements TenancyBootstrapper
{
// 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->bindNewMailManagerInstance();
$this->setConfig($tenant);
}
@ -58,6 +55,15 @@ class MailTenancyBootstrapper implements TenancyBootstrapper
public function revert(): void
{
$this->unsetConfig();
$this->bindNewMailManagerInstance();
}
protected function bindNewMailManagerInstance()
{
$this->app->extend(MailManager::class, function (MailManager $mailManager) {
return new MailManager($this->app);
});
}
protected function setConfig(Tenant $tenant): void

View file

@ -1,41 +0,0 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy; // todo new Overrides namespace?
use Illuminate\Mail\MailManager;
use Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper;
/**
* This class (paired with the MailTenancyBootstrapper)
* allows tenants to have their custom mailer credentials.
*
* Tenancy swaps Laravel's MailManager singleton for an instance of this class,
* which overrides the manager's get method to always resolve
* the mailers specified in the static $tenantMailers property
* instead of getting them from the $mailers property where they're cached.
*
* @see MailTenancyBootstrapper
*/
class TenancyMailManager extends MailManager
{
/**
* Mailers to always resolve from the container (even when they're
* cached and available in the $mailers property).
*/
public static array $tenantMailers = [];
/**
* Override the get method so that the mailers in $tenantMailers always get resolved,
* even when they're cached and available in the $mailers property.
*/
protected function get($name)
{
if (in_array($name, static::$tenantMailers)) {
return $this->resolve($name);
}
return parent::get($name);
}
}