-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRewriter.php
More file actions
86 lines (70 loc) · 2.31 KB
/
Rewriter.php
File metadata and controls
86 lines (70 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/**
* Created by PhpStorm.
* User: jderay
* Date: 2/13/15
* Time: 11:12 AM
*/
namespace Giftcards\ModRewrite;
use Giftcards\ModRewrite\Condition\PredicateCheckerInterface;
use Giftcards\ModRewrite\Formatter\FormatterInterface;
use Giftcards\ModRewrite\Compiler\Configuration;
use Giftcards\ModRewrite\Rule\MatcherInterface;
use Symfony\Component\HttpFoundation\Request;
class Rewriter
{
protected $formatter;
protected $ruleMatcher;
protected $conditionPredicateChecker;
public function __construct(
FormatterInterface $formatter,
MatcherInterface $ruleMatcher,
PredicateCheckerInterface $conditionPredicateChecker
) {
$this->formatter = $formatter;
$this->ruleMatcher = $ruleMatcher;
$this->conditionPredicateChecker = $conditionPredicateChecker;
}
public function rewrite($pathInfo, Request $request, Configuration $engineConfiguration)
{
foreach ($engineConfiguration->getRules() as $rule) {
$matchState = new MatchState($rule, $pathInfo, $request);
if (
$this->ruleMatcher->ruleMatches($pathInfo, $matchState->getRule(), $matchState)
&& $this->passesConditions($matchState)
) {
return new Result(
$this->formatter->format(
$matchState->getRule()->getRewrite()->getPredicate(),
$matchState
),
$rule
);
}
}
return new Result($pathInfo);
}
/**
* @param MatchState $matchState
* @return bool
*/
protected function passesConditions(MatchState $matchState)
{
foreach ($matchState->getRule()->getConditions() as $condition) {
$flags = $condition->getFlags();
$passes = $this->conditionPredicateChecker->checkPredicate(
$condition->getPredicate(),
$condition->getSubject(),
$flags,
$matchState
);
if (!$passes && empty($flags['OR']) && empty($flags['ornext'])) {
return false;
}
if ($passes && (!empty($flags['OR']) || !empty($flags['ornext']))) {
return true;
}
}
return true;
}
}