ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
MainToolCollector.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
23
36use Generator;
37
43{
48 private array $tools = [];
49
55 public function __construct(private array $providers, private ?ItemInformation $information = null)
56 {
57 $this->type_information_collection = new TypeInformationCollection();
58
59 // Tool
60 $tool = new TypeInformation(Tool::class, Tool::class, new ToolItemRenderer());
61 $tool->setCreationPrevented(true);
62 $this->type_information_collection->add($tool);
63
64 $tool = new TypeInformation(TreeTool::class, TreeTool::class, new TreeToolItemRenderer());
65 $tool->setCreationPrevented(true);
66 $this->type_information_collection->add($tool);
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 public function filterItemsByVisibilty(bool $async_only = false): void
86 {
87 $this->tools = array_filter($this->tools, $this->getVisibleFilter());
88 }
89
90 public function getSingleItem(IdentificationInterface $identification): isToolItem
91 {
92 foreach ($this->tools as $tool) {
93 if ($tool->getProviderIdentification()->serialize() === $identification->serialize()) {
94 return $tool;
95 }
96 }
97 return new Tool($identification);
98 }
99
100 public function prepareItemsForUIRepresentation(): void
101 {
102 array_walk($this->tools, function (isToolItem $tool): void {
103 $this->applyTypeInformation($tool);
104 });
105 }
106
107 public function cleanupItemsForUIRepresentation(): void
108 {
109 // TODO: Implement cleanupItemsForUIRepresentation() method.
110 }
111
112 public function sortItemsForUIRepresentation(): void
113 {
114 usort($this->tools, $this->getItemSorter());
115 }
116
117 public function getItemsForUIRepresentation(): Generator
118 {
119 yield from $this->tools;
120 }
121
122 public function hasItems(): bool
123 {
124 return $this->tools !== [];
125 }
126
127
128 public function hasVisibleItems(): bool
129 {
130 return $this->hasItems();
131 }
132
138 {
139 $item->setTypeInformation($this->getTypeInfoermationForItem($item));
140
141 return $item;
142 }
143
148 private function getTypeInfoermationForItem(isToolItem $item): TypeInformation
149 {
153 $type = $item::class;
154
155 return $this->type_information_collection->get($type);
156 }
157
158 private function getVisibleFilter(): callable
159 {
160 return static fn(isToolItem $tool): bool => $tool->isAvailable() && $tool->isVisible();
161 }
162
163 private function getItemSorter(): callable
164 {
165 return static fn(isToolItem $a, isToolItem $b): int => $a->getPosition() - $b->getPosition();
166 }
167}
__construct(private array $providers, private ?ItemInformation $information=null)
MainToolCollector constructor.
getSingleItem(IdentificationInterface $identification)
setTypeInformation(TypeInformation $information)
$provider
Definition: ltitoken.php:80
@noinspection PhpPropertyOnlyWrittenInspection
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
global $DIC
Definition: shib_login.php:26