/** * sortAssets method * * @param array|Collection $all * * @return \Codex\Support\Collection */ protected function sorter($all = []) { /** @var Collection $all */ $all = $all instanceof Collection ? $all : new Collection($all); $sorter = new Sorter(); $sorted = new Collection(); foreach ($all as $name => $asset) { $sorter->addItem($asset['name'], $asset['depends']); } foreach ($sorter->sort() as $name) { $sorted->add($all->where('name', $name)->first()); } return $sorted; }
public function getSorted($names) { $all = $this->whereIn('name', $names)->sortBy('priority')->replaceProcessors(); $all->each(function ($processor) use($all) { /** @var Processor $annotation */ $annotation = $processor['annotation']; foreach ($annotation->before as $before) { $otherProcessor = $all->where('name', $before)->first(); if ($otherProcessor !== null && false === in_array($annotation->name, $otherProcessor['after'], true)) { $otherProcessor['after'][] = $annotation->name; $all->set($before, $otherProcessor); } } }); $sorter = new Sorter(); foreach ($all as $item) { $sorter->addItem($item['name'], $item['after']); } $sorted = $sorter->sort(); if (count($sorter->getMissing()) > 0) { if (static::$handleMissing === self::MISSING_IGNORED) { } elseif (static::$handleMissing === self::MISSING_THROWS_EXECEPTION) { $dep = array_keys($sorter->getMissing()); $dep = implode(', ', $dep); throw CodexException::because("Sorter encountered a missing dependency for {$dep}"); } } $sorted = array_merge($sorted, array_diff($names, $sorted)); return $sorted; }
/** * getSorted method * * @return PluginCollection * @throws \Codex\Exception\CodexException */ public function sorted() { $sorter = new Sorter(); foreach ($this->all() as $plugin) { $sorter->addItem($plugin->name, $plugin->requires); } return (new static($sorter->sort()))->transform(function ($name) { return $this->get($name); }); }