|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the sauls/widget package. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Saulius Vaičeliūnas <[email protected]> |
|
6
|
|
|
* @link http://saulius.vaiceliunas.lt |
|
7
|
|
|
* @copyright 2018 |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Sauls\Component\Widget; |
|
14
|
|
|
|
|
15
|
|
|
use function Sauls\Component\Helper\define_object; |
|
16
|
|
|
use Sauls\Component\Collection\ArrayCollection; |
|
17
|
|
|
use Sauls\Component\Collection\Collection; |
|
18
|
|
|
use Sauls\Component\OptionsResolver\OptionsResolver; |
|
19
|
|
|
|
|
20
|
|
|
abstract class Widget implements WidgetInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
private $id; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Collection |
|
29
|
|
|
*/ |
|
30
|
|
|
private $options; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
public static $prefix = 'w'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var int |
|
39
|
|
|
*/ |
|
40
|
|
|
public static $total = 0; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* |
|
44
|
|
|
* @throws \Exception |
|
45
|
|
|
*/ |
|
46
|
15 |
|
private function configure(array $options = []): void |
|
47
|
|
|
{ |
|
48
|
|
|
try { |
|
49
|
15 |
|
$resolver = new OptionsResolver; |
|
50
|
15 |
|
$this->configureOptions($resolver); |
|
51
|
15 |
|
$this->options = new ArrayCollection($resolver->resolve($this->resolveOptions($options))); |
|
52
|
13 |
|
define_object($this, $this->options->all()); |
|
53
|
13 |
|
$this->initialize(); |
|
54
|
3 |
|
} catch (\Exception $e) { |
|
55
|
3 |
|
throw $e; |
|
56
|
|
|
} |
|
57
|
12 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* This method is called after all options and variables are configured |
|
61
|
|
|
* You can safely initialise your variables, or other logic here |
|
62
|
|
|
*/ |
|
63
|
12 |
|
protected function initialize(): void |
|
64
|
|
|
{ |
|
65
|
12 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
abstract protected function configureOptions(OptionsResolver $resolver): void; |
|
68
|
|
|
|
|
69
|
15 |
|
public function resolveOptions(array $options): array |
|
70
|
|
|
{ |
|
71
|
15 |
|
return $options; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
public function getId($generate = true): string |
|
75
|
|
|
{ |
|
76
|
2 |
|
if ($generate && $this->id === null) { |
|
77
|
1 |
|
$this->id = static::$prefix.static::$total++; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
return $this->id; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
public function setId(string $value): void |
|
84
|
|
|
{ |
|
85
|
1 |
|
$this->id = $value; |
|
86
|
1 |
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
public function getOptionsCollection(): Collection |
|
89
|
|
|
{ |
|
90
|
1 |
|
return $this->options; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
1 |
|
public function getOptions(): array |
|
95
|
|
|
{ |
|
96
|
1 |
|
return $this->options->all(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
9 |
|
public function getOption($key, $default = null) |
|
100
|
|
|
{ |
|
101
|
9 |
|
return $this->options->get($key, $default); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
abstract public function render(): string; |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @throws \Exception |
|
109
|
|
|
*/ |
|
110
|
15 |
|
public function widget(array $options = []): WidgetInterface |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
15 |
|
$this->configure($options); |
|
113
|
|
|
|
|
114
|
12 |
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
9 |
|
public function __toString() |
|
118
|
|
|
{ |
|
119
|
|
|
try { |
|
120
|
9 |
|
$this->startAndFlushOutputBuffers(); |
|
121
|
|
|
try { |
|
122
|
9 |
|
$out = $this->render(); |
|
123
|
1 |
|
} catch (\Exception $e) { |
|
124
|
1 |
|
$this->closeNotClosedOutputBuffer(); |
|
125
|
1 |
|
throw $e; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
8 |
|
return $this->outputOutputBufferAndWidgetContent($out); |
|
129
|
1 |
|
} catch (\Exception $e) { |
|
130
|
1 |
|
return $e->getMessage(); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
9 |
|
private function startAndFlushOutputBuffers(): void |
|
135
|
|
|
{ |
|
136
|
9 |
|
ob_start(); |
|
137
|
9 |
|
ob_implicit_flush(false); |
|
|
|
|
|
|
138
|
9 |
|
} |
|
139
|
|
|
|
|
140
|
1 |
|
private function closeNotClosedOutputBuffer(): void |
|
141
|
|
|
{ |
|
142
|
1 |
|
if (ob_get_level() > 0) { |
|
143
|
1 |
|
ob_end_clean(); |
|
144
|
|
|
} |
|
145
|
1 |
|
} |
|
146
|
|
|
|
|
147
|
8 |
|
private function outputOutputBufferAndWidgetContent(string $out): string |
|
148
|
|
|
{ |
|
149
|
8 |
|
return ob_get_clean().$out; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|