diff --git a/docs/Route.markdown b/docs/Route.markdown index a04ab25..853aa01 100644 --- a/docs/Route.markdown +++ b/docs/Route.markdown @@ -119,6 +119,17 @@ To load your ini file you'll need to set the path to your config file and call t ---------------------------------------- +### Lazy Loading routes from an external file + +When projects grow, loading all the routes and associate code becames to expensive, to avoid this, you can create a lazyload when a route is detected. + + getRoute()->addLazyRoute("/path", "path/to/includefile"); + +This will provoke when a route to a /path is detected it will load the file pointed by includefile. +There you can load more routes. + +---------------------------------------- + ### Server side redirects You can perform server side redirects using this module. The `redirect()` method takes between 1 and 3 parameters. diff --git a/src/EpiRoute.php b/src/EpiRoute.php index 3051d96..ef604ec 100644 --- a/src/EpiRoute.php +++ b/src/EpiRoute.php @@ -18,8 +18,11 @@ class EpiRoute { private static $instance; private $routes = array(); + private $lazyRoute = array(); + private $lazyRegexes = array(); private $regexes= array(); private $route = null; + const routeKey= '__route__'; const httpGet = 'GET'; const httpPost= 'POST'; @@ -94,6 +97,7 @@ public function delete($route, $callback, $isApi = false) */ public function load($file) { + print "si si la estsa cargando...
\n"; $file = Epi::getPath('config') . "/{$file}"; if(!file_exists($file)) { @@ -167,6 +171,15 @@ public function getRoute($route = false, $httpMethod = null) if($httpMethod === null) $httpMethod = $_SERVER['REQUEST_METHOD']; + + foreach($this->lazyRegexes as $ind => $regex) + { + if(preg_match($regex, $this->route)) + { + require_once $this->lazyRoute[$ind]['include']; + } + } + foreach($this->regexes as $ind => $regex) { @@ -254,6 +267,23 @@ private function addRoute($route, $callback, $method, $postprocess = false) if(Epi::getSetting('debug')) getDebug()->addMessage(__CLASS__, sprintf('Found %s : %s : %s', $method, $route, json_encode($callback))); } + + /** + * addLazyRoute($route, $filename) + * @name addLazyRoute + * @author Andres Tello + * @param string $route + * @param string $filename + * + */ + public function addLazyRoute($route=false, $include=false) + { + $this->lazyRoute[]=array('path'=>$route, 'include'=>$include); + $this->lazyRegexes[]= "#^{$route}/(.*)$#"; + if(EPi::GetSetting('debug')) + getDebug()->addMessage(__CLASS__, sprintf('Found lazyRoute : %s : %s ', $path, $include)); + } + } function getRoute() diff --git a/src/EpiSession.php b/src/EpiSession.php index ff001da..038b84d 100644 --- a/src/EpiSession.php +++ b/src/EpiSession.php @@ -48,6 +48,8 @@ interface EpiSessionInterface { public function get($key = null); public function set($key = null, $value = null); + public function delete($key = null); + public function end(); } if(!function_exists('getSession')) @@ -55,6 +57,9 @@ public function set($key = null, $value = null); function getSession() { $employ = EpiSession::employ(); + if(sizeof($employ)==1) + $class=$employ; + else $class = array_shift($employ); if($employ && class_exists($class)) return EpiSession::getInstance($class, $employ); diff --git a/src/EpiSession_Php.php b/src/EpiSession_Php.php index e1ab103..04f2b3b 100644 --- a/src/EpiSession_Php.php +++ b/src/EpiSession_Php.php @@ -29,6 +29,13 @@ public function set($key = null, $value = null) $_SESSION[$key] = $value; return $value; } + + public function delete($key = null) + { + if(empty($key) || !isset($_SESSION[$key])) + return false; + unset $_SESSION[$key]; + } public function __construct() {