diff options
author | Thomas Steur <thomas.steur@gmail.com> | 2015-09-03 10:55:43 +0300 |
---|---|---|
committer | Thomas Steur <thomas.steur@gmail.com> | 2015-09-22 12:04:34 +0300 |
commit | 01a93308b739a4031b76268b69e7bd0943d4c677 (patch) | |
tree | 4f55e49e549bfb72ebb29b11a7d57a46ccf644a1 /core/Plugin/Dimension | |
parent | 7be1092dbc11d71998291cfd42f96994a441c8b7 (diff) |
refs #8689 started working on PHP 7 support
Diffstat (limited to 'core/Plugin/Dimension')
-rw-r--r-- | core/Plugin/Dimension/VisitDimension.php | 49 |
1 files changed, 41 insertions, 8 deletions
diff --git a/core/Plugin/Dimension/VisitDimension.php b/core/Plugin/Dimension/VisitDimension.php index 48bcf70516..6932161d1b 100644 --- a/core/Plugin/Dimension/VisitDimension.php +++ b/core/Plugin/Dimension/VisitDimension.php @@ -309,7 +309,7 @@ abstract class VisitDimension extends Dimension } } - usort($instances, array('self', 'sortByRequiredFields')); + $instances = self::sortDimensions($instances); $cache->save($cacheId, $instances); } @@ -319,20 +319,53 @@ abstract class VisitDimension extends Dimension /** * @ignore + * @param VisitDimension[] $dimensions */ - public static function sortByRequiredFields($a, $b) + public static function sortDimensions($dimensions) { - $fields = $a->getRequiredVisitFields(); + $sorted = array(); + $exists = array(); + + // we first handle all the once without dependency + foreach ($dimensions as $index => $dimension) { + $fields = $dimension->getRequiredVisitFields(); + if (empty($fields)) { + $sorted[] = $dimension; + $exists[] = $dimension->getColumnName(); + unset($dimensions[$index]); + } + } - if (empty($fields)) { - return -1; + // find circular references + // and remove dependencies whose column cannot be resolved because it is not installed / does not exist / is defined by core + $depenencies = array(); + foreach ($dimensions as $dimension) { + $depenencies[$dimension->getColumnName()] = $dimension->getRequiredVisitFields(); } - if (in_array($b->columnName, $fields)) { - return 1; + foreach ($depenencies as $column => $fields) { + foreach ($fields as $key => $field) { + if (empty($depenencies[$field]) && !in_array($field, $exists)) { + // we cannot resolve that dependency as it does not exist + unset($depenencies[$column][$key]); + } elseif (!empty($depenencies[$field]) && in_array($column, $depenencies[$field])) { + throw new Exception("Circular reference detected for required field $field in dimension $column"); + } + } + } + + while (count($dimensions) > 0) { + foreach ($dimensions as $key => $dimension) { + $fields = $depenencies[$dimension->getColumnName()]; + if (count(array_intersect($fields, $exists)) === count($fields)) { + $sorted[] = $dimension; + $exists[] = $dimension->getColumnName(); + unset($dimensions[$key]); + } + } } - return 0; + return $sorted; } /** |