-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
namespace Packaged\Routing; | ||
|
||
use Packaged\Context\Context; | ||
use function is_callable; | ||
|
||
class RouteCompleteCallback implements RouteCompleter, Condition | ||
{ | ||
protected $_callback; | ||
protected $_condition; | ||
|
||
public function __construct(callable $callback = null) | ||
{ | ||
if($callback !== null) | ||
{ | ||
$this->setCallback($callback); | ||
} | ||
} | ||
|
||
public static function i(callable $callback = null) | ||
{ | ||
return new static($callback); | ||
} | ||
|
||
public function match(Context $context): bool | ||
{ | ||
if($this->_condition instanceof Condition) | ||
{ | ||
return $this->_condition->match($context); | ||
} | ||
return true; | ||
} | ||
|
||
public function setCondition(Condition $condition) | ||
{ | ||
$this->_condition = $condition; | ||
return $this; | ||
} | ||
|
||
public function setCallback(callable $callback) | ||
{ | ||
$this->_callback = $callback; | ||
return $this; | ||
} | ||
|
||
public function complete(Context $context) | ||
{ | ||
$func = $this->_callback; | ||
if(is_callable($func)) | ||
{ | ||
return $func($context); | ||
} | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace Packaged\Tests\Routing; | ||
|
||
use Exception; | ||
use Packaged\Context\Context; | ||
use Packaged\Http\Request; | ||
use Packaged\Http\Response; | ||
use Packaged\Routing\Handler\FuncHandler; | ||
use Packaged\Routing\RequestCondition; | ||
use Packaged\Routing\Route; | ||
use Packaged\Routing\RouteCompleteCallback; | ||
use Packaged\Tests\Routing\Supporting\TestSingleRouteSelector; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RouteCompleteCallbackTest extends TestCase | ||
{ | ||
public function testComplete() | ||
{ | ||
$handler = new FuncHandler( | ||
function () { | ||
return Response::create('OK'); | ||
} | ||
); | ||
|
||
$request = Request::create('http://www.test.com:8080/'); | ||
$ctx = new Context($request); | ||
|
||
(new TestSingleRouteSelector( | ||
Route::with( | ||
RouteCompleteCallback::i( | ||
function (Context $ctx) { | ||
$ctx->meta()->set('callback', 1); | ||
} | ||
) | ||
)->setHandler($handler) | ||
))->handle($ctx); | ||
|
||
$this->assertEquals(1, $ctx->meta()->get('callback')); | ||
|
||
try | ||
{ | ||
(new TestSingleRouteSelector( | ||
Route::with( | ||
RouteCompleteCallback::i( | ||
function (Context $ctx) { | ||
$ctx->meta()->set('callback', 2); | ||
} | ||
)->setCondition(RequestCondition::i()->domain('invalid')) | ||
)->setHandler($handler) | ||
))->handle($ctx); | ||
} | ||
catch(Exception $e) | ||
{ | ||
} | ||
$this->assertEquals(1, $ctx->meta()->get('callback')); | ||
|
||
(new TestSingleRouteSelector( | ||
Route::with( | ||
RouteCompleteCallback::i( | ||
function (Context $ctx) { | ||
$ctx->meta()->set('callback', 3); | ||
} | ||
)->setCondition(RequestCondition::i()->domain('test')) | ||
)->setHandler($handler) | ||
))->handle($ctx); | ||
|
||
$this->assertEquals(3, $ctx->meta()->get('callback')); | ||
|
||
(new TestSingleRouteSelector(Route::with(RouteCompleteCallback::i())->setHandler($handler)))->handle($ctx); | ||
$this->assertEquals(3, $ctx->meta()->get('callback')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters