Skip to content

Commit

Permalink
fix. toolbar disable,view comment not remove (#3010)
Browse files Browse the repository at this point in the history
fix. toolbar disable, view comment not remove
  • Loading branch information
nyufeng authored Sep 3, 2020
1 parent 2a4f256 commit 811d48d
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 55 deletions.
154 changes: 100 additions & 54 deletions system/Filters/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ class Filters
'after' => [],
];

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

/**
* Any arguments to be passed to filtersClass.
*
* @var array
*/
protected $argumentsClass = [];

/**
* The original config file
*
Expand Down Expand Up @@ -179,72 +197,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;
}

if ($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;
}

if ($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 @@ -296,6 +291,9 @@ public function initialize(string $uri = null)
$this->filters['after'][] = 'toolbar';
}

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

$this->initialized = true;

return $this;
Expand All @@ -313,6 +311,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 @@ -380,16 +388,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 @@ -530,6 +544,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

0 comments on commit 811d48d

Please sign in to comment.