-
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.
- Created Link object to represent a chain of middleware - Added Context to allow clean up how you can add extra state to a middleware. - Updated the middleware api to accept a Mw\Link as the last arg instead of a closure. Signed-off-by: RJ Garcia <rj@bighead.net>
- Loading branch information
Showing
10 changed files
with
203 additions
and
25 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
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 |
---|---|---|
|
@@ -38,7 +38,6 @@ Or build them: | |
make doc | ||
``` | ||
|
||
|
||
## Tests and Examples | ||
|
||
Run tests via: | ||
|
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
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,9 @@ | ||
<?php | ||
|
||
namespace Krak\Mw; | ||
|
||
/** returns the Invoke Instance */ | ||
interface Context | ||
{ | ||
public function getInvoke(); | ||
} |
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,37 @@ | ||
<?php | ||
|
||
namespace Krak\Mw\Context; | ||
|
||
use Pimple\Container, | ||
Krak\Mw; | ||
|
||
class PimpleContext implements \ArrayAccess, Mw\Context | ||
{ | ||
private $container; | ||
private $invoke; | ||
|
||
public function __construct(Container $container, $invoke = null) { | ||
$this->container = $container; | ||
$this->invoke = $invoke ?: Mw\pimpleAwareInvoke($container); | ||
} | ||
|
||
public function getInvoke() { | ||
return $this->invoke; | ||
} | ||
|
||
public function offsetSet($offset, $value) { | ||
return $this->container->offsetSet($offset, $value); | ||
} | ||
|
||
public function offsetGet($offset) { | ||
return $this->container->offsetGet($offset); | ||
} | ||
|
||
public function offsetExists($offset) { | ||
return $this->container->offsetExists($offset); | ||
} | ||
|
||
public function offsetUnset($offset) { | ||
return $this->container->offsetUnset($offset); | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
|
||
namespace Krak\Mw\Context; | ||
|
||
use Krak\Mw\Context; | ||
|
||
class StdContext implements Context | ||
{ | ||
private $invoke; | ||
|
||
public function __construct($invoke = 'call_user_func') { | ||
$this->invoke = $invoke; | ||
} | ||
|
||
public function getInvoke() { | ||
return $this->invoke; | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
namespace Krak\Mw; | ||
|
||
/** Represents a link in the middleware chain. A link instance is passed to every middleware | ||
as the last parameter which allows the next middleware to be called */ | ||
class Link | ||
{ | ||
private $mw; | ||
private $next; | ||
private $ctx; | ||
|
||
public function __construct($mw, Context $ctx, Link $next = null) { | ||
$this->mw = $mw; | ||
$this->ctx = $ctx; | ||
$this->next = $next; | ||
} | ||
|
||
public function __invoke(...$params) { | ||
$mw = $this->mw; | ||
$invoke = $this->ctx->getInvoke(); | ||
$params[] = $this->next; | ||
return $invoke($mw, ...$params); | ||
|
||
return $this->invoke($this->mw, ...$params); | ||
} | ||
|
||
/** Chains a middleware to the current link */ | ||
public function chain($mw) { | ||
return new static($mw, $this->ctx, $this); | ||
} | ||
|
||
public function getContext() { | ||
return $this->ctx; | ||
} | ||
} |
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
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
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