/** * @return MoodlePluginCollection */ public function sortByDependencies() { $elements = []; foreach ($this->items as $item) { $elements[$item->getComponent()] = []; } // Loop through a second time, only adding dependencies that exist in our list. foreach ($this->items as $item) { $dependencies = $item->getDependencies(); foreach ($dependencies as $dependency) { if (array_key_exists($dependency, $elements)) { $elements[$item->getComponent()][] = $dependency; } } } $sorter = new StringSort($elements); $results = $sorter->sort(); $sorted = new self(); foreach ($results as $result) { foreach ($this->items as $item) { if ($result === $item->getComponent()) { $sorted->add($item); break; } } } if ($this->count() !== $sorted->count()) { throw new \LogicException('The sorted list of plugins does not match the size of original list'); } return $sorted; }
public function sort($dependents) { $topologicalSort = new StringSort(); // build graph for topological sort foreach ($dependents as $name => $dependent) { $topologicalSort->add($name, $dependent instanceof Dependent ? $dependent->depends() : []); } // sort graph $sortedList = $topologicalSort->sort(); $sortedComponents = []; //translate sort graph foreach ($sortedList as $name) { $sortedComponents[$name] = $dependents[$name]; } return $sortedComponents; }
/** * Sort topological and resolve dependencies * * @param $components * @return array|\string[] */ private function sort($components) { $topologicalSort = new StringSort(); // build graph for topological sort foreach ($components as $name => $component) { /** @var Component $component */ $topologicalSort->add($name, $component->depends()); } // sort graph $sortedList = $topologicalSort->sort(); $sortedComponents = []; //translate sort graph foreach ($sortedList as $name) { $sortedComponents[$name] = $components[$name]; } return $sortedComponents; }