Skip to content

Commit

Permalink
Merge pull request #15 from DiversityTemplating/proper-order
Browse files Browse the repository at this point in the history
Proper ordering of dependecies
  • Loading branch information
davidlgj committed May 4, 2017
2 parents 5e7c825 + df0d65b commit d830971
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
21 changes: 19 additions & 2 deletions lib/Collection.php
Expand Up @@ -59,15 +59,32 @@ public function renderStyleTags() {
return $tags;
}

/**
* Return an ordered array of all components. Dependencies up front before their dependees.
*/
public function getAllComponents() {
$all_components = array();

foreach ($this->components as $component) {
$all_components = array_merge($all_components, $component->getDependencies());
$all_components[$component->name] = $component;
$all_components = array_merge($all_components, $component->getDependencies());
}

// First we reverse the list to get the correct dependency oredering, i.e. deps upfront
$all_components = array_reverse($all_components);

// Then we need to remove duplicate components, while keeping order.
$lookup = array();
$filtered = array();

foreach ($all_components as $component) {
if (!isset($lookup[$component->name])) {
$lookup[$component->name] = true;
$filtered[] = $component;
}
}

return $all_components;
return $filtered;
}

public function needsAngular() {
Expand Down
7 changes: 6 additions & 1 deletion lib/Component.php
Expand Up @@ -34,6 +34,11 @@ public function __construct(Factory $factory, $component_data) {
}
}

/**
* Get all the dependencies of a component, and their dependencies, in an ordered array.
* Duplicates may occur when two different dependencies in
* turn depends on the same component.
*/
public function getDependencies() {
$dependencies = array();

Expand All @@ -42,7 +47,7 @@ public function getDependencies() {
foreach ($this->spec->dependencies as $name => $spec) {
// Assume $spec is a version.
$component = $this->factory->get($name, $spec);
$dependencies[$name] = $component;
$dependencies[] = $component;

// Add the dependent components dependencies.
$dependencies = array_merge($dependencies, $component->getDependencies());
Expand Down
2 changes: 1 addition & 1 deletion test/unit/ComponentTest.php
Expand Up @@ -19,7 +19,7 @@ public function testDependencies() {
$component = self::$factory->get('test_3');
$dependencies = $component->getDependencies();

$this->assertEquals('test_1', $dependencies['test_1']->name);
$this->assertEquals('test_1', $dependencies[0]->name);
}

public function testGetStyles() {
Expand Down

0 comments on commit d830971

Please sign in to comment.