parent
6bf4ea2131
commit
22b17de4ca
@ -1,40 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace jrosset\Singleton;
|
|
||||||
|
|
||||||
use InvalidArgumentException;
|
|
||||||
use Throwable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface for class implementing an instance register with a default entry name
|
|
||||||
*/
|
|
||||||
interface IRegistryDefault extends IRegistry {
|
|
||||||
/**
|
|
||||||
* The default entry name
|
|
||||||
*
|
|
||||||
* @return string The default entry name
|
|
||||||
*
|
|
||||||
* @throws InvalidArgumentException If the entry name isn't set
|
|
||||||
*/
|
|
||||||
public static function getDefaultEntryName (): string;
|
|
||||||
/**
|
|
||||||
* Set the default entry name
|
|
||||||
*
|
|
||||||
* <b>WARNING :</b> 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;
|
|
||||||
}
|
|
||||||
@ -1,62 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace jrosset\Singleton;
|
|
||||||
|
|
||||||
use InvalidArgumentException;
|
|
||||||
use Throwable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Default base implementation for registry
|
|
||||||
*
|
|
||||||
* Instance initialization through the constructor
|
|
||||||
*
|
|
||||||
* @see IRegistry for interface
|
|
||||||
*/
|
|
||||||
trait TRegistryBase {
|
|
||||||
/**
|
|
||||||
* @var array[] The registry entries
|
|
||||||
*/
|
|
||||||
private static array $registerEntries = [];
|
|
||||||
/**
|
|
||||||
* @var static[] The current instances
|
|
||||||
*/
|
|
||||||
private static array $registryInstances = [];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritDoc
|
|
||||||
*/
|
|
||||||
public static function addEntry (string $name, ...$args): void {
|
|
||||||
if (static::hasEntry($name)) {
|
|
||||||
throw new InvalidArgumentException('An entry with "' . $name . '" already exists');
|
|
||||||
}
|
|
||||||
static::$registerEntries[$name] = $args;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* @inheritDoc
|
|
||||||
*/
|
|
||||||
public static function hasEntry (string $name): bool {
|
|
||||||
return isset(static::$registerEntries[$name]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
|
||||||
*/
|
|
||||||
public static function TRegistryBase_getInstance (string $name): self {
|
|
||||||
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];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace jrosset\Singleton;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default implementation for registry with external added entries
|
||||||
|
*
|
||||||
|
* Instance initialization through the constructor
|
||||||
|
*
|
||||||
|
* @see IRegistry for interface
|
||||||
|
*/
|
||||||
|
trait TRegistryExternal {
|
||||||
|
use TRegistry {
|
||||||
|
addEntry as protected TRegistry_addEntry;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public static function addEntry (string $name, ...$args): void {
|
||||||
|
self::TRegistry_addEntry($name, ...$args);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
protected static function initializeEntries (): void {
|
||||||
|
// Do nothing : add entry is now public for external added entries
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue