diff --git a/src/Singleton/IRegistry.php b/src/Singleton/IRegistry.php index 212fa0e..6bf4b7a 100644 --- a/src/Singleton/IRegistry.php +++ b/src/Singleton/IRegistry.php @@ -12,36 +12,27 @@ use Throwable; */ interface IRegistry { /** - * Register a new entry + * Get the instance * - * @param string $name The entry name - * @param array $args The instance constructor arguments + * If the instance is not already created then create it * - * @return void + * @param string|null $name The entry name. Null if default entry name * - * @throws InvalidArgumentException If the entry name already exists + * @return static The instance + * + * @throws InvalidArgumentException If the entry name doesn't exist + * @throws Throwable If instantiating failed */ - public static function addEntry (string $name, ...$args): void; + public static function getInstance (?string $name = null): self; + /** * Check if an entry name already exists * * @param string $name The entry name * * @return bool TRUE if the entry name already exists - */ - public static function hasEntry (string $name): bool; - - /** - * Get the instance - * - * If the instance is not already created then create it - * - * @param string The entry name * - * @return static The instance - * - * @throws InvalidArgumentException If the entry name doesn't exist - * @throws Throwable If instantiating failed + * @throws Throwable If an error occur */ - public static function getInstance (string $name): self; + public static function hasEntry (string $name): bool; } \ No newline at end of file diff --git a/src/Singleton/IRegistryDefault.php b/src/Singleton/IRegistryDefault.php deleted file mode 100644 index 60b8e59..0000000 --- a/src/Singleton/IRegistryDefault.php +++ /dev/null @@ -1,40 +0,0 @@ -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/TRegistry.php b/src/Singleton/TRegistry.php index 22c327d..3f66ce6 100644 --- a/src/Singleton/TRegistry.php +++ b/src/Singleton/TRegistry.php @@ -2,6 +2,9 @@ namespace jrosset\Singleton; +use InvalidArgumentException; +use Throwable; + /** * Default implementation for registry * @@ -10,12 +13,75 @@ namespace jrosset\Singleton; * @see IRegistry for interface */ trait TRegistry { - use TRegistryBase; + /** + * @var array[] The registry entries + */ + private static array $registerEntries = []; + /** + * @var static[] The current instances + */ + private static array $registryInstances = []; + + /** + * Initialize registry entries + * + * Use {@see TRegistryBase::addEntry()} to add a new entry + * + * @return void + * + * @throws Throwable If an error occur + */ + protected static abstract function initializeEntries (): void; + /** + * Register a new entry + * + * @param string $name The entry name + * @param array $args The instance constructor arguments + * + * @return void + * + * @throws InvalidArgumentException If the entry name already exists + * @throws Throwable If an error occur + */ + protected static function addEntry (string $name, ...$args): void { + if (static::hasEntry($name)) { + throw new InvalidArgumentException('An entry with "' . $name . '" already exists'); + } + static::$registerEntries[$name] = $args; + } + + /** + * The default entry name + * + * @return string The default entry name + * + * @throws InvalidArgumentException If the entry name isn't set + */ + protected static function getDefaultEntryName (): string { + throw new InvalidArgumentException('No default entry name available'); + } + + /** + * @inheritDoc + */ + public static function hasEntry (string $name): bool { + if (count(static::$registerEntries[$name] ?? []) === 0) { + static::initializeEntries(); + } + return isset(static::$registerEntries[$name]); + } /** * @inheritDoc */ - public static function getInstance (string $name): self { - return static::TRegistryBase_getInstance($name); + public static function getInstance (?string $name = null): self { + $name ??= static::getDefaultEntryName(); + if (!static::hasEntry($name)) { + throw new InvalidArgumentException('Unable to find entry "' . $name . '"'); + } + if (!isset(static::$registryInstances[$name])) { + static::$registryInstances[$name] = new static(...static::$registerEntries[$name]); + } + return static::$registryInstances[$name]; } } \ No newline at end of file diff --git a/src/Singleton/TRegistryBase.php b/src/Singleton/TRegistryBase.php deleted file mode 100644 index 40c4656..0000000 --- a/src/Singleton/TRegistryBase.php +++ /dev/null @@ -1,62 +0,0 @@ -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 { static::$registryDefaultEntryName = $name; } - - /** - * @inheritDoc - */ - public static function getInstance (?string $name = null): self { - return static::TRegistryBase_getInstance($name ?? static::getDefaultEntryName()); - } } \ No newline at end of file diff --git a/src/Singleton/TRegistryExternal.php b/src/Singleton/TRegistryExternal.php new file mode 100644 index 0000000..d07339f --- /dev/null +++ b/src/Singleton/TRegistryExternal.php @@ -0,0 +1,29 @@ +