13
13
14
14
use Closure ;
15
15
use ReflectionMethod ;
16
+ use Exception ;
16
17
use Buki \Router \RouterRequest ;
17
18
use Buki \Router \RouterCommand ;
18
19
use Buki \Router \RouterException ;
@@ -68,6 +69,16 @@ class Router
68
69
*/
69
70
protected $ mainMethod = 'main ' ;
70
71
72
+ /**
73
+ * @var string $mainMethod Cache file
74
+ */
75
+ protected $ cacheFile = null ;
76
+
77
+ /**
78
+ * @var bool $cacheLoaded Cache is loaded?
79
+ */
80
+ protected $ cacheLoaded = false ;
81
+
71
82
/**
72
83
* @var Closure $errorCallback Route error callback function
73
84
*/
@@ -93,6 +104,7 @@ function __construct(array $params = [])
93
104
}
94
105
95
106
$ this ->setPaths ($ params );
107
+ $ this ->loadCache ();
96
108
}
97
109
98
110
/**
@@ -135,6 +147,12 @@ protected function setPaths($params)
135
147
if (isset ($ params ['main_method ' ])) {
136
148
$ this ->mainMethod = $ params ['main_method ' ];
137
149
}
150
+
151
+ if (isset ($ params ['cache ' ])) {
152
+ $ this ->cacheFile = $ params ['cache ' ];
153
+ } else {
154
+ $ this ->cacheFile = realpath (__DIR__ . '/../cache.php ' );
155
+ }
138
156
}
139
157
140
158
/**
@@ -144,13 +162,17 @@ protected function setPaths($params)
144
162
* @param $method
145
163
* @param $params
146
164
*
147
- * @return void
165
+ * @return mixed
148
166
* @throws
149
167
*/
150
168
public function __call ($ method , $ params )
151
169
{
170
+ if ($ this ->cacheLoaded ) {
171
+ return true ;
172
+ }
173
+
152
174
if (is_null ($ params )) {
153
- return ;
175
+ return false ;
154
176
}
155
177
156
178
if (! in_array (strtoupper ($ method ), explode ('| ' , RouterRequest::$ validMethods )) ) {
@@ -190,7 +212,8 @@ public function __call($method, $params)
190
212
} else {
191
213
$ this ->addRoute ($ route , $ method , $ callback , $ settings );
192
214
}
193
- return ;
215
+
216
+ return true ;
194
217
}
195
218
196
219
/**
@@ -201,10 +224,14 @@ public function __call($method, $params)
201
224
* @param array|string|closure $settings
202
225
* @param string|closure $callback
203
226
*
204
- * @return void
227
+ * @return bool
205
228
*/
206
229
public function add ($ methods , $ route , $ settings , $ callback = null )
207
230
{
231
+ if ($ this ->cacheLoaded ) {
232
+ return true ;
233
+ }
234
+
208
235
if (is_null ($ callback )) {
209
236
$ callback = $ settings ;
210
237
$ settings = null ;
@@ -220,7 +247,7 @@ public function add($methods, $route, $settings, $callback = null)
220
247
call_user_func_array ([$ this , strtolower ($ methods )], [$ route , $ settings , $ callback ]);
221
248
}
222
249
223
- return ;
250
+ return true ;
224
251
}
225
252
226
253
/**
@@ -229,7 +256,7 @@ public function add($methods, $route, $settings, $callback = null)
229
256
* @param string|array $pattern
230
257
* @param null|string $attr
231
258
*
232
- * @return void
259
+ * @return mixed
233
260
* @throws
234
261
*/
235
262
public function pattern ($ pattern , $ attr = null )
@@ -250,7 +277,7 @@ public function pattern($pattern, $attr = null)
250
277
}
251
278
}
252
279
253
- return ;
280
+ return true ;
254
281
}
255
282
256
283
/**
@@ -354,10 +381,14 @@ public function run()
354
381
* @param closure|array $settings
355
382
* @param null|closure $callback
356
383
*
357
- * @return void
384
+ * @return bool
358
385
*/
359
386
public function group ($ name , $ settings = null , $ callback = null )
360
387
{
388
+ if ($ this ->cacheLoaded ) {
389
+ return true ;
390
+ }
391
+
361
392
$ groupName = trim ($ name , '/ ' );
362
393
$ group = [];
363
394
$ group ['route ' ] = '/ ' . $ groupName ;
@@ -406,6 +437,8 @@ public function group($name, $settings = null, $callback = null)
406
437
}
407
438
408
439
$ this ->endGroup ();
440
+
441
+ return true ;
409
442
}
410
443
411
444
/**
@@ -415,11 +448,15 @@ public function group($name, $settings = null, $callback = null)
415
448
* @param string|array $settings
416
449
* @param null|string $controller
417
450
*
418
- * @return void
451
+ * @return mixed
419
452
* @throws
420
453
*/
421
454
public function controller ($ route , $ settings , $ controller = null )
422
455
{
456
+ if ($ this ->cacheLoaded ) {
457
+ return true ;
458
+ }
459
+
423
460
if (is_null ($ controller )) {
424
461
$ controller = $ settings ;
425
462
$ settings = [];
@@ -435,7 +472,7 @@ public function controller($route, $settings, $controller = null)
435
472
}
436
473
437
474
if (! class_exists ($ controller )) {
438
- require ( $ controllerFile) ;
475
+ require $ controllerFile ;
439
476
}
440
477
441
478
$ controller = str_replace ('/ ' , '\\' , $ controller );
@@ -467,7 +504,7 @@ public function controller($route, $settings, $controller = null)
467
504
unset($ r );
468
505
}
469
506
470
- return ;
507
+ return true ;
471
508
}
472
509
473
510
/**
@@ -606,7 +643,7 @@ public function getList()
606
643
echo '<pre style="font-size:15px;"> ' ;
607
644
var_dump ($ this ->getRoutes ());
608
645
echo '</pre> ' ;
609
- die () ;
646
+ die;
610
647
}
611
648
612
649
/**
@@ -641,4 +678,42 @@ public function routerCommand()
641
678
{
642
679
return RouterCommand::getInstance ();
643
680
}
681
+
682
+ /**
683
+ * Cache all routes
684
+ *
685
+ * @return bool
686
+ * @throws Exception
687
+ */
688
+ public function cache ()
689
+ {
690
+ foreach ($ this ->getRoutes () as $ key => $ r ) {
691
+ if (!is_string ($ r ['callback ' ])) {
692
+ throw new Exception (sprintf ('Routes cannot contain a Closure/Function callback while caching. ' ));
693
+ }
694
+ }
695
+
696
+ $ cacheContent = '<?php return ' .var_export ($ this ->getRoutes (), true ).'; ' . PHP_EOL ;
697
+ if (false === file_put_contents ($ this ->cacheFile , $ cacheContent )) {
698
+ throw new Exception (sprintf ('Routes cache file could not be written. ' ));
699
+ }
700
+
701
+ return true ;
702
+ }
703
+
704
+ /**
705
+ * Load Cache file
706
+ *
707
+ * @return bool
708
+ */
709
+ protected function loadCache ()
710
+ {
711
+ if (file_exists ($ this ->cacheFile )) {
712
+ $ this ->routes = require $ this ->cacheFile ;
713
+ $ this ->cacheLoaded = true ;
714
+ return true ;
715
+ }
716
+
717
+ return false ;
718
+ }
644
719
}
0 commit comments