|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of laravel.su package. |
|
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
5
|
|
|
* file that was distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types = 1); |
|
8
|
|
|
|
|
9
|
|
|
namespace App\Services\ContentRenderer; |
|
10
|
|
|
|
|
11
|
|
|
use Illuminate\Events\Dispatcher; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Renderer. |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class AbstractRenderer implements ContentRenderInterface |
|
17
|
|
|
{ |
|
18
|
|
|
private const EVENT_PARSE_BEFORE = 'parse.before'; |
|
19
|
|
|
private const EVENT_PARSE_AFTER = 'parse.after'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var array|\Closure[] |
|
23
|
|
|
*/ |
|
24
|
|
|
private $before = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array|\Closure[] |
|
28
|
|
|
*/ |
|
29
|
|
|
private $after = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param \Closure $callback |
|
33
|
|
|
* @return $this|ContentRenderInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
public function before(\Closure $callback): ContentRenderInterface |
|
36
|
|
|
{ |
|
37
|
|
|
$this->before[] = $callback; |
|
38
|
|
|
|
|
39
|
|
|
return $this; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param \Closure $callback |
|
44
|
|
|
* @return $this|ContentRenderInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
public function after(\Closure $callback): ContentRenderInterface |
|
47
|
|
|
{ |
|
48
|
|
|
$this->after[] = $callback; |
|
49
|
|
|
|
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $body |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function fireBefore(string $body): string |
|
58
|
|
|
{ |
|
59
|
|
|
foreach ($this->before as $before) { |
|
60
|
|
|
$body = $this->parseEventsOutput($body, $before($body)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $body; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $body |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function fireAfter(string $body): string |
|
71
|
|
|
{ |
|
72
|
|
|
foreach ($this->after as $after) { |
|
73
|
|
|
$body = $this->parseEventsOutput($body, $after($body)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $body; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $original |
|
81
|
|
|
* @param string|mixed $result |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
|
private function parseEventsOutput(string $original, $result): string |
|
85
|
|
|
{ |
|
86
|
|
|
if (is_string($result)) { |
|
87
|
|
|
return $result; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $original; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|