diff --git a/config/services.php b/config/services.php index 96f3678..6d37e6a 100644 --- a/config/services.php +++ b/config/services.php @@ -79,6 +79,7 @@ '$settingsHydrator' => service('jbtronics.settings.settings_hydrator'), '$settingsResetter' => service('jbtronics.settings.settings_resetter'), '$settingsValidator' => service('jbtronics.settings.settings_validator'), + '$settingsRegistry' => service('jbtronics.settings.settings_registry'), ]) ; $services->alias(SettingsManagerInterface::class, 'jbtronics.settings.settings_manager'); diff --git a/src/Manager/SettingsManager.php b/src/Manager/SettingsManager.php index d5ee25a..9b456db 100644 --- a/src/Manager/SettingsManager.php +++ b/src/Manager/SettingsManager.php @@ -39,6 +39,7 @@ public function __construct( private readonly SettingsHydratorInterface $settingsHydrator, private readonly SettingsResetterInterface $settingsResetter, private readonly SettingsValidatorInterface $settingsValidator, + private readonly SettingsRegistryInterface $settingsRegistry, ) { @@ -48,6 +49,11 @@ public function __construct( public function get(string $settingsClass): object { + //If not a class name is given, try to resolve the name via SettingsRegistry + if (!class_exists($settingsClass)) { + $settingsClass = $this->settingsRegistry->getSettingsClassByName($settingsClass); + } + //Check if the settings class is already in memory if (isset($this->settings_by_class[$settingsClass])) { return $this->settings_by_class[$settingsClass]; diff --git a/src/Manager/SettingsManagerInterface.php b/src/Manager/SettingsManagerInterface.php index 198ff72..70624c0 100644 --- a/src/Manager/SettingsManagerInterface.php +++ b/src/Manager/SettingsManagerInterface.php @@ -33,10 +33,10 @@ interface SettingsManagerInterface { /** * Returns the value filled instance of the given settings class. - * @param string $settingsClass + * @param string $settingsClass The settings class to get the instance for. This can either be a class string, or the short name of the settings class. * @return object * @template T of object - * @phpstan-param class-string $settingsClass + * @phpstan-param class-string|string $settingsClass * @phpstan-return T */ public function get(string $settingsClass): object; diff --git a/tests/Manager/SettingsManagerTest.php b/tests/Manager/SettingsManagerTest.php new file mode 100644 index 0000000..ae67224 --- /dev/null +++ b/tests/Manager/SettingsManagerTest.php @@ -0,0 +1,99 @@ +service = self::getContainer()->get(SettingsManagerInterface::class); + } + + public function testGet(): void + { + //Test if we can get the settings class by classname + /** @var SimpleSettings $settings */ + $settings = $this->service->get(SimpleSettings::class); + $this->assertInstanceOf(SimpleSettings::class, $settings); + + //Try to change a value + $settings->setValue1('changed'); + + //Test if we can get the settings class by short name + $settings2 = $this->service->get('simple'); + $this->assertInstanceOf(SimpleSettings::class, $settings2); + //Must be the same instance of the class + $this->assertSame($settings, $settings2); + + //Test if the value is changed + $this->assertEquals('changed', $settings2->getValue1()); + } + + public function testResetToDefaultValues(): void + { + /** @var SimpleSettings $settings */ + $settings = $this->service->get(SimpleSettings::class); + $settings->setValue1('changed'); + + $this->service->resetToDefaultValues($settings); + + $this->assertEquals('default', $settings->getValue1()); + } + + + + public function testSaveAndReload(): void + { + /** @var SimpleSettings $settings */ + $settings = $this->service->get(SimpleSettings::class); + $settings->setValue1('changed'); + + //Save the settings + $this->service->save($settings); + + //Change the value again + $settings->setValue1('changed again'); + + //Reload the settings + $this->service->reload($settings); + + //And the value should be the one, which we saved before + $this->assertEquals('changed', $settings->getValue1()); + } +}