1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @package sitemaker |
5
|
|
|
* @copyright (c) 2013 Daniel A. (blitze) |
6
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace blitze\sitemaker\services\blocks; |
11
|
|
|
|
12
|
|
|
class routes |
13
|
|
|
{ |
14
|
|
|
/** @var \phpbb\cache\driver\driver_interface */ |
15
|
|
|
protected $cache; |
16
|
|
|
|
17
|
|
|
/** @var \phpbb\config\config */ |
18
|
|
|
protected $config; |
19
|
|
|
|
20
|
|
|
/** @var \blitze\sitemaker\services\blocks\factory */ |
21
|
|
|
protected $block_factory; |
22
|
|
|
|
23
|
|
|
/** @var \blitze\sitemaker\model\mapper_factory */ |
24
|
|
|
protected $mapper_factory; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor |
28
|
|
|
* |
29
|
|
|
* @param \phpbb\cache\driver\driver_interface $cache Cache driver interface |
30
|
|
|
* @param \phpbb\config\config $config Config object |
31
|
|
|
* @param \blitze\sitemaker\services\blocks\factory $block_factory Blocks factory object |
32
|
|
|
* @param \blitze\sitemaker\model\mapper_factory $mapper_factory Mapper factory object |
33
|
|
|
*/ |
34
|
|
|
public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\config\config $config, \blitze\sitemaker\services\blocks\factory $block_factory, \blitze\sitemaker\model\mapper_factory $mapper_factory) |
35
|
|
|
{ |
36
|
|
|
$this->cache = $cache; |
37
|
|
|
$this->config = $config; |
38
|
|
|
$this->block_factory = $block_factory; |
39
|
|
|
$this->mapper_factory = $mapper_factory; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $current_route |
44
|
|
|
* @param int $style_id |
45
|
|
|
* @param bool|false $edit_mode |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function get_route_info($current_route, $style_id, $edit_mode = false) |
49
|
|
|
{ |
50
|
|
|
$all_routes = $this->_get_all_routes(); |
51
|
|
|
|
52
|
|
|
if (isset($all_routes[$style_id][$current_route])) |
53
|
|
|
{ |
54
|
|
|
return $all_routes[$style_id][$current_route]; |
55
|
|
|
} |
56
|
|
|
else |
57
|
|
|
{ |
58
|
|
|
return $this->_get_default_route_info($all_routes, $current_route, $style_id, $edit_mode); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param array $route_info |
64
|
|
|
* @param int $style_id |
65
|
|
|
* @param bool $edit_mode |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function get_blocks_for_route(array $route_info, $style_id, $edit_mode) |
69
|
|
|
{ |
70
|
|
|
$blocks = $this->_get_cached_blocks($edit_mode); |
71
|
|
|
$route_id = $this->_get_display_route_id($route_info, $style_id, $edit_mode); |
72
|
|
|
|
73
|
|
|
return (isset($blocks[$style_id][$route_id]) && !$route_info['hide_blocks']) ? $blocks[$style_id][$route_id] : array(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Clear blocks cache |
78
|
|
|
*/ |
79
|
|
|
public function clear_cache() |
80
|
|
|
{ |
81
|
|
|
$this->cache->destroy('sitemaker_blocks'); |
82
|
|
|
$this->cache->destroy('sitemaker_block_routes'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param bool $edit_mode |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
protected function _get_cached_blocks($edit_mode) |
90
|
|
|
{ |
91
|
|
|
if (($blocks = $this->cache->get('sitemaker_blocks')) === false || $edit_mode) |
92
|
|
|
{ |
93
|
|
|
$blocks = $this->_get_all_blocks(); |
94
|
|
|
$this->_cache_block($blocks, $edit_mode); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $blocks; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
protected function _get_all_blocks() |
104
|
|
|
{ |
105
|
|
|
$block_mapper = $this->mapper_factory->create('blocks', 'blocks'); |
106
|
|
|
$collection = $block_mapper->find(); |
107
|
|
|
|
108
|
|
|
$blocks = array(); |
109
|
|
|
foreach ($collection as $entity) |
110
|
|
|
{ |
111
|
|
|
if ($block_instance = $this->block_factory->get_block($entity->get_name())) |
112
|
|
|
{ |
113
|
|
|
$default_settings = $block_instance->get_config(array()); |
114
|
|
|
$settings = $this->sync_settings($default_settings, $entity->get_settings()); |
|
|
|
|
115
|
|
|
|
116
|
|
|
$entity->set_settings($settings); |
117
|
|
|
|
118
|
|
|
$style = $entity->get_style(); |
119
|
|
|
$route_id = $entity->get_route_id(); |
120
|
|
|
$position = $entity->get_position(); |
121
|
|
|
|
122
|
|
|
$blocks[$style][$route_id][$position][] = $entity; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $blocks; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return array|mixed |
131
|
|
|
*/ |
132
|
|
|
protected function _get_all_routes() |
133
|
|
|
{ |
134
|
|
|
if (($all_routes = $this->cache->get('sitemaker_block_routes')) === false) |
135
|
|
|
{ |
136
|
|
|
$route_mapper = $this->mapper_factory->create('blocks', 'routes'); |
137
|
|
|
$collection = $route_mapper->find(); |
138
|
|
|
|
139
|
|
|
$all_routes = array(); |
140
|
|
|
foreach ($collection as $entity) |
141
|
|
|
{ |
142
|
|
|
$route = $entity->get_route(); |
143
|
|
|
$style = $entity->get_style(); |
144
|
|
|
$all_routes[$style][$route] = $entity->to_array(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$this->cache->put('sitemaker_block_routes', $all_routes); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $all_routes; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param array $all_routes |
155
|
|
|
* @param string $current_route |
156
|
|
|
* @param int $style_id |
157
|
|
|
* @param bool $edit_mode |
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
|
|
protected function _get_default_route_info(array $all_routes, $current_route, $style_id, $edit_mode) |
161
|
|
|
{ |
162
|
|
|
$default_route = $this->config['sitemaker_default_layout']; |
163
|
|
|
$default_info = array( |
164
|
|
|
'route_id' => 0, |
165
|
|
|
'route' => $current_route, |
166
|
|
|
'style' => $style_id, |
167
|
|
|
'hide_blocks' => false, |
168
|
|
|
'ex_positions' => array(), |
169
|
|
|
'has_blocks' => false, |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
return ($edit_mode === false && isset($all_routes[$style_id][$default_route])) ? $all_routes[$style_id][$default_route] : $default_info; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param array $route_info |
177
|
|
|
* @param int $style_id |
178
|
|
|
* @param bool $edit_mode |
179
|
|
|
* @return int |
180
|
|
|
*/ |
181
|
|
|
protected function _get_display_route_id(array $route_info, $style_id, $edit_mode) |
182
|
|
|
{ |
183
|
|
|
$route_id = $route_info['route_id']; |
184
|
|
|
if ($edit_mode === false && !$route_info['has_blocks']) |
185
|
|
|
{ |
186
|
|
|
$default_route = $this->get_route_info($this->config['sitemaker_default_layout'], $style_id, $edit_mode); |
187
|
|
|
$route_id = $default_route['route_id']; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return (int) $route_id; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param array $blocks |
195
|
|
|
* @param bool $edit_mode |
196
|
|
|
*/ |
197
|
|
|
protected function _cache_block(array $blocks, $edit_mode) |
198
|
|
|
{ |
199
|
|
|
if (!$edit_mode) |
200
|
|
|
{ |
201
|
|
|
$this->cache->put('sitemaker_blocks', $blocks); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: