1313
1414use Closure ;
1515use ReflectionMethod ;
16+ use Exception ;
1617use Buki \Router \RouterRequest ;
1718use Buki \Router \RouterCommand ;
1819use Buki \Router \RouterException ;
@@ -68,6 +69,16 @@ class Router
6869 */
6970 protected $ mainMethod = 'main ' ;
7071
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+
7182 /**
7283 * @var Closure $errorCallback Route error callback function
7384 */
@@ -93,6 +104,7 @@ function __construct(array $params = [])
93104 }
94105
95106 $ this ->setPaths ($ params );
107+ $ this ->loadCache ();
96108 }
97109
98110 /**
@@ -135,6 +147,12 @@ protected function setPaths($params)
135147 if (isset ($ params ['main_method ' ])) {
136148 $ this ->mainMethod = $ params ['main_method ' ];
137149 }
150+
151+ if (isset ($ params ['cache ' ])) {
152+ $ this ->cacheFile = $ params ['cache ' ];
153+ } else {
154+ $ this ->cacheFile = realpath (__DIR__ . '/../cache.php ' );
155+ }
138156 }
139157
140158 /**
@@ -144,13 +162,17 @@ protected function setPaths($params)
144162 * @param $method
145163 * @param $params
146164 *
147- * @return void
165+ * @return mixed
148166 * @throws
149167 */
150168 public function __call ($ method , $ params )
151169 {
170+ if ($ this ->cacheLoaded ) {
171+ return true ;
172+ }
173+
152174 if (is_null ($ params )) {
153- return ;
175+ return false ;
154176 }
155177
156178 if (! in_array (strtoupper ($ method ), explode ('| ' , RouterRequest::$ validMethods )) ) {
@@ -190,7 +212,8 @@ public function __call($method, $params)
190212 } else {
191213 $ this ->addRoute ($ route , $ method , $ callback , $ settings );
192214 }
193- return ;
215+
216+ return true ;
194217 }
195218
196219 /**
@@ -201,10 +224,14 @@ public function __call($method, $params)
201224 * @param array|string|closure $settings
202225 * @param string|closure $callback
203226 *
204- * @return void
227+ * @return bool
205228 */
206229 public function add ($ methods , $ route , $ settings , $ callback = null )
207230 {
231+ if ($ this ->cacheLoaded ) {
232+ return true ;
233+ }
234+
208235 if (is_null ($ callback )) {
209236 $ callback = $ settings ;
210237 $ settings = null ;
@@ -220,7 +247,7 @@ public function add($methods, $route, $settings, $callback = null)
220247 call_user_func_array ([$ this , strtolower ($ methods )], [$ route , $ settings , $ callback ]);
221248 }
222249
223- return ;
250+ return true ;
224251 }
225252
226253 /**
@@ -229,7 +256,7 @@ public function add($methods, $route, $settings, $callback = null)
229256 * @param string|array $pattern
230257 * @param null|string $attr
231258 *
232- * @return void
259+ * @return mixed
233260 * @throws
234261 */
235262 public function pattern ($ pattern , $ attr = null )
@@ -250,7 +277,7 @@ public function pattern($pattern, $attr = null)
250277 }
251278 }
252279
253- return ;
280+ return true ;
254281 }
255282
256283 /**
@@ -354,10 +381,14 @@ public function run()
354381 * @param closure|array $settings
355382 * @param null|closure $callback
356383 *
357- * @return void
384+ * @return bool
358385 */
359386 public function group ($ name , $ settings = null , $ callback = null )
360387 {
388+ if ($ this ->cacheLoaded ) {
389+ return true ;
390+ }
391+
361392 $ groupName = trim ($ name , '/ ' );
362393 $ group = [];
363394 $ group ['route ' ] = '/ ' . $ groupName ;
@@ -406,6 +437,8 @@ public function group($name, $settings = null, $callback = null)
406437 }
407438
408439 $ this ->endGroup ();
440+
441+ return true ;
409442 }
410443
411444 /**
@@ -415,11 +448,15 @@ public function group($name, $settings = null, $callback = null)
415448 * @param string|array $settings
416449 * @param null|string $controller
417450 *
418- * @return void
451+ * @return mixed
419452 * @throws
420453 */
421454 public function controller ($ route , $ settings , $ controller = null )
422455 {
456+ if ($ this ->cacheLoaded ) {
457+ return true ;
458+ }
459+
423460 if (is_null ($ controller )) {
424461 $ controller = $ settings ;
425462 $ settings = [];
@@ -435,7 +472,7 @@ public function controller($route, $settings, $controller = null)
435472 }
436473
437474 if (! class_exists ($ controller )) {
438- require ( $ controllerFile) ;
475+ require $ controllerFile ;
439476 }
440477
441478 $ controller = str_replace ('/ ' , '\\' , $ controller );
@@ -467,7 +504,7 @@ public function controller($route, $settings, $controller = null)
467504 unset($ r );
468505 }
469506
470- return ;
507+ return true ;
471508 }
472509
473510 /**
@@ -606,7 +643,7 @@ public function getList()
606643 echo '<pre style="font-size:15px;"> ' ;
607644 var_dump ($ this ->getRoutes ());
608645 echo '</pre> ' ;
609- die () ;
646+ die;
610647 }
611648
612649 /**
@@ -641,4 +678,42 @@ public function routerCommand()
641678 {
642679 return RouterCommand::getInstance ();
643680 }
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+ }
644719}
0 commit comments