Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix. toolbar disable,view comment not remove #3010

Merged
merged 16 commits into from
Sep 3, 2020
Merged
153 changes: 100 additions & 53 deletions system/Filters/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ class Filters
'after' => [],
];

/**
* The collection of filters' class names that will
* be used to execute in each position.
*
* @var array
*/
protected $filtersClass = [
nyufeng marked this conversation as resolved.
Show resolved Hide resolved
'before' => [],
'after' => [],
];

nyufeng marked this conversation as resolved.
Show resolved Hide resolved
/**
* Any arguments to be passed to filtersClass.
*
* @var array
*/
protected $argumentsClass = [];
nyufeng marked this conversation as resolved.
Show resolved Hide resolved

/**
* The original config file
*
Expand Down Expand Up @@ -138,71 +156,49 @@ public function run(string $uri, string $position = 'before')
{
$this->initialize(strtolower($uri));

foreach ($this->filters[$position] as $alias => $rules)
foreach ($this->filtersClass[$position] as $className)
{
if (is_numeric($alias) && is_string($rules))
{
$alias = $rules;
}
$class = new $className();

if (! array_key_exists($alias, $this->config->aliases))
if (! $class instanceof FilterInterface)
{
throw FilterException::forNoAlias($alias);
throw FilterException::forIncorrectInterface(get_class($class));
}

if (is_array($this->config->aliases[$alias]))
if ($position === 'before')
{
$classNames = $this->config->aliases[$alias];
}
else
{
$classNames = [$this->config->aliases[$alias]];
}
$result = $class->before($this->request, $this->argumentsClass[$className] ?? null);

foreach ($classNames as $className)
{
$class = new $className();

if (! $class instanceof FilterInterface)
if ($result instanceof RequestInterface)
{
throw FilterException::forIncorrectInterface(get_class($class));
$this->request = $result;
continue;
}

if ($position === 'before')
// If the response object was sent back,
// then send it and quit.
if ($result instanceof ResponseInterface)
{
$result = $class->before($this->request, $this->arguments[$alias] ?? null);

if ($result instanceof RequestInterface)
{
$this->request = $result;
continue;
}

// If the response object was sent back,
// then send it and quit.
if ($result instanceof ResponseInterface)
{
// short circuit - bypass any other filters
return $result;
}

// Ignore an empty result
if (empty($result))
{
continue;
}

// short circuit - bypass any other filters
return $result;
}
elseif ($position === 'after')

// Ignore an empty result
if (empty($result))
{
$result = $class->after($this->request, $this->response, $this->arguments[$alias] ?? null);
continue;
}

if ($result instanceof ResponseInterface)
{
$this->response = $result;
continue;
}
return $result;
}
elseif ($position === 'after')
{
$result = $class->after($this->request, $this->response, $this->argumentsClass[$className] ?? null);

if ($result instanceof ResponseInterface)
{
$this->response = $result;
continue;
}
}
}
Expand Down Expand Up @@ -250,6 +246,9 @@ public function initialize(string $uri = null)
$this->filters['after'][] = 'toolbar';
}

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

$this->initialized = true;

return $this;
Expand All @@ -267,6 +266,16 @@ public function getFilters(): array
return $this->filters;
}

/**
* Returns the filtersClass array.
*
* @return array
*/
public function getFiltersClass(): array
{
return $this->filtersClass;
}

/**
* Adds a new alias to the config file.
* MUST be called prior to initialize();
Expand Down Expand Up @@ -334,16 +343,22 @@ public function enableFilter(string $name, string $when = 'before')
throw FilterException::forNoAlias($name);
}

$classNames = (array) $this->config->aliases[$name];

foreach ($classNames as $className)
{
$this->argumentsClass[$className] = $this->arguments[$name] ?? null;
}

if (! isset($this->filters[$when][$name]))
{
$this->filters[$when][] = $name;
$this->filters[$when][] = $name;
$this->filtersClass[$when] = array_merge($this->filtersClass[$when], $classNames);
}

return $this;
}

//--------------------------------------------------------------------

/**
* Returns the arguments for a specified key, or all.
*
Expand Down Expand Up @@ -484,6 +499,38 @@ protected function processFilters(string $uri = null)
}
}

/**
* Maps filter aliases to the equivalent filter classes
*
* @throws FilterException
*
* @return void
*/
protected function processAliasesToClass(string $position)
{
foreach ($this->filters[$position] as $alias => $rules)
{
if (is_numeric($alias) && is_string($rules))
{
$alias = $rules;
}

if (! array_key_exists($alias, $this->config->aliases))
{
throw FilterException::forNoAlias($alias);
}

if (is_array($this->config->aliases[$alias]))
{
$this->filtersClass[$position] = array_merge($this->filtersClass[$position], $this->config->aliases[$alias]);
}
else
{
$this->filtersClass[$position][] = $this->config->aliases[$alias];
}
}
}

/**
* Check paths for match for URI
*
Expand Down
2 changes: 1 addition & 1 deletion system/View/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public function render(string $view, array $options = null, bool $saveData = nul

$this->logPerformance($this->renderVars['start'], microtime(true), $this->renderVars['view']);

if ($this->debug && (! isset($options['debug']) || $options['debug'] === true))
if (($this->debug && (! isset($options['debug']) || $options['debug'] === true)) && in_array('CodeIgniter\Filters\DebugToolbar', service('filters')->getFiltersClass()['after'], true))
{
$toolbarCollectors = config(\Config\Toolbar::class)->collectors;

Expand Down
Loading