Skip to content
This repository has been archived by the owner on Nov 13, 2019. It is now read-only.

Commit

Permalink
fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
phossa committed Sep 24, 2016
1 parent e3dde5c commit 8d49f8f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 34 deletions.
76 changes: 47 additions & 29 deletions src/Phossa2/Middleware/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,32 @@ class Queue extends ObjectAbstract implements QueueInterface
*/
public function __construct(array $middlewares = [])
{
// create the queue
$this->queue = new \SplQueue();

foreach ($middlewares as $mw) {
if (is_array($mw)) { // with condition
$this->push($mw[0], $mw[1]);
} else { // without condition
$this->push($mw);
}
}
// fill the queue with middlewares
$this->fillTheQueue($middlewares);
}

/**
* For compatiblity with other middlewares
* Compatible with middlewares of the signature
*
* ```php
* fn($request, $response, callable $next)
* ```
*
* @param RequestInterface $request
* @param ResponseInterface $response
* @param DelegateInterface $next
* @return ResponseInterface
* @access public
*/
public function __invoke(
RequestInterface $request,
ResponseInterface $response
ResponseInterface $response,
DelegateInterface $next = null
)/*# : ResponseInterface */ {
return $this->next($request, $response);
return $this->process($request, $response, $next);
}

/**
Expand All @@ -82,6 +84,7 @@ public function __invoke(
public function push($middleware, $condition = null)
{
$this->queue->push([$middleware, $condition]);
$this->queue->rewind();
return $this;
}

Expand All @@ -93,17 +96,11 @@ public function process(
ResponseInterface $response,
DelegateInterface $next = null
)/*# : ResponseInterface */ {
// rewind
$this->queue->rewind();

// process the queue
$response = $this->next($request, $response);

if ($next) { // queue is part of another queue
return $next->next($request, $response);
} else {
return $response;
}
// $next is parent queue
return $next ? $next->next($request, $response) : $response;
}

/**
Expand All @@ -125,7 +122,28 @@ public function next(
return $this->next($request, $response);
}
}
return $response; // end of the queue reached
$this->queue->rewind();
return $response;
}

/**
* Fill the queue with middlewares
*
* @param array $middlewares
* @access protected
*/
protected function fillTheQueue(array $middlewares)
{
foreach ($middlewares as $mw) {
// with conditions specified
if (is_array($mw)) {
$this->push($mw[0], $mw[1]);

// no condition
} else {
$this->push($mw);
}
}
}

/**
Expand All @@ -143,14 +161,14 @@ protected function runMiddleware(
RequestInterface $request,
ResponseInterface $response
)/*# : ResponseInterface */ {
// old style callable
if (is_callable($middleware)) {
return $middleware($request, $response, $this);

// instance of MiddlewareInterface
} elseif (is_object($middleware) && $middleware instanceof MiddlewareInterface) {
if (is_object($middleware) && $middleware instanceof MiddlewareInterface) {
return $middleware->process($request, $response, $this);

// old style callable
} elseif (is_callable($middleware)) {
return $middleware($request, $response, $this);

// unknown middleware type
} else {
throw new LogicException(
Expand Down Expand Up @@ -178,14 +196,14 @@ protected function evalCondition(
RequestInterface $request,
ResponseInterface $response
)/*# : bool */ {
// old style callable
if (is_callable($condition)) {
return $condition($request, $response);

// instanceof ConditionInterface
} elseif (is_object($condition) && $condition instanceof ConditionInterface) {
if (is_object($condition) && $condition instanceof ConditionInterface) {
return $condition->evaluate($request, $response);

// old style callable
} elseif (is_callable($condition)) {
return $condition($request, $response);

// unknown type
} else {
throw new LogicException(
Expand Down
44 changes: 39 additions & 5 deletions tests/src/Phossa2/Middleware/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,23 @@ protected function tearDown()
*/
public function test__construct()
{
// construct with array of middlewares
$object = new Queue($this->data);
$this->expectOutputString("MW_1_S MW_2_S MW_2_E MW_1_E ");
$object->process($this->createRequest('/test'), $this->createResponse());
}

/**
* Tests Queue->__invoke()
*
* @cover Phossa2\Middleware\Queue::__invoke()
*/
public function test__invoke()
{
// invoke as callable
$object = new Queue($this->data);
$this->expectOutputString("MW_1_S MW_2_S MW_2_E MW_1_E ");
$object($this->createRequest('/test'), $this->createResponse());
}

/**
Expand All @@ -75,26 +82,53 @@ public function test__invoke()
*/
public function testPush()
{
// no condition
// push one by one
foreach ($this->data as $mw) {
$this->object->push($mw);
}

// process
$this->expectOutputString("MW_1_S MW_2_S MW_2_E MW_1_E ");
$this->object->process($this->createRequest('/test'), $this->createResponse());
}

/**
* Tests Queue->process()
* process with conditions
*
* @cover Phossa2\Middleware\Queue::process()
*/
public function testProcess()
public function testProcess1()
{
// add conditions
$data = $this->data;
$data[0] = [$data[0], function($request, $response) {return false;} ];

// process
$object = new Queue($data);
$this->expectOutputString("MW_2_S MW_2_E ");
$object($this->createRequest('/test'), $this->createResponse());
}

/**
* Tests Queue->next()
* Tests queue in queue
*
* @cover Phossa2\Middleware\Queue::process()
*/
public function testNext()
public function testProcess2()
{
$data = [
new Queue($this->data), // queue as middleware
function($request, $response, $next) {
echo "MW_3_S ";
$response = $next($request, $response);
echo "MW_3_E ";
return $response;
},
];

$object = new Queue($data);
$this->expectOutputString("MW_1_S MW_2_S MW_2_E MW_1_E MW_3_S MW_3_E ");
$object->process($this->createRequest('/test'), $this->createResponse());
}
}

0 comments on commit 8d49f8f

Please sign in to comment.