mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 09:34:05 +00:00
* Replace MailManager singleton with an instance of a custom mail manager which always resolves the mailers instead of getting the cached ones
* Fix code style (php-cs-fixer)
* Add MailTenancyBootstrapper
* Add MailTenancyBootstrapper to tenancy.bootstrappers config (commented out)
* Fix code style (php-cs-fixer)
* Make credentials map a public static property
* Always resolve only the mailers specified in the mailersToNotCache public static property
* Fix typo in comment
* Update TenancyServiceProvider comment
* add todo
* Add comments to TenancyMailManager, rename property
* Remove the configKey array check
* Simplify bootstrap method
* Change $credentialsMap so that config keys are the keys, and the tenant property names are the values
* Rename $mailersToAlwaysResolve to $tenantMailers
* Update comment
* Update comment
* Rename variable in TenancyServiceProvider comment
* Scaffold tests
* Update comments after review
* Uncomment MailTenancyBootstrapper in config
* Use array_key_exists instead of null check
* Split config logic into methods
* Update mapping credentials
* Add tests for the added logic
* Fix code style (php-cs-fixer)
* Delete default 'smtp' mailer in $tenantMailers
* Add separate method to pick the appropriate mail credentials map preset
* Specify test name
* Move mail bootstrapper tests to BootstrapperTest
* Depend less on the default mailer by adding a static `$mailer` property
* Use static property for map presets
* Comment out MailTenancyBootstrapper from config
* Add return types to MailTenancyBootstrapper methods
* Update test name
* Move MailManager extension to MailTenancyBootstrapper
* Fix code style (php-cs-fixer)
* Update config reverting test
* Use `invade()` instead of ReflectionClass
* Fix constructor parameter formatting
* Delete TenancyMailManager, update tests
* Add return type
* Update comment
* Update MailTest
* Delete `group('mailer')`
* Delete bindNewMailManagerInstance()
* Delete remaining `group('mailer')`
* Fix comment
* Fix comment
Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
79 lines
2.1 KiB
PHP
79 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Stancl\Tenancy\Bootstrappers;
|
|
|
|
use Illuminate\Config\Repository;
|
|
use Illuminate\Foundation\Application;
|
|
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
|
use Stancl\Tenancy\Contracts\Tenant;
|
|
|
|
class MailTenancyBootstrapper implements TenancyBootstrapper
|
|
{
|
|
/**
|
|
* Tenant properties to be mapped to config (similarly to the TenantConfig feature).
|
|
*
|
|
* For example:
|
|
* [
|
|
* 'config.key.name' => 'tenant_property',
|
|
* ]
|
|
*/
|
|
public static array $credentialsMap = [];
|
|
|
|
public static string|null $mailer = null;
|
|
|
|
protected array $originalConfig = [];
|
|
|
|
public static array $mapPresets = [
|
|
'smtp' => [
|
|
'mail.mailers.smtp.host' => 'smtp_host',
|
|
'mail.mailers.smtp.port' => 'smtp_port',
|
|
'mail.mailers.smtp.username' => 'smtp_username',
|
|
'mail.mailers.smtp.password' => 'smtp_password',
|
|
],
|
|
];
|
|
|
|
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] ?? []);
|
|
}
|
|
|
|
public function bootstrap(Tenant $tenant): void
|
|
{
|
|
// Forget the mail manager instance to clear the cached mailers
|
|
$this->app->forgetInstance('mail.manager');
|
|
|
|
$this->setConfig($tenant);
|
|
}
|
|
|
|
public function revert(): void
|
|
{
|
|
$this->unsetConfig();
|
|
|
|
$this->app->forgetInstance('mail.manager');
|
|
}
|
|
|
|
protected function setConfig(Tenant $tenant): void
|
|
{
|
|
foreach (static::$credentialsMap as $configKey => $storageKey) {
|
|
$override = $tenant->$storageKey;
|
|
|
|
if (array_key_exists($storageKey, $tenant->getAttributes())) {
|
|
$this->originalConfig[$configKey] ??= $this->config->get($configKey);
|
|
|
|
$this->config->set($configKey, $override);
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function unsetConfig(): void
|
|
{
|
|
foreach ($this->originalConfig as $key => $value) {
|
|
$this->config->set($key, $value);
|
|
}
|
|
}
|
|
}
|