/** * Sorts the YAML fragments so that the "before" and "after" rules are met. * Throws an error if there's a loop * * We can't use regular sorts here - we need a topological sort. Easiest * way is with a DAG, so build up a DAG based on the before/after rules, then * sort that. * * @return void */ protected function sortYamlFragments() { $frags = $this->yamlConfigFragments; // Build a directed graph $dag = new SS_DAG($frags); foreach ($frags as $i => $frag) { foreach ($frags as $j => $otherfrag) { if ($i == $j) continue; $order = $this->relativeOrder($frag, $otherfrag); if ($order == 'before') $dag->addedge($i, $j); elseif ($order == 'after') $dag->addedge($j, $i); } } $this->yamlConfigFragments = $dag->sort(); }
/** * Sorts the YAML fragments so that the "before" and "after" rules are met. * Throws an error if there's a loop * * We can't use regular sorts here - we need a topological sort. Easiest * way is with a DAG, so build up a DAG based on the before/after rules, then * sort that. * * @return void */ protected function sortYamlFragments() { $frags = $this->yamlConfigFragments; // Build a directed graph $dag = new SS_DAG($frags); foreach ($frags as $i => $frag) { foreach ($frags as $j => $otherfrag) { if ($i == $j) { continue; } $order = $this->relativeOrder($frag, $otherfrag); if ($order == 'before') { $dag->addedge($i, $j); } elseif ($order == 'after') { $dag->addedge($j, $i); } } } try { $this->yamlConfigFragments = $dag->sort(); } catch (SS_DAG_CyclicException $e) { if (!Director::isLive() && isset($_REQUEST['debug'])) { $res = '<h1>Remaining config fragment graph</h1>'; $res .= '<dl>'; foreach ($e->dag as $node) { $res .= "<dt>{$node['from']['module']}/{$node['from']['file']}#{$node['from']['name']}" . " marked to come after</dt><dd><ul>"; foreach ($node['to'] as $to) { $res .= "<li>{$to['module']}/{$to['file']}#{$to['name']}</li>"; } $res .= "</ul></dd>"; } $res .= '</dl>'; echo $res; } user_error('Based on their before & after rules two fragments both need to be before/after each other', E_USER_ERROR); } }