summaryrefslogtreecommitdiff
path: root/includes/classes/BitSingleton.php
blob: a7eb295768fe9b561510355ceae1178fea7e6570 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
 * Base class for all objects where only one object should be created
 *
 * @version $Header$
 *
 * Copyright (c) 2004 bitweaver.org
 * All Rights Reserved. See below for details and a complete list of authors.
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
 *
 * Virtual base class (as much as one can have such things in PHP) for all
 * derived tikiwiki classes that require database access.
 *
 * created 2004/8/15
 *
 * @author spider <spider@steelsun.com>
 * @package  kernel
 */

namespace Bitweaver;

/**
 * required setup
 */

/**
 * @package  kernel
 */
abstract class BitSingleton extends BitBase implements BitCacheable {

	protected static $singletons = null;

	function __construct() {
		parent::__construct();
	}

	public static function loadSingleton( $pVarName = null ) {
		$class = static::getClass();
		$split = explode('\\', $class);
		$globalVarName = !empty( $pVarName ) ? $pVarName : ( isset($split[2]) ? 'g'.$split[2] : 'g'.$split[1] );
		global $$globalVarName;

		if( !($$globalVarName = static::loadFromCache( 'Singleton' )) ) {
			$$globalVarName = new $class();
		}
		if(!isset(static::$singletons[$globalVarName])) {
			static::$singletons[$globalVarName] = $$globalVarName;
			global $gBitSmarty;
			if( $gBitSmarty ) {
				$gBitSmarty->assign( $globalVarName, $$globalVarName );
			}
		}
		return static::$singletons[$globalVarName];
	}

	public function getCacheKey() {
		return 'Singleton';
	}

	public static function isCacheableClass() {
		return true;
	}

	public function isCacheableObject() {
		return true;
	}

}