1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2014 Krzysztof Magosa |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
9
|
|
|
* |
10
|
|
|
* Unless required by applicable law or agreed to in writing, software |
11
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
12
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13
|
|
|
* See the License for the specific language governing permissions and |
14
|
|
|
* limitations under the License. |
15
|
|
|
*/ |
16
|
|
|
namespace KM\Saffron; |
17
|
|
|
|
18
|
|
|
class RouterFactory |
19
|
|
|
{ |
20
|
|
|
protected $collection; |
21
|
|
|
protected $initClosure; |
22
|
|
|
protected $cacheDir; |
23
|
|
|
protected $classSuffix; |
24
|
|
|
protected $debug = false; |
25
|
|
|
|
26
|
|
|
public function __construct(callable $initClosure) |
27
|
|
|
{ |
28
|
|
|
$this->initClosure = $initClosure; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $dir |
33
|
|
|
* @return RouterFactory |
34
|
|
|
*/ |
35
|
|
|
public function setCacheDir($dir) |
36
|
|
|
{ |
37
|
|
|
$this->cacheDir = $dir; |
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function getCacheDir() |
45
|
|
|
{ |
46
|
|
|
if (!$this->cacheDir) { |
47
|
|
|
$this->cacheDir = sys_get_temp_dir(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this->cacheDir; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $suffix |
55
|
|
|
* @return RouterFactory |
56
|
|
|
*/ |
57
|
|
|
public function setClassSuffix($suffix) |
58
|
|
|
{ |
59
|
|
|
$this->classSuffix = $suffix; |
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param bool $debug |
65
|
|
|
* @return RouterFactory |
66
|
|
|
*/ |
67
|
|
|
public function setDebug($debug) |
68
|
|
|
{ |
69
|
|
|
$this->debug = $debug; |
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function getClassSuffix() |
77
|
|
|
{ |
78
|
|
|
static $iteration = 0; |
79
|
|
|
|
80
|
|
|
if ($this->debug) { |
81
|
|
|
return $this->classSuffix.md5(microtime(true).(++$iteration)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->classSuffix; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $className |
89
|
|
|
* @param \Closure $generator |
90
|
|
|
* @return object |
91
|
|
|
*/ |
92
|
|
|
protected function createInstance($className, \Closure $generator) |
93
|
|
|
{ |
94
|
|
|
$cacheFile = $this->getCacheDir().'/'.$className.'.php'; |
95
|
|
|
|
96
|
|
|
if (!is_readable($cacheFile) || $this->debug) { |
97
|
|
|
file_put_contents( |
98
|
|
|
$cacheFile, |
99
|
|
|
$generator($className), |
100
|
|
|
LOCK_EX |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
require_once $cacheFile; |
105
|
|
|
|
106
|
|
|
if ($this->debug) { |
107
|
|
|
unlink($cacheFile); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return new $className; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return UrlMatcher\Base |
115
|
|
|
*/ |
116
|
|
|
public function getUrlMatcher() |
117
|
|
|
{ |
118
|
|
|
return $this->createInstance( |
119
|
|
|
'KM_Saffron_UrlMatcher_'.$this->getClassSuffix(), |
120
|
|
|
function ($className) { |
121
|
|
|
$generator = new UrlMatcher\Generator($this->getCollection()); |
122
|
|
|
return $generator->generate($className); |
123
|
|
|
} |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return UrlBuilder\Base |
129
|
|
|
*/ |
130
|
|
|
public function getUrlBuilder() |
131
|
|
|
{ |
132
|
|
|
return $this->createInstance( |
133
|
|
|
'KM_Saffron_UrlBuilder_'.$this->getClassSuffix(), |
134
|
|
|
function ($className) { |
135
|
|
|
$generator = new UrlBuilder\Generator($this->getCollection()); |
136
|
|
|
return $generator->generate($className); |
137
|
|
|
} |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return RoutesCollection |
143
|
|
|
*/ |
144
|
|
|
protected function getCollection() |
145
|
|
|
{ |
146
|
|
|
if (null === $this->collection) { |
147
|
|
|
$this->collection = new RoutesCollection(); |
148
|
|
|
call_user_func($this->initClosure, $this->collection); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this->collection; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return Router |
156
|
|
|
*/ |
157
|
|
|
public function build() |
158
|
|
|
{ |
159
|
|
|
return new Router($this); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|