/
ControlRegistry.php
93 lines (81 loc) · 2.9 KB
/
ControlRegistry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace Concrete\Core\Form\Context\Registry;
use Concrete\Core\Application\Application;
use Concrete\Core\Express\Form\Context\FormContext;
use Concrete\Core\Express\Form\Context\ViewContext;
use Concrete\Core\Express\Form\Control\View\AssociationFormView;
use Concrete\Core\Express\Form\Control\View\AssociationView;
use Concrete\Core\Form\Context\ContextInterface;
use Concrete\Core\Express\Form\Control\View\AttributeKeyFormView;
use Concrete\Core\Express\Form\Control\View\AttributeKeyView;
/**
* A simple class for registering context to view bindings, in the event that certain contexts ought to
* deliver different views. (Used by Express Attribute Key View vs Form)
*/
class ControlRegistry
{
public function __construct()
{
$this->registerControl('express_control_attribute_key', [
[new FormContext(), AttributeKeyFormView::class],
[new ViewContext(), AttributeKeyView::class],
]);
$this->registerControl('express_control_association', [
[new FormContext(), AssociationFormView::class],
[new ViewContext(), AssociationView::class],
]);
}
/**
* @var ControlEntry[]
*/
protected $entries = [];
public function registerControl($handle, $entries)
{
foreach($entries as $row) {
$this->register($row[0], $handle, $row[1]);
}
}
public function register(ContextInterface $context, $handle, $viewClass)
{
$entry = new ControlEntry($context, $handle, $viewClass);
$this->addOrReplaceEntry($entry);
}
protected function addOrReplaceEntry(ControlEntry $entry)
{
$index = null;
foreach($this->entries as $key => $existingEntry) {
if ($entry->getHandle() == $existingEntry->getHandle()) {
if (get_class($existingEntry->getContext()) == get_class($entry->getContext())) {
$index = $key;
}
}
}
if ($index) {
$this->entries[$index] = $entry;
} else {
$this->entries[] = $entry;
}
}
protected function getEntryFromContext(ContextInterface $context, $handle)
{
foreach($this->entries as $key => $existingEntry) {
if ($handle == $existingEntry->getHandle()) {
$entryContext = $existingEntry->getContext();
if ($context instanceof $entryContext) {
return $existingEntry;
}
}
}
}
public function getControlView(ContextInterface $context, $handle, $arguments = array())
{
$entry = $this->getEntryFromContext($context, $handle);
array_unshift($arguments, $context);
if (is_object($entry)) {
return call_user_func_array(
[new \ReflectionClass($entry->getViewClass()), 'newInstance'],
$arguments
);
}
}
}