Created
June 9, 2018 18:34
-
-
Save mortenson/ad473579e506aff053f2395f22bdb9f4 to your computer and use it in GitHub Desktop.
Sort a Drupal graph into chunks of dependencies that can be installed concurrently
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Drupal\Component\Graph; | |
function chunk_graph(Graph $graph_object) { | |
// Sort the graph by weight. | |
$graph = $graph_object->searchAndSort(); | |
uasort($graph, 'Drupal\Component\Utility\SortArray::sortByWeightElement'); | |
// Ensure that graph nodes are sorted from least to most edges. | |
$graph = array_reverse($graph); | |
$all_imported = []; | |
$chunked_graph = []; | |
// Traverse the ordered graph and chunk it into sets that can be installed | |
// concurrently. | |
while ($graph) { | |
$chunk = []; | |
foreach ($graph as $i => $node) { | |
$edges = array_keys($node['edges']); | |
if (count(array_intersect($edges, $all_imported)) === count($edges)) { | |
$chunk[] = $i; | |
unset($graph[$i]); | |
} | |
} | |
$all_imported = array_merge($all_imported, $chunk); | |
$chunked_graph[] = $chunk; | |
} | |
return $chunked_graph; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment