1515use webfiori \cli \Runner ;
1616use webfiori \file \exceptions \FileException ;
1717use webfiori \file \File ;
18+ use webfiori \framework \cache \Cache ;
1819use webfiori \framework \exceptions \RoutingException ;
1920use webfiori \framework \ui \HTTPCodeView ;
2021use webfiori \framework \ui \StarterPage ;
@@ -507,14 +508,16 @@ public static function incSiteMapRoute() {
507508 Response::addHeader ('content-type ' ,'text/xml ' );
508509 };
509510 self ::closure ([
510- 'path ' => '/sitemap.xml ' ,
511- 'route-to ' => $ sitemapFunc ,
512- 'in-sitemap ' => true
511+ RouteOption::PATH => '/sitemap.xml ' ,
512+ RouteOption::TO => $ sitemapFunc ,
513+ RouteOption::SITEMAP => true ,
514+ RouteOption::CACHE_DURATION => 86400 //1 day
513515 ]);
514516 self ::closure ([
515- 'path ' => '/sitemap ' ,
516- 'route-to ' => $ sitemapFunc ,
517- 'in-sitemap ' => true
517+ RouteOption::PATH => '/sitemap ' ,
518+ RouteOption::TO => $ sitemapFunc ,
519+ RouteOption::SITEMAP => true ,
520+ RouteOption::CACHE_DURATION => 86400 //1 day
518521 ]);
519522 }
520523 /**
@@ -529,7 +532,8 @@ public static function notFound() {
529532 * Adds new route to a web page.
530533 *
531534 * Note that the route which created using this method will be added to
532- * 'global' and 'web' middleware groups.
535+ * 'global' and 'web' middleware groups. Additionally, the routes will
536+ * be cached for one hour.
533537 *
534538 * @param array $options An associative array that contains route
535539 * options. Available options are:
@@ -755,13 +759,14 @@ private function addRouteHelper0($options): bool {
755759 $ asApi = $ options [RouteOption::API ];
756760 $ closureParams = $ options [RouteOption::CLOSURE_PARAMS ] ;
757761 $ path = $ options [RouteOption::PATH ];
762+ $ cache = $ options [RouteOption::CACHE_DURATION ];
758763
759764 if ($ routeType == self ::CLOSURE_ROUTE && !is_callable ($ routeTo )) {
760765 return false ;
761766 }
762767 $ routeUri = new RouterUri ($ this ->getBase ().$ path , $ routeTo ,$ caseSensitive , $ closureParams );
763768 $ routeUri ->setAction ($ options [RouteOption::ACTION ]);
764-
769+ $ routeUri -> setCacheDuration ( $ cache );
765770 if (!$ this ->hasRouteHelper ($ routeUri )) {
766771 if ($ asApi === true ) {
767772 $ routeUri ->setType (self ::API_ROUTE );
@@ -928,6 +933,12 @@ private function checkOptionsArr(array $options): array {
928933 } else {
929934 $ caseSensitive = true ;
930935 }
936+
937+ if (isset ($ options [RouteOption::CACHE_DURATION ])) {
938+ $ cacheDuration = $ options [RouteOption::CACHE_DURATION ];
939+ } else {
940+ $ cacheDuration = 0 ;
941+ }
931942
932943 $ routeType = $ options [RouteOption::TYPE ] ?? Router::CUSTOMIZED ;
933944
@@ -978,7 +989,8 @@ private function checkOptionsArr(array $options): array {
978989 RouteOption::VALUES => $ varValues ,
979990 RouteOption::MIDDLEWARE => $ mdArr ,
980991 RouteOption::REQUEST_METHODS => $ this ->getRequestMethodsHelper ($ options ),
981- RouteOption::ACTION => $ action
992+ RouteOption::ACTION => $ action ,
993+ RouteOption::CACHE_DURATION => $ cacheDuration
982994 ];
983995 }
984996 private function copyOptionsToSub ($ options , &$ subRoute ) {
@@ -1376,7 +1388,6 @@ private function routeFound(RouterUri $route, bool $loadResource) {
13761388 if ($ route ->getType () == self ::API_ROUTE && !defined ('API_CALL ' )) {
13771389 define ('API_CALL ' , true );
13781390 }
1379-
13801391 if (is_callable ($ route ->getRouteTo ())) {
13811392 if ($ loadResource === true ) {
13821393 call_user_func_array ($ route ->getRouteTo (),$ route ->getClosureParams ());
@@ -1453,6 +1464,16 @@ private function routeFound(RouterUri $route, bool $loadResource) {
14531464 * @throws RoutingException
14541465 */
14551466 private function searchRoute (RouterUri $ routeUri , string $ uri , bool $ loadResource , bool $ withVars = false ): bool {
1467+ $ data = Cache::get ($ uri );
1468+
1469+ if ($ data !== null ) {
1470+ Response::write ($ data ['body ' ]);
1471+ Response::setCode ($ data ['http-code ' ]);
1472+ foreach ($ data ['headers ' ] as $ headerObj ) {
1473+ Response::addHeader ($ headerObj ->getName (), $ headerObj ->getValue ());
1474+ }
1475+ return true ;
1476+ }
14561477 $ pathArray = $ routeUri ->getPathArray ();
14571478 $ requestMethod = Request::getMethod ();
14581479 $ indexToSearch = 'static ' ;
@@ -1600,7 +1621,10 @@ private static function view(array $options): bool {
16001621 if (gettype ($ options ) == 'array ' ) {
16011622 $ options [RouteOption::TYPE ] = Router::VIEW_ROUTE ;
16021623 self ::addToMiddlewareGroup ($ options , 'web ' );
1603-
1624+ if (!isset ($ options [RouteOption::CACHE_DURATION ])) {
1625+ //Cache pages for 1 hour by default
1626+ $ options [RouteOption::CACHE_DURATION ] = 3600 ;
1627+ }
16041628 return Router::getInstance ()->addRouteHelper1 ($ options );
16051629 }
16061630
0 commit comments