ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
MainToolCollector.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
22 
36 use Generator;
37 
43 {
47  private $information;
55  private $tools;
59  private $providers;
60 
66  public function __construct(array $providers, ItemInformation $information = null)
67  {
68  $this->providers = $providers;
69  $this->information = $information;
70  $this->type_information_collection = new TypeInformationCollection();
71 
72  // Tool
73  $tool = new TypeInformation(Tool::class, Tool::class, new ToolItemRenderer());
74  $tool->setCreationPrevented(true);
75  $this->type_information_collection->add($tool);
76 
77  $tool = new TypeInformation(TreeTool::class, TreeTool::class, new TreeToolItemRenderer());
78  $tool->setCreationPrevented(true);
79  $this->type_information_collection->add($tool);
80 
81  $this->tools = [];
82  }
83 
84  public function collectStructure() : void
85  {
86  global $DIC;
87  $called_contexts = $DIC->globalScreen()->tool()->context()->stack();
88 
89  $tools_to_merge = [];
90 
91  foreach ($this->providers as $provider) {
92  $context_collection = $provider->isInterestedInContexts();
93  if ($context_collection->hasMatch($called_contexts)) {
94  $tools_to_merge[] = $provider->getToolsForContextStack($called_contexts);
95  }
96  }
97  $this->tools = array_merge([], ...$tools_to_merge);
98  }
99 
100  public function filterItemsByVisibilty(bool $async_only = false) : void
101  {
102  $this->tools = array_filter($this->tools, $this->getVisibleFilter() ?? function ($v, $k) : bool {
103  return !empty($v);
104  }, $this->getVisibleFilter() === null ? ARRAY_FILTER_USE_BOTH : 0);
105  }
106 
107  public function getSingleItem(IdentificationInterface $identification) : isToolItem
108  {
109  foreach ($this->tools as $tool) {
110  if ($tool->getProviderIdentification()->serialize() === $identification->serialize()) {
111  return $tool;
112  }
113  }
114  return new Tool($identification);
115  }
116 
117  public function prepareItemsForUIRepresentation() : void
118  {
119  array_walk($this->tools, function (isToolItem $tool) : void {
120  $this->applyTypeInformation($tool);
121  });
122  }
123 
124  public function cleanupItemsForUIRepresentation() : void
125  {
126  // TODO: Implement cleanupItemsForUIRepresentation() method.
127  }
128 
129  public function sortItemsForUIRepresentation() : void
130  {
131  usort($this->tools, $this->getItemSorter());
132  }
133 
135  {
136  yield from $this->tools;
137  }
138 
139  public function hasItems() : bool
140  {
141  return count($this->tools) > 0;
142  }
143 
144 
145  public function hasVisibleItems() : bool
146  {
147  return $this->hasItems();
148  }
149 
154  private function applyTypeInformation(isToolItem $item) : isToolItem
155  {
156  $item->setTypeInformation($this->getTypeInfoermationForItem($item));
157 
158  return $item;
159  }
160 
165  private function getTypeInfoermationForItem(isToolItem $item) : TypeInformation
166  {
170  $type = get_class($item);
171 
172  return $this->type_information_collection->get($type);
173  }
174 
175  private function getVisibleFilter() : callable
176  {
177  return static function (isToolItem $tool) : bool {
178  return ($tool->isAvailable() && $tool->isVisible());
179  };
180  }
181 
182  private function getItemSorter() : callable
183  {
184  return static function (isToolItem $a, isToolItem $b) : int {
185  return $a->getPosition() - $b->getPosition();
186  };
187  }
188 }
PhpPropertyOnlyWrittenInspection
$type
setTypeInformation(TypeInformation $information)
getPosition()
Return the default position for installation, this will be overridden by the configuration later...
global $DIC
Definition: goto.php:24
__construct(array $providers, ItemInformation $information=null)
MainToolCollector constructor.
getSingleItem(IdentificationInterface $identification)
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples