-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStaticProxyAware.php
96 lines (84 loc) · 2.1 KB
/
StaticProxyAware.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
<?php
/**
* Qubus\Inheritance
*
* @link https://github.com/QubusPHP/inheritance
* @copyright 2022
* @author Joshua Parker <josh@joshuaparker.blog>
* @license https://opensource.org/licenses/mit-license.php MIT License
*
* @since 2.0.1
*/
declare(strict_types=1);
namespace Qubus\Inheritance;
use ReflectionClass;
use ReflectionException;
use RuntimeException;
trait StaticProxyAware
{
/**
* The stored singleton instance.
*
* @var self $instance.
*/
protected static $instance;
/**
* Creates the original or retrieves the stored singleton instance.
*
* @return self
* @throws ReflectionException
*/
public static function getInstance(): static
{
if (! static::$instance) {
static::$instance = (new ReflectionClass(static::class))
->newInstanceWithoutConstructor();
}
return static::$instance;
}
/**
* Reset the Container instance.
*/
public static function resetInstance(): void
{
if (self::$instance) {
self::$instance = null;
}
}
/**
* The constructor is disabled.
*
* @throws RuntimeException If called..
*/
public function __construct()
{
throw new RuntimeException('You may not explicitly instantiate this object, because it is a singleton.');
}
/**
* Cloning is disabled.
*
* @throws RuntimeException If called.
*/
public function __clone()
{
throw new RuntimeException('You may not clone this object, because it is a singleton.');
}
/**
* Wakeup is disabled.
*
* @throws RuntimeException If called.
*/
public function __wakeup()
{
throw new RuntimeException('You may not wakeup this object, because it is a singleton.');
}
/**
* Unserialization is disabled.
*
* @throws RuntimeException If called.
*/
public function unserialize(array $serializedData)
{
throw new RuntimeException('You may not unserialize this object, because it is a singleton.');
}
}