|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PeterColes\Themes; |
|
4
|
|
|
|
|
5
|
|
|
class Themes |
|
6
|
|
|
{ |
|
7
|
|
|
protected $theme = null; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* get the name of the theme for this context. |
|
11
|
|
|
* |
|
12
|
|
|
* @param \Illuminate\Http\Request $request |
|
13
|
|
|
* @return boolean |
|
14
|
|
|
*/ |
|
15
|
|
|
public function setTheme($request) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->findMatch($request); |
|
18
|
|
|
$this->addThemePath(); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Getter for the theme that is chosen. |
|
23
|
|
|
* |
|
24
|
|
|
* @return string theme |
|
25
|
|
|
*/ |
|
26
|
|
|
public function getTheme() |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->theme; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Iterate over the match criteria until a successful match is achieved. |
|
33
|
|
|
* |
|
34
|
|
|
* @return void |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function findMatch($request) |
|
37
|
|
|
{ |
|
38
|
|
|
foreach (app('config')->get('themes') as $match) |
|
39
|
|
|
{ |
|
40
|
|
|
if ($this->testMatch($request, $match)) { |
|
41
|
|
|
break; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* If there's no match critera or all rules are matched, set the theme and exit. |
|
48
|
|
|
* |
|
49
|
|
|
* @return void|boolean |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function testMatch($request, $match) |
|
52
|
|
|
{ |
|
53
|
|
|
if (isset($match['match'])) { |
|
54
|
|
|
$rules = $this->explodeRules($match['match']); |
|
55
|
|
|
|
|
56
|
|
|
foreach ($rules as $rule) { |
|
57
|
|
|
if (!$this->handleRule($request, $rule)) { |
|
58
|
|
|
return false; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// nothing to match or all matches succeeded |
|
64
|
|
|
$this->theme = $match['theme']; |
|
65
|
|
|
return true; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Remove whtespace from rules and separate the rules in a match group. |
|
70
|
|
|
* |
|
71
|
|
|
* @return array rules |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function explodeRules($rules) |
|
74
|
|
|
{ |
|
75
|
|
|
$rules = preg_replace('/\s+/', '', $rules); |
|
76
|
|
|
return explode('|', $rules); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Separate rule name from parameters and pass to matcher class to evaluate. |
|
81
|
|
|
* |
|
82
|
|
|
* @return boolean |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function handleRule($request, $rule) |
|
85
|
|
|
{ |
|
86
|
|
|
$rule = explode(':', $rule); |
|
87
|
|
|
$params = isset($rule[1]) ? $rule[1] : null; |
|
88
|
|
|
$class = '\\PeterColes\\Themes\\Matchers\\'.studly_case($rule[0]); |
|
89
|
|
|
|
|
90
|
|
|
return (new $class)->handle($request, $params); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Add path for current theme's views to the list that Laravel searches. |
|
95
|
|
|
* |
|
96
|
|
|
* @return void |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function addThemePath() |
|
99
|
|
|
{ |
|
100
|
|
|
if ($this->theme) { |
|
101
|
|
|
app('view.finder')->addLocation(themes_path($this->theme.'/views')); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|