Skip to content

Commit

Permalink
Allow adding class name string to middware queue.
Browse files Browse the repository at this point in the history
The strings are resolved to class instance when Middleware::get() is called.

Also for convenience add()/push() and prepend() can now take an array of middlewares.
  • Loading branch information
ADmad committed Jul 27, 2016
1 parent 3d1785e commit 06a9763
Showing 1 changed file with 63 additions and 15 deletions.
78 changes: 63 additions & 15 deletions src/Http/MiddlewareQueue.php
Expand Up @@ -14,8 +14,10 @@
*/
namespace Cake\Http;

use Cake\Core\App;
use Countable;
use LogicException;
use RuntimeException;

/**
* Provides methods for creating and manipulating a "queue" of middleware callables.
Expand All @@ -24,62 +26,108 @@
class MiddlewareQueue implements Countable
{
/**
* The queue of middleware callables.
* The queue of middlewares.
*
* @var array
*/
protected $queue = [];

/**
* Get the middleware object at the provided index.
* The queue of middleware callables.
*
* @var array
*/
protected $callables = [];

/**
* Get the middleware at the provided index.
*
* @param int $index The index to fetch.
* @return callable|null Either the callable middleware or null
* if the index is undefined.
*/
public function get($index)
{
if (isset($this->queue[$index])) {
return $this->queue[$index];
if (isset($this->callables[$index])) {
return $this->callables[$index];
}

return null;
return $this->resolve($index);
}

/**
* Resolve middleware name to callable.
*
* @param int $index The index to fetch.
* @return callable|null Either the callable middleware or null
* if the index is undefined.
*/
protected function resolve($index)
{
if (!isset($this->queue[$index])) {
return null;
}

if (is_string($this->queue[$index])) {
$class = $this->queue[$index];
$className = App::className($class, 'Middleware', 'Middleware');
if (!$className || !class_exists($className)) {
throw new RuntimeException(sprintf(
'Middleware "%s" was not found.',
$class
));
}

$callable = new $class;
} else {
$callable = $this->queue[$index];
}

return $this->callables[$index] = $callable;
}

/**
* Append a middleware callable to the end of the queue.
*
* @param callable $callable The middleware callable to append.
* @param callable|string|array $middleware The middleware(s) to append.
* @return $this
*/
public function add(callable $callable)
public function add($middleware)
{
$this->queue[] = $callable;
if (is_array($middleware)) {
return $this->queue = array_merge($this->queue, $middleware);
}

$this->queue[] = $middleware;

return $this;
}

/**
* Alias for MiddlewareQueue::add().
*
* @param callable $callable The middleware callable to append.
* @param callable|string|array $middleware The middleware(s) to append.
* @return $this
* @see MiddlewareQueue::add()
*/
public function push(callable $callable)
public function push($middleware)
{
return $this->add($callable);
return $this->add($middleware);
}

/**
* Prepend a middleware callable to the start of the queue.
* Prepend a middleware to the start of the queue.
*
* @param callable $callable The middleware callable to prepend.
* @param callable|string|array $middleware The middleware(s) to prepend.
* @return $this
*/
public function prepend(callable $callable)
public function prepend($middleware)
{
array_unshift($this->queue, $callable);
if (is_array($middleware)) {
return $this->queue = array_merge($middleware, $this->queue);
}

array_unshift($this->queue, $middleware);

return $this;
}
Expand Down

0 comments on commit 06a9763

Please sign in to comment.