-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleton.php
executable file
·140 lines (118 loc) · 3.85 KB
/
Singleton.php
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* Aomebo - a module-based MVC framework for PHP 5.3 and higher
*
* Copyright 2010 - 2015 by Christian Johansson <[email protected]>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
* @license LGPL version 3
* @see http://www.aomebo.org/ or https://github.com/cjohansson/Aomebo-Framework
*/
/**
*
*/
namespace Aomebo
{
/**
*
*/
class Singleton extends \Aomebo\Base
{
/**
* @internal
* @static
* @var array
*/
private static $_instances;
/**
* @internal
* @static
* @var array
*/
private static $_isConstructed;
/**
*
*/
public function __construct()
{
parent::__construct();
$calledClass = get_called_class();
if (\Aomebo\Application::isInhibitedToConstruct(
$calledClass)
) {
Throw new \Exception(
sprintf(
self::systemTranslate('Class %s may not be constructed at this time in the execution chain. Please adjust your runtime configuration.'),
$calledClass
)
);
}
if (!is_array(self::$_instances)) {
self::$_instances = array();
}
if (!is_array(self::$_isConstructed)) {
self::$_isConstructed = array();
}
if (!self::_isInstanciated($calledClass)) {
self::$_instances[$calledClass] = & $this;
}
}
/**
* @static
* @param string|null [$className = null]
* @return Singleton
*/
public static function getInstance($className = null)
{
if (empty($className)) {
$className = get_called_class();
}
if (!self::_isInstanciated($className)) {
$newObject = new $className();
if (!self::_isInstanciated($className)) {
self::$_instances[$className] = $newObject;
}
}
return self::$_instances[$className];
}
/**
* @static
* @param string|null [$className = null]
* @return bool
*/
protected static function _isInstanciated($className = null)
{
$calledClass = (!empty($className) ? $className : get_called_class());
return isset(self::$_instances[$calledClass]);
}
/**
* @static
* @param string|null [$className = null]
*/
protected static function _flagThisConstructed($className = null)
{
$calledClass = (!empty($className) ? $className : get_called_class());
self::$_isConstructed[$calledClass] = true;
}
/**
* @static
* @param string|null [$className = null]
* @return bool
*/
protected static function _isConstructed($className = null)
{
$calledClass = (!empty($className) ? $className : get_called_class());
return (is_array(self::$_isConstructed)
&& !empty(self::$_isConstructed[$calledClass]));
}
}
}