Completed
Push — development ( ac22bd...5236fa )
by Stephen
14:46
created

TemplateLayers::instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 11
ccs 4
cts 5
cp 0.8
crap 2.032
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Functions used to manage template layers
5
 *
6
 * @name      ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
9
 *
10
 * @version 1.1
11
 *
12
 */
13
14
namespace ElkArte\Themes;
15
16
use Priority;
17
18
/**
19
 * Class used to manage template layers
20
 *
21
 * An instance of the class can be retrieved with the static method instance
22
 */
23
class TemplateLayers extends Priority
24
{
25
	/**
26
	 * Layers not removed in case of errors
27
	 */
28
	private $_error_safe_layers = null;
29
30
	/**
31
	 * Are we handling an error?
32
	 * Hopefully not, so default is false
33
	 */
34
	private $_is_error = false;
35
36
	/**
37
	 * The layers added when this is true will be used in the error screen
38
	 */
39
	private static $_error_safe = false;
40
41
	/**
42
	 * Instance of the class
43
	 */
44
	private static $_instance = null;
0 ignored issues
show
Unused Code introduced by
The property $_instance is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
45
46
	/**
47
	 * Add a new layer to the pile
48
	 *
49
	 * @param string   $layer    name of a layer
50
	 * @param int|null $priority an integer defining the priority of the layer.
51
	 */
52
	public function add($layer, $priority = null)
53
	{
54
		parent::add($layer, $priority);
55
56
		if (self::$_error_safe)
57
		{
58
			$this->_error_safe_layers[] = $layer;
59
		}
60
	}
61
62
	/**
63
	 * Add a layer to the pile before another existing layer
64
	 *
65
	 * @param string $layer     the name of a layer
66
	 * @param string $following the name of the layer before which $layer must be added
67
	 */
68
	public function addBefore($layer, $following)
69
	{
70
		parent::addBefore($layer, $following);
71
72
		if (self::$_error_safe)
73
		{
74
			$this->_error_safe_layers[] = $layer;
75
		}
76
	}
77
78
	/**
79
	 * Add a layer to the pile after another existing layer
80
	 *
81
	 * @param string $layer    the name of a layer
82
	 * @param string $previous the name of the layer after which $layer must be added
83
	 */
84
	public function addAfter($layer, $previous)
85
	{
86
		parent::addAfter($layer, $previous);
87
88
		if (self::$_error_safe)
89
		{
90
			$this->_error_safe_layers[] = $layer;
91
		}
92
	}
93
94
	/**
95
	 * Add a layer at the end of the pile
96
	 *
97
	 * @param string   $layer    name of a layer
98
	 * @param int|null $priority an integer defining the priority of the layer.
99
	 */
100
	public function addEnd($layer, $priority = null)
101
	{
102
		parent::addEnd($layer, $priority);
103
104
		if (self::$_error_safe)
105
		{
106
			$this->_error_safe_layers[] = $layer;
107
		}
108
	}
109
110
	/**
111
	 * Add a layer at the beginning of the pile
112
	 *
113
	 * @param string   $layer    name of a layer
114
	 * @param int|null $priority an integer defining the priority of the layer.
115
	 */
116
	public function addBegin($layer, $priority = null)
117
	{
118
		parent::addBegin($layer, $priority);
119
120
		if (self::$_error_safe)
121
		{
122
			$this->_error_safe_layers[] = $layer;
123
		}
124
	}
125
126
	/**
127
	 * Prepares the layers so that they are usable by the template
128
	 * The function sorts the layers according to the priority and saves the
129
	 * result in $_sorted_entities
130
	 *
131
	 * @return array the sorted layers
132
	 */
133
	public function prepareContext()
134
	{
135
		$all_layers = $this->sort();
136
137
		// If we are dealing with an error page (fatal_error) then we have to prune all the unwanted layers
138
		if ($this->_is_error)
139
		{
140
			$dummy = $all_layers;
141
			$all_layers = [];
142
143
			foreach ($dummy as $key => $val)
144
			{
145
				if (in_array($key, $this->_error_safe_layers))
146
				{
147
					$all_layers[$key] = $val;
148
				}
149
			}
150
		}
151
152
		asort($all_layers);
153
		$this->_sorted_entities = array_keys($all_layers);
154
155
		return $this->_sorted_entities;
156
	}
157
158
	/**
159
	 * Reverse the layers order
160
	 *
161
	 * @return array the reverse ordered layers
162
	 */
163
	public function reverseLayers()
164
	{
165
		if ($this->_sorted_entities === null)
166
		{
167
			$this->prepareContext();
168
		}
169
170
		return array_reverse($this->_sorted_entities);
171
	}
172
173
	/**
174
	 * Check if at least one layer has been added
175
	 *
176
	 * @param boolean $base if true will not consider body and html layers in result
177
	 *
178
	 * @return bool true if at least one layer has been added
179
	 * @todo at that moment _all_after and _all_before are not considered because they may not be "forced"
180
	 */
181
	public function hasLayers($base = false)
182
	{
183
		if (!$base)
184
		{
185
			return (!empty($this->_all_general) || !empty($this->_all_begin) || !empty($this->_all_end));
186
		}
187
		else
188
		{
189
			return array_diff_key(array_merge($this->_all_general, $this->_all_begin, $this->_all_end), [
190
				'body' => 0,
191
				'html' => 0,
192
			]);
193
		}
194
	}
195
196
	/**
197
	 * Return the layers that have been loaded
198
	 */
199
	public function getLayers()
200
	{
201
		return array_keys(array_merge($this->_all_general, $this->_all_begin, $this->_all_end, $this->_all_after,
202
			$this->_all_before));
203
	}
204
205
	/**
206
	 * Turns "error mode" on, so that only the allowed layers are displayed
207
	 */
208
	public function isError()
209
	{
210
		$this->_is_error = true;
211
	}
212
}
213