ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
MainToolCollector.php
Go to the documentation of this file.
2
3use Closure;
17
24{
25
29 private $information;
37 private $tools;
41 private $providers = [];
42
43
50 public function __construct(array $providers, ItemInformation $information = null)
51 {
52 $this->providers = $providers;
53 $this->information = $information;
54 $this->type_information_collection = new TypeInformationCollection();
55
56 // Tool
57 $tool = new TypeInformation(Tool::class, Tool::class, new ToolItemRenderer());
58 $tool->setCreationPrevented(true);
59 $this->type_information_collection->add($tool);
60
61 $tool = new TypeInformation(TreeTool::class, TreeTool::class, new TreeToolItemRenderer());
62 $tool->setCreationPrevented(true);
63 $this->type_information_collection->add($tool);
64
65 $this->tools = [];
66 }
67
68
69 public function collectStructure() : void
70 {
71 global $DIC;
72 $called_contexts = $DIC->globalScreen()->tool()->context()->stack();
73
74 $tools_to_merge = [];
75
76 foreach ($this->providers as $provider) {
77 $context_collection = $provider->isInterestedInContexts();
78 if ($context_collection->hasMatch($called_contexts)) {
79 $tools_to_merge[] = $provider->getToolsForContextStack($called_contexts);
80 }
81 }
82 $this->tools = array_merge([], ...$tools_to_merge);
83 }
84
85
86 public function filterItemsByVisibilty(bool $async_only = false) : void
87 {
88 $this->tools = array_filter($this->tools, $this->getVisibleFilter());
89 }
90
91 public function getSingleItem(IdentificationInterface $identification) : isToolItem
92 {
93 foreach ($this->tools as $tool) {
94 if ($tool->getProviderIdentification()->serialize() === $identification->serialize()) {
95 return $tool;
96 }
97 }
98 return new Tool($identification);
99 }
100
101
102 public function prepareItemsForUIRepresentation() : void
103 {
104 array_walk($this->tools, function (isToolItem $tool) {
105 $this->applyTypeInformation($tool);
106 });
107 }
108
109 public function cleanupItemsForUIRepresentation() : void
110 {
111 // TODO: Implement cleanupItemsForUIRepresentation() method.
112 }
113
114 public function sortItemsForUIRepresentation() : void
115 {
116 usort($this->tools, $this->getItemSorter());
117 }
118
119
123 public function getItemsForUIRepresentation() : \Generator
124 {
125 yield from $this->tools;
126 }
127
128
132 public function hasItems() : bool
133 {
134 return count($this->tools) > 0;
135 }
136
137
144 {
145 $item->setTypeInformation($this->getTypeInfoermationForItem($item));
146
147 return $item;
148 }
149
150
156 private function getTypeInfoermationForItem(isToolItem $item) : TypeInformation
157 {
161 $type = get_class($item);
162
163 return $this->type_information_collection->get($type);
164 }
165
166
170 private function getVisibleFilter() : Closure
171 {
172 return static function (isToolItem $tool) {
173 return ($tool->isAvailable() && $tool->isVisible());
174 };
175 }
176
177
181 private function getItemSorter() : Closure
182 {
183 return static function (isToolItem &$a, isToolItem &$b) {
184 return $a->getPosition() > $b->getPosition();
185 };
186 }
187}
An exception for terminatinating execution or to throw for unit testing.
getSingleItem(IdentificationInterface $identification)
__construct(array $providers, ItemInformation $information=null)
MainToolCollector constructor.
setTypeInformation(TypeInformation $information)
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
$type
$DIC
Definition: xapitoken.php:46