-
Notifications
You must be signed in to change notification settings - Fork 2
sample impl. of prefilters #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hbandura
wants to merge
6
commits into
getbonder:master
Choose a base branch
from
hbandura:newFilters
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c25f1fc
sample impl. of prefilters
hbandura 3ba7080
Controller filter variables working, but still not using getFilters i…
hbandura 51e8202
Controllers filters seem to be working. Commit to check on pull reque…
hbandura 425e4ee
Added correct tests for getFilters
hbandura 0ea7b13
Fixed signature
hbandura 31b0bc6
Fixed scrutinizer issues
hbandura File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,20 +1,21 @@ | ||
| <?php | ||
|
|
||
| namespace Bonder\Filters; | ||
| use Bonder\Controller; | ||
|
|
||
| /** | ||
| * Provides the filters to be used by a given uri. | ||
| * Provides the filters to be used by a given uri and controller. | ||
| * | ||
| * @author hbandura | ||
| */ | ||
| interface FiltersProvider { | ||
|
|
||
| /** | ||
| * Returns the filters for the given uri. | ||
| * Returns the filters for the given uri (global) and the controller. | ||
| * | ||
| * @param string $uri the uri. | ||
| * @return \Bonder\Filter[] the filters. | ||
| * @return \Bonder\Filter[] the filters, as an array of alias => filter. | ||
| */ | ||
| public function getFilters($uri); | ||
| public function getFilters($uri, Controller $controller); | ||
|
|
||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,41 +1,51 @@ | ||
| <?php | ||
|
|
||
| namespace Bonder\Filters; | ||
| use Bonder\Collections\Map; | ||
| use Bonder\Controller; | ||
| use Bonder\Request; | ||
|
|
||
| /** | ||
| * A filter chain used in a request dispatch. | ||
| * | ||
| * @author hbandura | ||
| */ | ||
| final class SimpleFilterChain implements | ||
| \Bonder\Filters\FilterChain, | ||
| \Bonder\Filters\NextFilterCaller { | ||
| \Bonder\Filters\FilterChain { | ||
|
|
||
| /** | ||
| * @var \Bonder\Filter[] | ||
| */ | ||
| private $filters; | ||
|
|
||
| /** | ||
| * @var Array(alias, \Bonder\Filter) [] | ||
| */ | ||
| private $filtersList; | ||
|
|
||
| /** | ||
| * @var \Bonder\Controller | ||
| * @var Controller | ||
| */ | ||
| private $controller; | ||
|
|
||
| /** | ||
| * Creates a new FilterChain. | ||
| * | ||
| * @param \Bonder\Filter[] $filters the filters, in order. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we reflect the 'alias' => 'filter' feature in the comment? Something like:
|
||
| * @param \Bonder\Controller $controller the controller, the end of the chain. | ||
| * @param Controller $controller the controller, the end of the chain. | ||
| */ | ||
| public function __construct(Array $filters, \Bonder\Controller $controller) { | ||
| public function __construct(Array $filters, Controller $controller) { | ||
| $this->filters = $filters; | ||
| $this->controller = $controller; | ||
| foreach ($this->filters as $alias => $filter) { | ||
| $this->filtersList[] = array($alias, $filter); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns the controller. | ||
| * | ||
| * @return \Bonder\Controller the controller. | ||
| * @return Controller the controller. | ||
| */ | ||
| public function getController() { | ||
| return $this->controller; | ||
|
|
@@ -54,17 +64,62 @@ public function getFilters() { | |
| * Executes the first filter in the chain. If no filters are available, | ||
| * calls the controller. | ||
| * | ||
| * @param \Bonder\Request $request the request. | ||
| * @param Request $request the request. | ||
| * @return \Bonder\Response the response. | ||
| */ | ||
| public function call(\Bonder\Request $request) { | ||
| if (empty($this->filters)) { | ||
| public function execute(Request $request) { | ||
| return $this->executeStep($request, 0); | ||
| } | ||
|
|
||
| /** | ||
| * Recursion entry point for next filter callers. Public because PHP lacks private inner classes. | ||
| * | ||
| * @param Request $request | ||
| * @param $step | ||
| * @param Map $filterVariables | ||
| * @return mixed | ||
| */ | ||
| public function executeStep(Request $request, $step) { | ||
| if ($step < count($this->filtersList)) { | ||
| list($alias, $filter) = $this->filtersList[$step]; | ||
| return $filter->filter($request, new SimpleFilterChainNextCaller($this, $step + 1, $alias)); | ||
| } | ||
| if ($step == count($this->filtersList)) { | ||
| return $this->controller->service($request); | ||
| } | ||
| $nextFilter = reset($this->filters); | ||
| $nextFilters = array_slice($this->filters, 1); | ||
| return $nextFilter->filter($request, | ||
| new SimpleFilterChain($nextFilters, $this->controller)); | ||
| throw new \InvalidArgumentException("Step can't be higher than the filters amount + 1"); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** Utility class to simplify filter chain recursion. */ | ||
| final class SimpleFilterChainNextCaller implements NextFilterCaller { | ||
|
|
||
| /** | ||
| * @var SimpleFilterChain | ||
| */ | ||
| private $filterChain; | ||
|
|
||
| /** | ||
| * @var int | ||
| */ | ||
| private $step; | ||
|
|
||
| /** | ||
| * @var string | ||
| */ | ||
| private $alias; | ||
|
|
||
| public function __construct(SimpleFilterChain $filterChain, $step, $alias) { | ||
| $this->filterChain = $filterChain; | ||
| $this->step = $step; | ||
| $this->alias = $alias; | ||
| } | ||
|
|
||
| public function call(Request $request, Array $variables = array()) { | ||
| if (!is_numeric($this->alias)) { | ||
| $request->getFilterVariables()->set($this->alias, Map::fromArray($variables)); | ||
| } | ||
| return $this->filterChain->executeStep($request, $this->step); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Used to send the processed variables from the current filter. They could then be retrieved in the controller with something like $context->getFilterVariables(FilterClass)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FilterClass string or we'll use the alias?