Skip to content

Commit

Permalink
Changed Fluent model rules to be added before the silverstripe/cms mo…
Browse files Browse the repository at this point in the history
…del rule rather than before all rules
  • Loading branch information
UndefinedOffset committed Feb 5, 2019
1 parent b631f75 commit 07bf8fe
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/Extension/FluentDirectorExtension.php
Expand Up @@ -68,14 +68,10 @@ class FluentDirectorExtension extends Extension
public function updateRules(&$rules)
{
$originalRules = $rules;
$rules = $this->getExplicitRoutes($originalRules);
$fluentRules = $this->getExplicitRoutes($rules);

// Merge all other routes (maintain priority)
foreach ($originalRules as $key => $route) {
if (!isset($rules[$key])) {
$rules[$key] = $route;
}
}
// Insert Fluent Rules before the default '$URLSegment//$Action/$ID/$OtherID'
$rules = $this->insertRuleBefore($rules, '$URLSegment//$Action/$ID/$OtherID', $fluentRules);

$request = Injector::inst()->get(HTTPRequest::class);
if (!$request) {
Expand Down Expand Up @@ -159,4 +155,21 @@ protected function getRuleController($existingRule, $localeObj)
$this->owner->extend('updateLocalePageController', $controller, $localeObj);
return $controller;
}

/**
* Inserts the given rule(s) before another rule
* @param array $rules Array of rules to insert before
* @param string $key Rule to insert the new rules before
* @param array $rule New Rules to insert
* @return array Resulting array of rules
*/
protected function insertRuleBefore(array $rules, $key, array $rule)
{
$i = array_search($key, array_keys($rules));
if ($i !== false) {
return array_slice($rules, 0, $i, true) + $rule + array_slice($rules, $i, null, true);
}

return $rules;
}
}

0 comments on commit 07bf8fe

Please sign in to comment.