ILIAS  release_8 Revision v8.24
MainToolCollector.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
23
37use Generator;
38
44{
50 private array $tools;
54 private array $providers;
55
61 public function __construct(array $providers, ItemInformation $information = null)
62 {
63 $this->providers = $providers;
64 $this->information = $information;
65 $this->type_information_collection = new TypeInformationCollection();
66
67 // Tool
68 $tool = new TypeInformation(Tool::class, Tool::class, new ToolItemRenderer());
69 $tool->setCreationPrevented(true);
70 $this->type_information_collection->add($tool);
71
72 $tool = new TypeInformation(TreeTool::class, TreeTool::class, new TreeToolItemRenderer());
73 $tool->setCreationPrevented(true);
74 $this->type_information_collection->add($tool);
75
76 $this->tools = [];
77 }
78
79 public function collectStructure(): void
80 {
81 global $DIC;
82 $called_contexts = $DIC->globalScreen()->tool()->context()->stack();
83
84 $tools_to_merge = [];
85
86 foreach ($this->providers as $provider) {
87 $context_collection = $provider->isInterestedInContexts();
88 if ($context_collection->hasMatch($called_contexts)) {
89 $tools_to_merge[] = $provider->getToolsForContextStack($called_contexts);
90 }
91 }
92 $this->tools = array_merge([], ...$tools_to_merge);
93 }
94
95 public function filterItemsByVisibilty(bool $async_only = false): void
96 {
97 $this->tools = array_filter($this->tools, $this->getVisibleFilter());
98 }
99
100 public function getSingleItem(IdentificationInterface $identification): isToolItem
101 {
102 foreach ($this->tools as $tool) {
103 if ($tool->getProviderIdentification()->serialize() === $identification->serialize()) {
104 return $tool;
105 }
106 }
107 return new Tool($identification);
108 }
109
110 public function prepareItemsForUIRepresentation(): void
111 {
112 array_walk($this->tools, function (isToolItem $tool): void {
113 $this->applyTypeInformation($tool);
114 });
115 }
116
117 public function cleanupItemsForUIRepresentation(): void
118 {
119 // TODO: Implement cleanupItemsForUIRepresentation() method.
120 }
121
122 public function sortItemsForUIRepresentation(): void
123 {
124 usort($this->tools, $this->getItemSorter());
125 }
126
127 public function getItemsForUIRepresentation(): Generator
128 {
129 yield from $this->tools;
130 }
131
132 public function hasItems(): bool
133 {
134 return count($this->tools) > 0;
135 }
136
137
138 public function hasVisibleItems(): bool
139 {
140 return $this->hasItems();
141 }
142
148 {
149 $item->setTypeInformation($this->getTypeInfoermationForItem($item));
150
151 return $item;
152 }
153
158 private function getTypeInfoermationForItem(isToolItem $item): TypeInformation
159 {
163 $type = get_class($item);
164
165 return $this->type_information_collection->get($type);
166 }
167
168 private function getVisibleFilter(): callable
169 {
170 return static function (isToolItem $tool): bool {
171 return ($tool->isAvailable() && $tool->isVisible());
172 };
173 }
174
175 private function getItemSorter(): callable
176 {
177 return static function (isToolItem $a, isToolItem $b): int {
178 return $a->getPosition() - $b->getPosition();
179 };
180 }
181}
getSingleItem(IdentificationInterface $identification)
__construct(array $providers, ItemInformation $information=null)
MainToolCollector constructor.
global $DIC
Definition: feed.php:28
setTypeInformation(TypeInformation $information)
$provider
Definition: ltitoken.php:83
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
$type