3 use wcf\system\exception\SystemException
;
6 * Base class for singleton factories.
8 * @author Alexander Ebert
9 * @copyright 2001-2018 WoltLab GmbH
10 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11 * @package WoltLabSuite\Core\System
13 abstract class SingletonFactory
{
16 * @var SingletonFactory[]
18 protected static $__singletonObjects = [];
21 * Singletons do not support a public constructor. Override init() if
22 * your class needs to initialize components on creation.
24 protected final function __construct() {
29 * Called within __construct(), override if necessary.
31 protected function init() { }
34 * Object cloning is disallowed.
36 protected final function __clone() { }
39 * Object serializing is disallowed.
41 public final function __sleep() {
42 throw new SystemException('Serializing of Singletons is not allowed');
46 * Returns an unique instance of current child class.
49 * @throws SystemException
51 public static final function getInstance() {
52 $className = get_called_class();
53 if (!array_key_exists($className, self
::$__singletonObjects)) {
54 self
::$__singletonObjects[$className] = null;
55 self
::$__singletonObjects[$className] = new $className();
57 else if (self
::$__singletonObjects[$className] === null) {
58 throw new SystemException("Infinite loop detected while trying to retrieve object for '".$className."'");
61 return self
::$__singletonObjects[$className];
65 * Returns whether this singleton is already initialized.
69 public static final function isInitialized() {
70 $className = get_called_class();
72 return isset(self
::$__singletonObjects[$className]);