Skip to content

Commit

Permalink
Improved current url detection and minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
andersonsalas committed Apr 14, 2018
1 parent 71fe796 commit dc23400
Show file tree
Hide file tree
Showing 5 changed files with 328 additions and 120 deletions.
5 changes: 2 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
[![Total Downloads](https://poser.pugx.org/luthier/luthier/downloads?format=flat-square)](https://packagist.org/packages/luthier/luthier)
[![Latest Unstable Version](https://poser.pugx.org/luthier/luthier/v/unstable?format=flat-square)](https://packagist.org/packages/luthier/luthier)
[![License](https://poser.pugx.org/luthier/luthier/license?format=flat-square)](https://packagist.org/packages/luthier/luthier)
[![composer.lock](https://poser.pugx.org/luthier/luthier/composerlock?format=flat-square)](https://packagist.org/packages/luthier/luthier)

Laravel-like routing and Middleware support for CodeIgniter 3. **Luthier-CI** makes the development of CodeIgniter apps even more enjoyable and simple!

Expand Down Expand Up @@ -226,13 +225,13 @@ inside the group will share the *prefix* (first argument) and, optionally, anoth
// Prefix only
Route::group('prefix', function(){
Route::get('bar','test@bar');
Route::get('baz','test@baz);
Route::get('baz','test@baz');
});

// Prefix and shared properties
Route::group('prefix', ['namespace' => 'foo', 'middleware' => ['Admin','IPFilter']], function(){
Route::get('bar','test@bar');
Route::get('baz','test@baz);
Route::get('baz','test@baz');
});
```

Expand Down
172 changes: 131 additions & 41 deletions src/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class Hook
* Defines all required hooks in order to boot Luthier-CI
*
* @param array $hooks Existing hooks
*
* @return array
*
* @access public
Expand Down Expand Up @@ -53,29 +53,27 @@ public static function getHooks($hooks = [])
$hooks['post_controller'][] = $_hook;
}

//
// Pre system hook
//

$hooks['pre_system'][] = function()
{
// Defining some constants
define('LUTHIER_CI_VERSION', 1.0);
//
// Luthier-CI Initialization
//
// Defining some constants, creating and loading required files, etc.
//

define('LUTHIER_CI_VERSION', '0.2.0');

$isAjax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
$isCli = is_cli();
$isWeb = !$isCli;

// Including facades
require_once __DIR__ . '/Facades/Route.php' ;

// Route files
if( !file_exists(APPPATH . '/routes') )
{
mkdir( APPPATH . '/routes' );
}

// Middleware folder
if( !file_exists(APPPATH . '/middleware') )
{
mkdir( APPPATH . '/middleware' );
Expand All @@ -101,7 +99,6 @@ public static function getHooks($hooks = [])
require_once(APPPATH . '/routes/api.php');
}

// CLI routes file
// (NOT WORKING YET!!!)
if( !file_exists(APPPATH . '/routes/cli.php') )
{
Expand All @@ -114,20 +111,24 @@ public static function getHooks($hooks = [])
Route::get('/', Route::DEFAULT_CONTROLLER . '@index' );
}

// Default (fake) controller
if( !file_exists(APPPATH . '/controllers/' . Route::DEFAULT_CONTROLLER . '.php'))
{
copy(__DIR__ . '/Resources/DefaultController.php', APPPATH.'/controllers/'. Route::DEFAULT_CONTROLLER .'.php' );
}

// Global functions
require_once( __DIR__ . '/Functions.php');

// Compile all routes
Route::compileAll();

// This allows us to use any HTTP Verb through a form with a hidden field

//
// HTTP verbs in forms fix
//
// Since we only can perform GET and POST request via traditional html forms,
// this allows us to use any HTTP Verb if the form contains a hidden field
// named "_method"
//

if(isset($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'post')
{
if(isset($_POST['_method']))
Expand All @@ -136,23 +137,127 @@ public static function getHooks($hooks = [])
}
}

// Parsing uri and setting the current Luthier route (if exists)
// FIXME: Maybe this isn't sufficient
$uri = '/';

if(isset($_SERVER['PATH_INFO']))
//
// Parsing the current url
//
// This is the same code of the CI_URI class, but since we can't load it here
// (because 'undefined constant' errors) we have not choice that copy the
// needed code:
//

$uriProtocol = config_item('uri_protocol');

$removeRelativeDirectory = function($uri)
{
$uris = array();
$tok = strtok($uri, '/');
while ($tok !== FALSE)
{
if (( ! empty($tok) OR $tok === '0') && $tok !== '..')
{
$uris[] = $tok;
}
$tok = strtok('/');
}

return implode('/', $uris);
};

$parseRequestUri = function() use($removeRelativeDirectory)
{
$uri = parse_url('http://dummy'.$_SERVER['REQUEST_URI']);
$query = isset($uri['query']) ? $uri['query'] : '';
$uri = isset($uri['path']) ? $uri['path'] : '';

if (isset($_SERVER['SCRIPT_NAME'][0]))
{
if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
{
$uri = (string) substr($uri, strlen($_SERVER['SCRIPT_NAME']));
}
elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
{
$uri = (string) substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
}
}

if (trim($uri, '/') === '' && strncmp($query, '/', 1) === 0)
{
$query = explode('?', $query, 2);
$uri = $query[0];
$_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : '';
}
else
{
$_SERVER['QUERY_STRING'] = $query;
}

parse_str($_SERVER['QUERY_STRING'], $_GET);

if ($uri === '/' OR $uri === '')
{
$uri = '/';
}

$uri = $removeRelativeDirectory($uri);

return $uri;
};

if($uriProtocol == 'REQUEST_URI')
{
$url = $parseRequestUri();
}
elseif($uriProtocol == 'QUERY_STRING')
{
$uri = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');

if (trim($uri, '/') === '')
{
$uri = '';
}
elseif (strncmp($uri, '/', 1) === 0)
{
$uri = explode('?', $uri, 2);
$_SERVER['QUERY_STRING'] = isset($uri[1]) ? $uri[1] : '';
$uri = $uri[0];
}

parse_str($_SERVER['QUERY_STRING'], $_GET);

$url = $removeRelativeDirectory($uri);
}
elseif($uriProtocol == 'PATH_INFO')
{
$url = isset($_SERVER['PATH_INFO']) ? trim($_SERVER['PATH_INFO']) : $parseRequestUri();
}
else
{
$uri = trim($_SERVER['PATH_INFO'],'/');
show_error('Unsupported uri protocol', 500, 'Luthier-CI boot error');
}

if(empty($url))
{
$url = '/';
}


//
// Setting the current Luthier-CI route
//
// With the url correctly parsed, now it's time to find what
// route matches that url
//

try
{
$currentRoute = Route::getByUrl($uri);
$currentRoute = Route::getByUrl($url);
}
catch(RouteNotFoundException $e)
{
Route::addCompiledRoute($uri);
$currentRoute = Route::any($uri, function(){
Route::addCompiledRoute($url);
$currentRoute = Route::any($url, function(){
if(!is_cli() && is_callable(Route::get404()))
{
$_404 = Route::get404();
Expand All @@ -169,10 +274,6 @@ public static function getHooks($hooks = [])
Route::setCurrentRoute($currentRoute);
};

//
// Pre controller hook
//

$hooks['pre_controller'][] = function()
{
global $params, $URI, $class, $method;
Expand Down Expand Up @@ -223,7 +324,7 @@ public static function getHooks($hooks = [])
{
$route->params[$pcount]->value = $URI->segment($i+1);

// Removing "sticky" route parameters from CI callback parameters
// Removing "sticky" route parameters
if(substr($route->params[$pcount]->getName(), 0, 1) == '_')
{
unset($params[$pcount]);
Expand All @@ -236,21 +337,12 @@ public static function getHooks($hooks = [])
Route::setCurrentRoute($route);
};

//
// Post controller constructor hook
//

$hooks['post_controller_constructor'][] = function()
{
global $params;

// Loading required URL helper
ci()->load->helper('url');

// Injecting current route
ci()->route = Route::getCurrentRoute();

// Injecting middleware class
ci()->middleware = new Middleware();

if(method_exists(ci(), 'preMiddleware'))
Expand All @@ -263,7 +355,7 @@ public static function getHooks($hooks = [])
ci()->middleware->run($middleware);
}

// Seting "sticky" route params values as default for current route
// Setting "sticky" route parameters values as default for current route
foreach(ci()->route->params as &$param)
{
if(substr($param->getName(),0,1) == '_')
Expand Down Expand Up @@ -291,9 +383,6 @@ public static function getHooks($hooks = [])
}
};

//
// Post controller hook
//

$hooks['post_controller'][] = function()
{
Expand All @@ -303,6 +392,7 @@ public static function getHooks($hooks = [])
}
};


return $hooks;
}
}
Loading

0 comments on commit dc23400

Please sign in to comment.