|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Factory for PageType handlers |
|
4
|
|
|
* |
|
5
|
|
|
* @package Stencil |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class HandlerFactory |
|
10
|
|
|
*/ |
|
11
|
|
|
class Stencil_Handler_Factory implements Stencil_Handler_Factory_Interface { |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Format the class name: |
|
15
|
|
|
*/ |
|
16
|
|
|
const CLASS_FORMAT = 'Stencil_%s'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* List of registered Handlers |
|
20
|
|
|
* |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
private static $handlers = array(); |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Set a hierarchy handler for a page |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $page Page to set. |
|
29
|
|
|
* @param array|Traversable|null $handler Optional. Handler to use. |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function set_hierarchy_handler( $page, $handler = null ) { |
|
32
|
|
|
self::set_settable_handler( 'Hierarchy', $page, $handler = null ); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Set a page type handler |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $page Page to set. |
|
39
|
|
|
* @param callable $handler Handler to use. |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function set_page_type_handler( $page, $handler ) { |
|
42
|
|
|
self::set_settable_handler( 'PageType', $page, $handler ); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Remove handler for a page. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $page Page to remove from. |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function remove_page_type_handler( $page ) { |
|
51
|
|
|
self::set_settable_handler( 'PageType', $page ); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Set page type hooking function |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $page Page to set to. |
|
58
|
|
|
* @param callable $handler Handler to use. |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function set_page_type_hooker( $page, $handler ) { |
|
61
|
|
|
self::set_settable_handler( 'PageHook', $page, $handler ); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Remove page hooker from page |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $page Page to remove from. |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function remove_page_type_hooker( $page ) { |
|
70
|
|
|
self::set_settable_handler( 'PageHook', $page ); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get current hierarchy handler for page |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $page Page to get from. |
|
77
|
|
|
* |
|
78
|
|
|
* @return array|Traversable |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function get_hierarchy_handler( $page ) { |
|
81
|
|
|
return self::get_settable_handler( $page, 'Hierarchy' ); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get the current page type handler |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $page Page to get from. |
|
88
|
|
|
* |
|
89
|
|
|
* @return callable |
|
90
|
|
|
*/ |
|
91
|
|
|
public static function get_page_type_handler( $page ) { |
|
92
|
|
|
return self::get_settable_handler( $page, 'Page_Type' ); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get the current page type hooker |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $page Page to get from. |
|
99
|
|
|
* |
|
100
|
|
|
* @return callable |
|
101
|
|
|
*/ |
|
102
|
|
|
public static function get_page_type_hooker( $page ) { |
|
103
|
|
|
return self::get_settable_handler( $page, 'Page_Hook' ); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Run the handler for the specified page |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $page Page to call handler for |
|
110
|
|
|
* @param Stencil_Handler_Interface $controller Controller that initiated the call |
|
111
|
|
|
*/ |
|
112
|
|
|
public static function run_page_type_handler( $page, Stencil_Handler_Interface $controller ) { |
|
113
|
|
|
self::execute_handler( self::get_page_type_handler( $page ), $controller ); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Run the hooker for the specified page |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $page Page to call handler for |
|
120
|
|
|
* @param Stencil_Handler_Interface $controller Controller that initiated the call |
|
121
|
|
|
*/ |
|
122
|
|
|
public static function run_page_type_hook( $page, Stencil_Handler_Interface $controller ) { |
|
123
|
|
|
self::execute_handler( self::get_page_type_hooker( $page ), $controller ); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Execute a handler if it is callable |
|
128
|
|
|
* |
|
129
|
|
|
* @param callable|array $handler The handler for the page requested |
|
130
|
|
|
* @param Stencil_Handler_Interface $controller Controller that initiated the call |
|
131
|
|
|
*/ |
|
132
|
|
|
private static function execute_handler( $handler, Stencil_Handler_Interface $controller ) { |
|
133
|
|
|
if ( is_callable( $handler ) ) { |
|
134
|
|
|
call_user_func( $handler, $controller ); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Unified setter function |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $type Type of object. |
|
142
|
|
|
* @param string $page Page name. |
|
143
|
|
|
* @param mixed|null $handler Handler to apply. |
|
144
|
|
|
* |
|
145
|
|
|
* @throws InvalidArgumentException For invalid argument types. |
|
146
|
|
|
*/ |
|
147
|
|
|
private static function set_settable_handler( $type, $page, $handler = null ) { |
|
148
|
|
|
// Create NullObject for empty handler, i.e. remove functionality. |
|
149
|
|
|
if ( empty( $handler ) ) { |
|
150
|
|
|
$handler = Stencil_Subclass_Factory::create_null( sprintf( self::CLASS_FORMAT, $type ) ); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
switch ( $type ) { |
|
154
|
|
|
case 'Hierarchy': |
|
155
|
|
|
if ( ! is_array( $handler ) && ! ( $handler instanceof Traversable ) ) { |
|
156
|
|
|
throw new InvalidArgumentException( 'Expected $handler to be array or Traversable.' ); |
|
157
|
|
|
} |
|
158
|
|
|
break; |
|
159
|
|
|
|
|
160
|
|
|
default: |
|
161
|
|
|
if ( ! is_callable( $handler ) ) { |
|
162
|
|
|
throw new InvalidArgumentException( 'Expected $handler to be callable.' ); |
|
163
|
|
|
} |
|
164
|
|
|
break; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
self::$handlers[ $type ][ $page ] = $handler; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Unified getter function |
|
172
|
|
|
* |
|
173
|
|
|
* @param string $page Page to get from. |
|
174
|
|
|
* @param string $type Type to get. |
|
175
|
|
|
* |
|
176
|
|
|
* @return array Callable function |
|
177
|
|
|
*/ |
|
178
|
|
|
private static function get_settable_handler( $page, $type ) { |
|
179
|
|
|
if ( ! isset( self::$handlers[ $type ][ $page ] ) ) { |
|
180
|
|
|
$handler = Stencil_Cached_Subclass_Factory::create_or_null( $page, sprintf( self::CLASS_FORMAT, $type ) ); |
|
181
|
|
|
|
|
182
|
|
|
switch ( $type ) { |
|
183
|
|
|
case 'Hierarchy': |
|
184
|
|
|
break; |
|
185
|
|
|
default: |
|
186
|
|
|
$handler = array( $handler, 'execute' ); |
|
187
|
|
|
break; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
self::$handlers[ $type ][ $page ] = $handler; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
return self::$handlers[ $type ][ $page ]; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|