-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRewriterBuilder.php
More file actions
143 lines (122 loc) · 3.6 KB
/
RewriterBuilder.php
File metadata and controls
143 lines (122 loc) · 3.6 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* Created by PhpStorm.
* User: jderay
* Date: 2/25/15
* Time: 3:03 PM
*/
namespace Giftcards\ModRewrite;
use Giftcards\ModRewrite\Condition\ChainPredicateChecker;
use Giftcards\ModRewrite\Condition\ExistencePredicateChecker;
use Giftcards\ModRewrite\Condition\FormattingPredicateChecker;
use Giftcards\ModRewrite\Condition\NotPredicateChecker;
use Giftcards\ModRewrite\Condition\PredicateCheckerInterface;
use Giftcards\ModRewrite\Condition\RegexPredicateChecker;
use Giftcards\ModRewrite\Formatter\ApacheServerVariableFormatter;
use Giftcards\ModRewrite\Formatter\ChainFormatter;
use Giftcards\ModRewrite\Formatter\FormatterInterface;
use Giftcards\ModRewrite\Formatter\LastPassingConditionFormatter;
use Giftcards\ModRewrite\Formatter\PathInfoFormatter;
use Giftcards\ModRewrite\Formatter\RewriteFormatter;
use Giftcards\ModRewrite\Rule\MatcherInterface;
use Giftcards\ModRewrite\Rule\RegexMatcher;
class RewriterBuilder
{
protected $formatter;
protected $ruleMatcher;
protected $conditionPredicateChecker;
protected $workingDir = '';
public static function create()
{
return new static();
}
public function build()
{
return new Rewriter(
$this->getFormatter(),
$this->getRuleMatcher(),
$this->getConditionPredicateChecker()
);
}
public function getFormatter()
{
if (!$this->formatter) {
$this->formatter = $this->getDefaultFormatter();
}
return $this->formatter;
}
/**
* @param mixed $formatter
* @return $this
*/
public function setFormatter(FormatterInterface $formatter)
{
$this->formatter = $formatter;
return $this;
}
/**
* @return mixed
*/
public function getRuleMatcher()
{
if (!$this->ruleMatcher) {
$this->ruleMatcher = $this->getDefaultRuleMatcher();
}
return $this->ruleMatcher;
}
/**
* @param mixed $ruleMatcher
* @return $this
*/
public function setRuleMatcher(MatcherInterface $ruleMatcher)
{
$this->ruleMatcher = $ruleMatcher;
return $this;
}
/**
* @return mixed
*/
public function getConditionPredicateChecker()
{
if (!$this->conditionPredicateChecker) {
$this->conditionPredicateChecker = $this->getDefaultConditionPredicateChecker();
}
return $this->conditionPredicateChecker;
}
/**
* @param mixed $conditionPredicateChecker
* @return $this
*/
public function setConditionPredicateChecker(PredicateCheckerInterface $conditionPredicateChecker)
{
$this->conditionPredicateChecker = $conditionPredicateChecker;
return $this;
}
protected function getDefaultFormatter()
{
$chain = new ChainFormatter();
$chain
->add(new ApacheServerVariableFormatter())
->add(new LastPassingConditionFormatter())
->add(new RewriteFormatter())
->add(new PathInfoFormatter())
;
return $chain;
}
protected function getDefaultRuleMatcher()
{
return new RegexMatcher();
}
protected function getDefaultConditionPredicateChecker()
{
$chain = new ChainPredicateChecker();
$chain
->add(new ExistencePredicateChecker($this->workingDir))
->setDefault(new RegexPredicateChecker())
;
return new FormattingPredicateChecker(
$this->getFormatter(),
new NotPredicateChecker($chain)
);
}
}