Skip to content

Commit

Permalink
refactor: extract setToolbarToLast()
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Nov 19, 2023
1 parent 71cc57a commit baeea0f
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions system/Filters/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,25 +266,10 @@ public function runRequired(string $position = 'before')
return $position === 'before' ? $this->request : $this->response;
}

// Set the toolbar filter to the last position to be executed
$afters = [];
$found = false;

foreach ($this->config->required['after'] as $alias) {
if ($alias === 'toolbar') {
$found = true;

continue;
}

$afters[] = $alias;
}

if ($found) {
$afters[] = 'toolbar';
}

$this->config->required['after'] = $afters;
// Set the toolbar filter to the last position to be executed
$this->config->required['after'] = $this->setToolbarToLast($this->config->required['after']);

$filterClasses = [];

Expand All @@ -308,6 +293,33 @@ public function runRequired(string $position = 'before')
return $this->runAfter($filterClasses[$position]);
}

/**
* Set the toolbar filter to the last position to be executed.
*
* @param list<string> $filters `after` filter array
*/
private function setToolbarToLast(array $filters): array
{
$afters = [];
$found = false;

foreach ($filters as $alias) {
if ($alias === 'toolbar') {
$found = true;

continue;
}

$afters[] = $alias;
}

if ($found) {
$afters[] = 'toolbar';
}

return $afters;
}

/**
* Runs through our list of filters provided by the configuration
* object to get them ready for use, including getting uri masks
Expand Down Expand Up @@ -342,13 +354,7 @@ public function initialize(?string $uri = null)
}

// Set the toolbar filter to the last position to be executed
if (in_array('toolbar', $this->filters['after'], true)
&& ($count = count($this->filters['after'])) > 1
&& $this->filters['after'][$count - 1] !== 'toolbar'
) {
array_splice($this->filters['after'], array_search('toolbar', $this->filters['after'], true), 1);
$this->filters['after'][] = 'toolbar';
}
$this->filters['after'] = $this->setToolbarToLast($this->filters['after']);

$this->processAliasesToClass('before');
$this->processAliasesToClass('after');
Expand Down

0 comments on commit baeea0f

Please sign in to comment.