1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Biurad opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.2 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Biurad\UI\Helper; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* SlotsHelper manages template slots. |
22
|
|
|
* |
23
|
|
|
* @author Fabien Potencier <[email protected]> |
24
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class SlotsHelper extends AbstractHelper |
27
|
|
|
{ |
28
|
|
|
/** @var array<string,mixed> */ |
29
|
|
|
protected $slots = []; |
30
|
|
|
|
31
|
|
|
/** @var array<int,string> */ |
32
|
|
|
protected $openSlots = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Starts a new slot. |
36
|
|
|
* |
37
|
|
|
* This method starts an output buffer that will be |
38
|
|
|
* closed when the stop() method is called. |
39
|
|
|
* |
40
|
|
|
* @throws \InvalidArgumentException if a slot with the same name is already started |
41
|
|
|
*/ |
42
|
|
|
public function start(string $name): void |
43
|
|
|
{ |
44
|
|
|
if (\in_array($name, $this->openSlots, true)) { |
45
|
|
|
throw new \InvalidArgumentException(\sprintf('A slot named "%s" is already started.', $name)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$this->openSlots[] = $name; |
49
|
|
|
$this->slots[$name] = ''; |
50
|
|
|
|
51
|
|
|
\ob_start(); |
52
|
|
|
\ob_implicit_flush(\PHP_VERSION_ID >= 80000 ? false : 0); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Stops a slot. |
57
|
|
|
* |
58
|
|
|
* @throws \LogicException if no slot has been started |
59
|
|
|
*/ |
60
|
|
|
public function stop(): void |
61
|
|
|
{ |
62
|
|
|
if (!$this->openSlots) { |
|
|
|
|
63
|
|
|
throw new \LogicException('No slot started.'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$name = \array_pop($this->openSlots); |
67
|
|
|
|
68
|
|
|
$this->slots[$name] = \ob_get_clean(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns true if the slot exists. |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public function has(string $name) |
77
|
|
|
{ |
78
|
|
|
return isset($this->slots[$name]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Gets the slot value. |
83
|
|
|
* |
84
|
|
|
* @param bool|string $default The default slot content |
85
|
|
|
* |
86
|
|
|
* @return string|false The slot content |
87
|
|
|
*/ |
88
|
5 |
|
public function get(string $name, $default = false) |
89
|
|
|
{ |
90
|
5 |
|
return $this->slots[$name] ?? $default; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Sets a slot value. |
95
|
|
|
*/ |
96
|
5 |
|
public function set(string $name, string $content): void |
97
|
|
|
{ |
98
|
5 |
|
$this->slots[$name] = $content; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Outputs a slot. |
103
|
|
|
* |
104
|
|
|
* @param bool|string $default The default slot content |
105
|
|
|
* |
106
|
|
|
* @return bool true if the slot is defined or if a default content has been provided, false otherwise |
107
|
|
|
*/ |
108
|
|
|
public function output(string $name, $default = false): bool |
109
|
|
|
{ |
110
|
|
|
if (!isset($this->slots[$name])) { |
111
|
|
|
if (false !== $default) { |
112
|
|
|
echo $default; |
|
|
|
|
113
|
|
|
|
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
echo $this->slots[$name]; |
121
|
|
|
|
122
|
|
|
return true; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
6 |
|
public function getName(): string |
129
|
|
|
{ |
130
|
6 |
|
return 'slots'; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|