From df0d65b1e044bc6b457291cfcf38b5d1a02964fe Mon Sep 17 00:00:00 2001 From: David Jensen Date: Wed, 3 May 2017 14:29:21 +0200 Subject: [PATCH] Proper ordering of dependecies ...so script and style tags render in proper order. --- lib/Collection.php | 21 +++++++++++++++++++-- lib/Component.php | 7 ++++++- test/unit/ComponentTest.php | 2 +- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/Collection.php b/lib/Collection.php index 7b5f8dd..cc5c092 100644 --- a/lib/Collection.php +++ b/lib/Collection.php @@ -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() { diff --git a/lib/Component.php b/lib/Component.php index 07f26b3..c506a34 100644 --- a/lib/Component.php +++ b/lib/Component.php @@ -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(); @@ -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()); diff --git a/test/unit/ComponentTest.php b/test/unit/ComponentTest.php index 0865783..af13c07 100644 --- a/test/unit/ComponentTest.php +++ b/test/unit/ComponentTest.php @@ -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() {