Completed
Push — development ( 8981a4...d3a488 )
by Stephen
18s
created

ParserWrapper::instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 *
5
 * @name      ElkArte Forum
6
 * @copyright ElkArte Forum contributors
7
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
8
 *
9
 * @version 1.1 Release Candidate 1
10
 *
11
 */
12
13
namespace BBC;
14
15
/**
16
 * Class ParserWrapper
17
 *
18
 * Wrap around the BBC parsers before we implement a DIC.
19
 * Deprecate in future versions in favor of a DIC
20
 */
21
final class ParserWrapper
22
{
23
	/** @var array Disabled tags */
24
	protected $disabled = array();
25
	/** @var \BBC\Codes */
26
	protected $codes;
27
	/** @var  \BBC\BBCParser */
28
	protected $bbc_parser;
29
	/** @var  \BBC\SmileyParser */
30
	protected $smiley_parser;
31
	/** @var  \BBC\HtmlParser */
32
	protected $html_parser;
33
	/** @var  \BBC\Autolink */
34
	protected $autolink_parser;
35
	/** @var bool If smileys are enabled */
36
	protected $smileys_enabled = true;
37
	/** @var ParserWrapper */
38
	public static $instance;
39
40
	/**
41
	 * Find and return ParserWrapper instance if it exists,
42
	 * or create a new instance
43
	 *
44
	 * @return ParserWrapper
45
	 */
46 9
	public static function instance()
47
	{
48 9
		if (self::$instance === null)
49 9
		{
50 1
			self::$instance = new ParserWrapper;
51 1
		}
52
53 9
		return self::$instance;
54
	}
55
56
	/**
57
	 * ParserWrapper constructor.
58
	 */
59 1
	private function __construct()
60
	{
61
62 1
	}
63
64
	/**
65
	 * Check if the server load is too high to execute BBC parsing
66
	 *
67
	 * @return bool If the parser can execute
68
	 */
69 6
	protected function checkLoad()
70
	{
71 6
		global $modSettings, $context;
72
73 6
		if (!empty($modSettings['bbc']) && $modSettings['current_load'] >= $modSettings['bbc'])
74 6
		{
75
			$context['disabled_parse_bbc'] = true;
76
			return false;
77
		}
78
79 6
		return true;
80
	}
81
82
	/**
83
	 * Is BBC parsing enabled
84
	 *
85
	 * @return bool
86
	 */
87 6
	protected function isEnabled()
88
	{
89 6
		global $modSettings;
90
91 6
		return !empty($modSettings['enableBBC']);
92
	}
93
94
	/**
95
	 * Enable or disable smileys
96
	 *
97
	 * @param bool|int $toggle
98
	 *
99
	 * @return $this
100
	 */
101 6
	public function enableSmileys($toggle)
102
	{
103 6
		$this->smileys_enabled = (bool) $toggle;
104 6
		return $this;
105
	}
106
107
	/**
108
	 * Get parsers based on where it will be used
109
	 *
110
	 * @param string $area Where it is being called from
111
	 * @return array
112
	 */
113 6
	protected function getParsersByArea($area)
114
	{
115
		$parsers = array(
116 6
			'autolink' => false,
117 6
			'html' => false,
118 6
			'bbc' => false,
119 6
			'smiley' => false,
120 6
		);
121
122
		// First see if any hooks set a parser.
123 6
		foreach ($parsers as $parser_type => &$parser)
124
		{
125 6
			call_integration_hook('integrate_' . $area . '_' . $parser_type . '_parser', array(&$parser, $this));
126
127
			// If not, use the default one
128 6
			if ($parser === false)
129 6
			{
130 6
				$parser = call_user_func(array($this, 'get' . ucfirst($parser_type) . 'Parser'), $area);
131 6
			}
132 6
		}
133
134 6
		return $parsers;
135
	}
136
137
	/**
138
	 * Return the current message parsers
139
	 *
140
	 * @return array
141
	 */
142
	public function getMessageParser()
143
	{
144
		return $this->getParsersByArea('message');
145
	}
146
147
	/**
148
	 * Return the current signature parsers
149
	 *
150
	 * @return array
151
	 */
152
	public function getSignatureParser()
153
	{
154
		return $this->getParsersByArea('signature');
155
	}
156
157
	/**
158
	 * Return the news parsers
159
	 *
160
	 * @return array
161
	 */
162
	public function getNewsParser()
163
	{
164
		return $this->getParsersByArea('news');
165
	}
166
167
	/**
168
	 * Parse a string based on where it's being called from
169
	 *
170
	 * @param string $area Where this is being called from
171
	 * @param string $message The message to be parsed
172
	 *
173
	 * @return string The Parsed message
174
	 */
175 6
	protected function parse($area, $message)
176
	{
177
		// If the load average is too high, don't parse the BBC.
178 6
		if (!$this->checkLoad())
179 6
		{
180
			return $message;
181
		}
182
183 6
		$parsers = $this->getParsersByArea($area);
184 6
		$smileys_enabled = $this->smileys_enabled && $GLOBALS['user_info']['smiley_set'] !== 'none';
185
186 6
		if (!$this->isEnabled())
187 6
		{
188
			// You need to run the smiley parser to get rid of the markers
189
			return $parsers['smiley']
0 ignored issues
show
Bug introduced by
The method setEnabled cannot be called on $parsers['smiley'] (of type false).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
190
				->setEnabled($smileys_enabled)
191
				->parse($message);
192
		}
193
194 6
		$message = $parsers['bbc']->parse($message);
0 ignored issues
show
Bug introduced by
The method parse cannot be called on $parsers['bbc'] (of type false).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
195
196 6
		return $parsers['smiley']
0 ignored issues
show
Bug introduced by
The method setEnabled cannot be called on $parsers['smiley'] (of type false).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
197 6
			->setEnabled($smileys_enabled)
198 6
			->parse($message);
199
	}
200
201
	/**
202
	 * Parse the BBC and smileys in messages
203
	 *
204
	 * @param string $message
205
	 * @param bool|int $smileys_enabled
206
	 *
207
	 * @return string
208
	 */
209 4
	public function parseMessage($message, $smileys_enabled)
210
	{
211 4
		return $this->enableSmileys($smileys_enabled)->parse('message', $message);
212
	}
213
214
	/**
215
	 * Parse the BBC and smileys in signatures
216
	 *
217
	 * @param string $signature
218
	 * @param bool $smileys_enabled
219
	 *
220
	 * @return string
221
	 */
222 1
	public function parseSignature($signature, $smileys_enabled)
223
	{
224 1
		return $this->enableSmileys($smileys_enabled)->parse('signature', $signature);
225
	}
226
227
	/**
228
	 * Parse the BBC and smileys in news items
229
	 *
230
	 * @param string $news
231
	 *
232
	 * @return string
233
	 */
234
	public function parseNews($news)
235
	{
236
		return $this->enableSmileys(true)->parse('news', $news);
237
	}
238
239
	/**
240
	 * Parse the BBC and smileys in emails
241
	 *
242
	 * @param string $email
243
	 *
244
	 * @return string
245
	 */
246
	public function parseEmail($email)
247
	{
248
		return $this->enableSmileys(false)->parse('email', $email);
249
	}
250
251
	/**
252
	 * Parse the BBC and smileys in custom profile fields
253
	 *
254
	 * @param string $field
255
	 *
256
	 * @return string
257
	 */
258
	public function parseCustomFields($field)
259
	{
260
		// @todo this should account for which field is being parsed and hook on that
261
262
		return $this->enableSmileys(true)->parse('customfields', $field);
263
	}
264
265
	/**
266
	 * Parse the BBC and smileys in poll questions/answers
267
	 *
268
	 * @param string $poll
269
	 *
270
	 * @return string
271
	 */
272
	public function parsePoll($poll)
273
	{
274
		return $this->enableSmileys(true)->parse('poll', $poll);
275
	}
276
277
	/**
278
	 * Parse the BBC and smileys in the registration agreement
279
	 *
280
	 * @param string $agreement
281
	 *
282
	 * @return string
283
	 */
284
	public function parseAgreement($agreement)
285
	{
286
		return $this->enableSmileys(true)->parse('agreement', $agreement);
287
	}
288
289
	/**
290
	 * Parse the BBC and smileys in personal messages
291
	 *
292
	 * @param string $pm
293
	 *
294
	 * @return string
295
	 */
296
	public function parsePM($pm)
297
	{
298
		return $this->enableSmileys(true)->parse('pm', $pm);
299
	}
300
301
	/**
302
	 * Parse the BBC and smileys in user submitted reports
303
	 *
304
	 * @param string $report
305
	 *
306
	 * @return string
307
	 */
308
	public function parseReport($report)
309
	{
310
		return $this->enableSmileys(true)->parse('report', $report);
311
	}
312
313
	/**
314
	 * Parse the BBC and smileys in package descriptions
315
	 *
316
	 * @param string $package
317
	 *
318
	 * @return string
319
	 */
320
	public function parsePackage($package)
321
	{
322
		return $this->enableSmileys(true)->parse('package', $package);
323
	}
324
325
	/**
326
	 * Parse the BBC and smileys in user verification controls
327
	 *
328
	 * @param string $question
329
	 *
330
	 * @return string
331
	 */
332
	public function parseVerificationControls($question)
333
	{
334
		return $this->enableSmileys(true)->parse('package', $question);
335
	}
336
337
	/**
338
	 * Parse the BBC and smileys in moderator notices to users
339
	 *
340
	 * @param string $notice
341
	 *
342
	 * @return string
343
	 */
344
	public function parseNotice($notice)
345
	{
346
		return $this->enableSmileys(true)->parse('notice', $notice);
347
	}
348
349
	/**
350
	 * Parse the BBC and smileys in board descriptions
351
	 *
352
	 * @param string $board
353
	 *
354
	 * @return string
355
	 */
356 1
	public function parseBoard($board)
357
	{
358 1
		return $this->enableSmileys(true)->parse('board', $board);
359
	}
360
361
	/**
362
	 * Set the disabled tags
363
	 *
364
	 * @param string[] $disabled (usually from $modSettings['disabledBBC'])
365
	 *
366
	 * @return $this
367
	 */
368
	public function setDisabled(array $disabled)
369
	{
370
		foreach ($disabled as $tag)
371
		{
372
			$this->disabled[trim($tag)] = true;
373
		}
374
375
		return $this;
376
	}
377
378
	/**
379
	 * Return the bbc code definitions for the parser
380
	 *
381
	 * @return Codes
382
	 */
383 4
	public function getCodes()
384
	{
385 4
		if ($this->codes === null)
386 4
		{
387 1
			$additional_bbc = array();
388 1
			call_integration_hook('integrate_additional_bbc', array(&$additional_bbc));
389 1
			$this->codes = new Codes($additional_bbc, $this->disabled);
390 1
		}
391
392 4
		return $this->codes;
393
	}
394
395
	/**
396
	 * Return an instance of the bbc parser
397
	 *
398
	 * @return BBCParser
399
	 */
400 6
	public function getBBCParser()
401
	{
402 6
		if ($this->bbc_parser === null)
403 6
		{
404 1
			$this->bbc_parser = new BBCParser($this->getCodes(), $this->getAutolinkParser());
405 1
		}
406
407 6
		return $this->bbc_parser;
408
	}
409
410
	/**
411
	 * Return an, that's right not and, just an, like a single instance of the autolink parser
412
	 *
413
	 * @return Autolink
414
	 */
415 6
	public function getAutolinkParser()
416
	{
417 6
		if ($this->autolink_parser === null)
418 6
		{
419 1
			$this->autolink_parser = new Autolink($this->getCodes());
420 1
		}
421
422 6
		return $this->autolink_parser;
423
	}
424
425
	/**
426
	 * Return an, that's right not and, just an, like a single instance of the Smiley parser
427
	 *
428
	 * @return SmileyParser
429
	 */
430 6
	public function getSmileyParser()
431
	{
432 6
		global $context;
433
434 6
		if ($this->smiley_parser === null)
435 6
		{
436 1
			if (!isset($context['user']['smiley_path']))
437 1
			{
438
				loadUserContext();
439
			}
440
441 1
			$this->smiley_parser = new \BBC\SmileyParser($context['user']['smiley_path']);
442 1
			$this->smiley_parser->setEnabled($context['smiley_enabled']);
443 1
		}
444
445 6
		return $this->smiley_parser;
446
	}
447
448
	/**
449
	 * Return an, that's right not and, just an, like a single instance of the HTML parser
450
	 *
451
	 * @return HtmlParser
452
	 */
453 6
	public function getHtmlParser()
454
	{
455 6
		if ($this->html_parser === null)
456 6
		{
457 1
			$this->html_parser = new HtmlParser;
458 1
		}
459
460 6
		return $this->html_parser;
461
	}
462
}