Skip to content

Commit

Permalink
Add withContext and withLinkClass to MwStack
Browse files Browse the repository at this point in the history
Signed-off-by: RJ Garcia <rj@bighead.net>
  • Loading branch information
ragboyjr committed Jan 19, 2017
1 parent b42321c commit 8a47548
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

## Added

- Added `withContext` and `withLinkClass` options to MwStack.

## [0.4.0] - 2017-01-04
### Changed

Expand Down
8 changes: 7 additions & 1 deletion doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ array splitArgs(array $args)
return $next(...$args);
};
}
class MwStack implements Countable
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -232,6 +232,12 @@ Closure compose(callable $last = null)
Composes the stack into a handler.
Generator getEntries()
Yields the raw stack entries in the order they were added.
MwStack withContext(Context $ctx)
Creates a clone of the current stack with an updated context
MwStack withLinkClass($class)
Creates a clone of the current stack with an updated link class
MwStack withEntries($entries)
Creates a clone of the current stack with updated entries.
MwStack static createFromEntries($name, $entries)
Creates a stack with a set of entries. ``mw\stack`` internally calls this.

Expand Down
10 changes: 10 additions & 0 deletions src/MwStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ public function compose($last = null) {
return compose($this->normalize(), $this->ctx, $last, $this->link_class);
}

public function withContext(Context $ctx) {
$stack = clone $this;
$stack->ctx = $ctx;
return $stack;
}
public function withLinkClass($link_class) {
$stack = clone $this;
$stack->link_class = $link_class;
return $stack;
}
public function withEntries($entries) {
return static::createFromEntries($this->name, $entries, $this->ctx, $this->link_class);
}
Expand Down
17 changes: 17 additions & 0 deletions test/mw.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public function handle($s) {
}
}

class MyLink extends Mw\Link {}

describe('Mw', function() {
describe('#compose', function() {
it('composes a set of middleware into a handler', function() {
Expand Down Expand Up @@ -180,6 +182,21 @@ function() { return 1; },
$handler = $stack->compose();
assert($handler(2) === 1);
});
it('allows you to change context', function() {
$stack = mw\stack('stack')->withContext(new Mw\Context\StdContext(function() { return 1; }));
$stack->push(id());
$handler = $stack->compose();
assert($handler(2) === 1);
});
it('allows you to change link class', function() {
$stack = mw\stack('stack')->withLinkClass(MyLink::class);
$stack->push(function($id, $next) {
assert($next instanceof MyLink);
return;
});
$handler = $stack->compose();
$handler(1);
});
});
describe('#stackMerge', function() {
it('merges stacks together into a new stack', function() {
Expand Down

0 comments on commit 8a47548

Please sign in to comment.