From 6bf4ea2131dd57d9229f42956dd5e9c6d3d48c84 Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Tue, 8 Nov 2022 12:59:27 +0100 Subject: [PATCH] Add interfaces and traits for registries --- src/Singleton/IRegistry.php | 47 ++++++++++++++++++++++ src/Singleton/IRegistryDefault.php | 40 +++++++++++++++++++ src/Singleton/ISingleton.php | 4 +- src/Singleton/TRegistry.php | 21 ++++++++++ src/Singleton/TRegistryBase.php | 62 ++++++++++++++++++++++++++++++ src/Singleton/TRegistryDefault.php | 44 +++++++++++++++++++++ src/Singleton/TSingleton.php | 18 ++++----- tests/registry.php | 38 ++++++++++++++++++ tests/registry_default.php | 39 +++++++++++++++++++ tests/{test.php => singleton.php} | 14 ++++--- 10 files changed, 311 insertions(+), 16 deletions(-) create mode 100644 src/Singleton/IRegistry.php create mode 100644 src/Singleton/IRegistryDefault.php create mode 100644 src/Singleton/TRegistry.php create mode 100644 src/Singleton/TRegistryBase.php create mode 100644 src/Singleton/TRegistryDefault.php create mode 100644 tests/registry.php create mode 100644 tests/registry_default.php rename tests/{test.php => singleton.php} (55%) diff --git a/src/Singleton/IRegistry.php b/src/Singleton/IRegistry.php new file mode 100644 index 0000000..212fa0e --- /dev/null +++ b/src/Singleton/IRegistry.php @@ -0,0 +1,47 @@ +WARNING : The entry name MUST already exists + * + * @param string $name The new default name + * + * @return void + * + * @throws InvalidArgumentException If the entry name doesn't exists + */ + public static function setDefaultEntryName (string $name): void; + + /** + * @inheritDoc + * + * @throws InvalidArgumentException If the entry name doesn't exist or no default entry name + * @throws Throwable If instantiating failed + */ + public static function getInstance (?string $name = null): self; +} \ No newline at end of file diff --git a/src/Singleton/ISingleton.php b/src/Singleton/ISingleton.php index 4e35c85..06b57f9 100644 --- a/src/Singleton/ISingleton.php +++ b/src/Singleton/ISingleton.php @@ -3,7 +3,9 @@ namespace jrosset\Singleton; /** - * Interface for class that must implements a singleton + * Interface for class implementing a singleton + * + * @see TSingleton for common implementation */ interface ISingleton { /** diff --git a/src/Singleton/TRegistry.php b/src/Singleton/TRegistry.php new file mode 100644 index 0000000..22c327d --- /dev/null +++ b/src/Singleton/TRegistry.php @@ -0,0 +1,21 @@ +cpt = $cpt; + } + + public function getCpt (): int { + return $this->cpt; + } + public function increment (): void { + $this->cpt++; + } +} + +RegistryTest::addEntry('cpt_1', 0); +RegistryTest::addEntry('cpt_2', 5); + +var_dump(RegistryTest::getInstance('cpt_1')->getCpt()); +var_dump(RegistryTest::getInstance('cpt_2')->getCpt()); + +RegistryTest::getInstance('cpt_1')->increment(); +RegistryTest::getInstance('cpt_2')->increment(); + +var_dump(RegistryTest::getInstance('cpt_1')->getCpt()); +var_dump(RegistryTest::getInstance('cpt_2')->getCpt()); \ No newline at end of file diff --git a/tests/registry_default.php b/tests/registry_default.php new file mode 100644 index 0000000..c93eec1 --- /dev/null +++ b/tests/registry_default.php @@ -0,0 +1,39 @@ +cpt = $cpt; + } + + public function getCpt (): int { + return $this->cpt; + } + public function increment (): void { + $this->cpt++; + } +} + +RegistryDefaultTest::addEntry('cpt_1', -5); +RegistryDefaultTest::addEntry('cpt_2', 12); +RegistryDefaultTest::setDefaultEntryName('cpt_1'); + +var_dump(RegistryDefaultTest::getInstance()->getCpt()); +var_dump(RegistryDefaultTest::getInstance('cpt_2')->getCpt()); + +RegistryDefaultTest::getInstance()->increment(); +RegistryDefaultTest::getInstance('cpt_2')->increment(); + +var_dump(RegistryDefaultTest::getInstance()->getCpt()); +var_dump(RegistryDefaultTest::getInstance('cpt_2')->getCpt()); \ No newline at end of file diff --git a/tests/test.php b/tests/singleton.php similarity index 55% rename from tests/test.php rename to tests/singleton.php index db564b8..cf1e872 100644 --- a/tests/test.php +++ b/tests/singleton.php @@ -6,10 +6,14 @@ require_once __DIR__ . '/../vendor/autoload.php'; use jrosset\Singleton\ISingleton; use jrosset\Singleton\TSingleton; -class Test implements ISingleton { +class SingletonTest implements ISingleton { use TSingleton; - private int $cpt = 0; + private int $cpt; + + private function __construct () { + $this->cpt = 0; + } public function getCpt (): int { return $this->cpt; @@ -19,6 +23,6 @@ class Test implements ISingleton { } } -var_dump(Test::getInstance()->getCpt()); -Test::getInstance()->increment(); -var_dump(Test::getInstance()->getCpt()); +var_dump(SingletonTest::getInstance()->getCpt()); +SingletonTest::getInstance()->increment(); +var_dump(SingletonTest::getInstance()->getCpt());