Skip to content

Commit

Permalink
Merge pull request #1 from php-api-clients/feature-priority
Browse files Browse the repository at this point in the history
Priority
  • Loading branch information
WyriHaximus committed Nov 11, 2016
2 parents 2e727bb + 2d5091f commit 3ac9c02
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/DefaultPriorityTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare(strict_types=1);

namespace ApiClients\Foundation\Middleware;

trait DefaultPriorityTrait
{
/**
* @return int
*/
public function priority(): int
{
return 500;
}
}
8 changes: 8 additions & 0 deletions src/MiddlewareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
*/
interface MiddlewareInterface
{
/**
* Priority ranging from 0 to 1000. Where 1000 will be executed first on `pre` and 0 last on `pre`.
* For `post` the order is reversed.
*
* @return int
*/
public function priority(): int;

/**
* Return the processed $request via a fulfilled promise.
* When implementing cache or other feature that returns a response, do it with a rejected promise.
Expand Down
2 changes: 2 additions & 0 deletions tests/DummyMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

namespace ApiClients\Tests\Foundation\Middleware;

use ApiClients\Foundation\Middleware\DefaultPriorityTrait;
use ApiClients\Foundation\Middleware\MiddlewareInterface;
use ApiClients\Foundation\Middleware\PostTrait;
use ApiClients\Foundation\Middleware\PreTrait;

class DummyMiddleware implements MiddlewareInterface
{
use DefaultPriorityTrait;
use PreTrait;
use PostTrait;
}
9 changes: 9 additions & 0 deletions tests/DummyMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@

class DummyMiddlewareTest extends TestCase
{
public function testPriority()
{
$middleware = new DummyMiddleware();
$this->assertSame(
500,
$middleware->priority()
);
}

public function testPre()
{
$middleware = new DummyMiddleware();
Expand Down

0 comments on commit 3ac9c02

Please sign in to comment.