1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Shared |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Shared\Message\Loader; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* LoaderAwareTrait |
19
|
|
|
* |
20
|
|
|
* One implementation of `LoaderAwareInterface` |
21
|
|
|
* |
22
|
|
|
* @package Phossa2\Shared |
23
|
|
|
* @author Hong Zhang <[email protected]> |
24
|
|
|
* @see LoaderAwareInterface |
25
|
|
|
* @version 2.0.0 |
26
|
|
|
* @since 2.0.0 added |
27
|
|
|
*/ |
28
|
|
|
trait LoaderAwareTrait |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Message loaders pool, [ classname => $loader ] |
32
|
|
|
* |
33
|
|
|
* @var LoaderInterface[] |
34
|
|
|
* @access private |
35
|
|
|
*/ |
36
|
|
|
private static $loaders = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* loader update indicator |
40
|
|
|
* |
41
|
|
|
* @var bool |
42
|
|
|
* @access private |
43
|
|
|
*/ |
44
|
|
|
private static $updated = false; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set/replace loader for current message class (static bind) |
48
|
|
|
* |
49
|
|
|
* @param LoaderInterface $loader the mapping loader |
50
|
|
|
* @access public |
51
|
|
|
* @api |
52
|
|
|
*/ |
53
|
|
|
public static function setLoader( |
54
|
|
|
LoaderInterface $loader |
55
|
|
|
) { |
56
|
|
|
// set loader for current class |
57
|
|
|
self::$loaders[get_called_class()] = $loader; |
58
|
|
|
|
59
|
|
|
// update indicator |
60
|
|
|
static::setStatus(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Unset loader for calling message class |
65
|
|
|
* |
66
|
|
|
* if $search is true, search upwards in the inheritance tree |
67
|
|
|
* |
68
|
|
|
* @param bool $search search upwards |
69
|
|
|
* @access public |
70
|
|
|
*/ |
71
|
|
|
public static function unsetLoader(/*# bool */ $search = false) |
72
|
|
|
{ |
73
|
|
|
$class = static::hasLoader($search); |
74
|
|
|
if (false !== $class) { |
75
|
|
|
// unset loader for current class |
76
|
|
|
unset(self::$loaders[$class]); |
77
|
|
|
|
78
|
|
|
// update indicator |
79
|
|
|
static::setStatus(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get loader for current message class (static bind) |
85
|
|
|
* |
86
|
|
|
* use `hasLoader()` before this method ! |
87
|
|
|
* |
88
|
|
|
* @return LoaderInterface |
89
|
|
|
* @access protected |
90
|
|
|
*/ |
91
|
|
|
protected static function getLoader()/*# : LoaderInterface */ |
92
|
|
|
{ |
93
|
|
|
return self::$loaders[get_called_class()]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Check loader for calling message class (static bind) |
98
|
|
|
* |
99
|
|
|
* if $search is true, search upwards in inhertiant tree for loader |
100
|
|
|
* if current class has no loader set |
101
|
|
|
* |
102
|
|
|
* @param bool $search search upwards |
103
|
|
|
* @return false|string false or classname for which has loader |
104
|
|
|
* @access protected |
105
|
|
|
*/ |
106
|
|
|
protected static function hasLoader( |
107
|
|
|
/*# bool */ $search = false |
108
|
|
|
) { |
109
|
|
|
$class = get_called_class(); |
110
|
|
|
|
111
|
|
|
if (isset(self::$loaders[$class])) { |
112
|
|
|
return $class; |
113
|
|
|
} elseif (__CLASS__ === $class) { |
114
|
|
|
return false; |
115
|
|
|
} elseif ($search) { |
116
|
|
|
$parent = get_parent_class($class); |
117
|
|
|
return $parent::hasLoader(true); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Set updated indicator |
125
|
|
|
* |
126
|
|
|
* @param bool $status updated status |
127
|
|
|
* @access protected |
128
|
|
|
*/ |
129
|
|
|
protected static function setStatus( |
130
|
|
|
/*# bool */ $status = true |
131
|
|
|
) { |
132
|
|
|
self::$updated = $status; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get updated indicator |
137
|
|
|
* |
138
|
|
|
* @return bool |
139
|
|
|
* @access protected |
140
|
|
|
*/ |
141
|
|
|
protected static function isStatusUpdated()/*: bool */ |
142
|
|
|
{ |
143
|
|
|
return self::$updated; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|