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
139 changes: 86 additions & 53 deletions system/Filters/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class Filters
'after' => [],
];

protected $filtersClass = [
nyufeng marked this conversation as resolved.
Show resolved Hide resolved
'before' => [],
'after' => [],
];

nyufeng marked this conversation as resolved.
Show resolved Hide resolved
/**
* The original config file
*
Expand Down Expand Up @@ -138,71 +143,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]))
{
$classNames = $this->config->aliases[$alias];
}
else
if ($position === 'before')
{
$classNames = [$this->config->aliases[$alias]];
}

foreach ($classNames as $className)
{
$class = new $className();
$result = $class->before($this->request, $this->argumentsClass[$className] ?? null);

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 @@ -239,6 +222,8 @@ public function initialize(string $uri = null)
$this->processGlobals($uri);
$this->processMethods();
$this->processFilters($uri);
$this->processAliasesToClass('before');
$this->processAliasesToClass('after');

$this->initialized = true;

Expand All @@ -257,6 +242,11 @@ public function getFilters(): array
return $this->filters;
}

public function getFiltersClass() :array
nyufeng marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->filtersClass;
}

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

if (is_array($this->config->aliases[$name]))
{
$classNames = $this->config->aliases[$name];
}
else
{
$classNames = [$this->config->aliases[$name]];
}
nyufeng marked this conversation as resolved.
Show resolved Hide resolved

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 @@ -474,6 +477,36 @@ protected function processFilters(string $uri = null)
}
}

/**
* filter alias to class
*
* @return type
nyufeng marked this conversation as resolved.
Show resolved Hide resolved
*/
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 @@ -269,7 +269,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