|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* 2017 Romain CANON <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the TYPO3 FormZ project. |
|
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
|
7
|
|
|
* under the terms of the GNU General Public License, either |
|
8
|
|
|
* version 3 of the License, or any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, see: |
|
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Romm\Formz\Service\ViewHelper; |
|
15
|
|
|
|
|
16
|
|
|
use Closure; |
|
17
|
|
|
use Romm\Formz\Exceptions\EntryNotFoundException; |
|
18
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
|
19
|
|
|
|
|
20
|
|
|
class SlotViewHelperService implements SingletonInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Contains the closures which will render the registered slots. The keys |
|
24
|
|
|
* of this array are the names of the slots. |
|
25
|
|
|
* |
|
26
|
|
|
* @var Closure[] |
|
27
|
|
|
*/ |
|
28
|
|
|
private $closures = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array[] |
|
32
|
|
|
*/ |
|
33
|
|
|
private $arguments = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Adds a closure - which will render the slot with the given name - to the |
|
37
|
|
|
* private storage in this class. |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $name |
|
40
|
|
|
* @param Closure $closure |
|
41
|
|
|
* @param array $arguments |
|
42
|
|
|
*/ |
|
43
|
|
|
public function addSlot($name, Closure $closure, array $arguments) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->closures[$name] = $closure; |
|
46
|
|
|
$this->arguments[$name] = $arguments; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Returns the closure which will render the slot with the given name. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $name |
|
53
|
|
|
* @return Closure |
|
54
|
|
|
* @throws EntryNotFoundException |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getSlotClosure($name) |
|
57
|
|
|
{ |
|
58
|
|
|
if (false === $this->hasSlot($name)) { |
|
59
|
|
|
throw EntryNotFoundException::slotClosureSlotNotFound($name); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $this->closures[$name]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Returns the closure which will render the slot with the given name. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $name |
|
69
|
|
|
* @return array |
|
70
|
|
|
* @throws EntryNotFoundException |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getSlotArguments($name) |
|
73
|
|
|
{ |
|
74
|
|
|
if (false === $this->hasSlot($name)) { |
|
75
|
|
|
throw EntryNotFoundException::slotArgumentsSlotNotFound($name); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $this->arguments[$name]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $name |
|
83
|
|
|
* @return bool |
|
84
|
|
|
*/ |
|
85
|
|
|
public function hasSlot($name) |
|
86
|
|
|
{ |
|
87
|
|
|
return true === isset($this->closures[$name]); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Resets the list of closures. |
|
92
|
|
|
*/ |
|
93
|
|
|
public function resetState() |
|
94
|
|
|
{ |
|
95
|
|
|
$this->closures = []; |
|
96
|
|
|
$this->arguments = []; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|