-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirector.php
executable file
·52 lines (51 loc) · 1.65 KB
/
Director.php
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
<?
namespace stradivari\core {
abstract class Director {
public static $lowerCase = true;
public static $regexpFlags = '';
public static function executeRulesFile($rulesFile, $execute, $redirectType = null) {
$converter = new \stradivari\data_converter\FileConverter($rulesFile);
$converter->array();
static::executeRules($converter->data, $execute, $redirectType);
}
public static function executeRules($rules, $execute, $redirectType = null) {
if ( !$rules ) {
return;
}
$url = App::$pool['input']['url'];
if ( static::$lowerCase ) {
foreach ( $url as &$part ) {
$part = strtolower($part);
}
unset($part);
}
foreach ( $rules as $regexp => $rule ) {
static::executeRule($regexp, $rule, $url, $execute, $redirectType);
}
}
protected static function executeRule($regexp, $rule, $url, $execute, $redirectType = null) {
foreach (array('regexp', 'rule') as $param) {
if ( is_string($$param) ) {
$$param = static::prepareExpression($$param, $url);
}
}
$regexp = '/' . str_replace('/', '\/', $regexp) . '/';
$regexp .= static::$regexpFlags;
if ( @preg_match($regexp, $url[$execute], $matches) ) {
if ( is_string($rule) ) {
$rule = static::prepareExpression($rule, $matches);
static::execute($rule, $redirectType);
} else {
static::executeRules($rule, $execute, $redirectType);
}
}
}
protected static function prepareExpression($expression, $rules) {
foreach ( $rules as $key => $part ) {
$expression = str_replace("##{$key}##", $part, $expression);
}
return $expression;
}
protected static function execute($rule, $redirectType) {}
}
}