Skip to content

Allow to load files and routes at run time. #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/Route.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 30 additions & 0 deletions src/EpiRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -94,6 +97,7 @@ public function delete($route, $callback, $isApi = false)
*/
public function load($file)
{
print "si si la estsa cargando...<br>\n";
$file = Epi::getPath('config') . "/{$file}";
if(!file_exists($file))
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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 <[email protected]>
* @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()
Expand Down
5 changes: 5 additions & 0 deletions src/EpiSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,18 @@ 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'))
{
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);
Expand Down
7 changes: 7 additions & 0 deletions src/EpiSession_Php.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down