diff --git a/App/Config/Routes.php b/App/Config/Routes.php
index 2c128c3..b8aa133 100644
--- a/App/Config/Routes.php
+++ b/App/Config/Routes.php
@@ -17,18 +17,16 @@
View::render('errors.404');
});
-Route::get('/', 'Home@index', ['namespace' => 'Frontend']);
-
-Route::group('/frontend', function(){
-
- Route::get('/', 'Home@index', ['namespace' => 'Frontend']);
- Route::get('/home', 'Home@index', ['namespace' => 'Frontend']);
-
+Route::namespace('frontend')->group(function(){
+ Route::get('/', 'Home@index');
});
-Route::group('/backend', function(){
-
- Route::get('/', 'Dashboard@index', ['namespace' => 'Backend']);
- Route::get('/dashboard', 'Dashboard@index', ['namespace' => 'Backend']);
+Route::prefix('frontend')->namespacer('frontend')->group(function(){
+ Route::get('/', 'Home@index');
+ Route::get('/home', 'Home@index');
+});
-}, ['middleware' => ['Auth']]);
\ No newline at end of file
+Route::prefix('backend')->namespacer('backend')->middleware(['auth'])->group(function(){
+ Route::get('/', 'Dashboard@index');
+ Route::get('/dashboard', 'Dashboard@index');
+});
\ No newline at end of file
diff --git a/CHANGELOG b/CHANGELOG
index 2355cf5..dd64f99 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,7 @@
+v.2.0.5
+ - [Yeni] Router Kütüphanesi tamamen yenilendi. Domain tanımlama desteği eklendi.
+ - [Düzeltme] Import kütüphanesi içerisindeki yükleme hataları giderildi.
+
v2.0.4
- [Yeni] Console desteği eklendi.
- [Düzeltme] Validation kütüphanesindeki rules() ve bulkData() methodları iyileştirildi.
diff --git a/System/Kernel/Constants.php b/System/Kernel/Constants.php
index 4c2da74..39eba64 100644
--- a/System/Kernel/Constants.php
+++ b/System/Kernel/Constants.php
@@ -42,7 +42,7 @@
define('ENV', 'development');
// Framework Version
-define('VERSION', '2.0.4');
+define('VERSION', '2.0.5');
// Default Timezone
define('TIMEZONE', 'Europe/Istanbul');
diff --git a/System/Kernel/Import.php b/System/Kernel/Import.php
index 0c8e0dc..626bfa2 100644
--- a/System/Kernel/Import.php
+++ b/System/Kernel/Import.php
@@ -44,12 +44,12 @@ public function view($file, $data = [])
*/
public function helper($file)
{
- $filePath = APP_DIR . 'Helpers/' . $file . '.php';
+ $filePath = APP_DIR . 'Helpers/' . ucfirst($file) . '.php';
if (file_exists($filePath))
require_once $filePath;
else
- throw new ExceptionHandler('Dosya bulunamadı.', 'Helper : ' . $file);
+ throw new ExceptionHandler('Dosya bulunamadı.', 'Helper : ' . ucfirst($file));
}
@@ -62,18 +62,18 @@ public function helper($file)
public static function model($file, $namespace = null)
{
if (is_null($namespace)) {
- $filePath = MODEL_DIR . $file . '.php';
- $class = 'App\\Models\\' . $file;
+ $filePath = MODEL_DIR . ucfirst($file) . '.php';
+ $class = 'App\\Models\\' . ucfirst($file);
} else {
- $filePath = MODEL_DIR . ucfirst($namespace) . '/' . $file . '.php';
- $class = 'App\\Models\\' . ucfirst($namespace) . '\\' . $file;
+ $filePath = MODEL_DIR . ucfirst($namespace) . '/' . ucfirst($file) . '.php';
+ $class = 'App\\Models\\' . ucfirst($namespace) . '\\' . ucfirst($file);
}
if (file_exists($filePath)) {
require_once $filePath;
return new $class;
} else
- throw new ExceptionHandler('Dosya bulunamadı.', 'Model : ' . $file);
+ throw new ExceptionHandler('Dosya bulunamadı.', 'Model : ' . ucfirst($file));
}
@@ -104,7 +104,7 @@ public static function config($file)
if (file_exists(APP_DIR . 'Config/' . ucwords($file) . '.php'))
return require APP_DIR . 'Config/' . ucwords($file) . '.php';
else
- throw new ExceptionHandler('Dosya bulunamadı.', 'Config : ' . APP_DIR . 'Config/' . $file . '.php');
+ throw new ExceptionHandler('Dosya bulunamadı.', 'Config : ' . APP_DIR . 'Config/' . ucfirst($file) . '.php');
}
diff --git a/System/Libs/Console/Console.php b/System/Libs/Console/Console.php
index 9d36209..dbbd0e1 100644
--- a/System/Libs/Console/Console.php
+++ b/System/Libs/Console/Console.php
@@ -86,8 +86,6 @@ public function run($params)
} else {
return $this->getColoredString('Gecersiz komut. "' . $params[0] . '"', 'white', 'red');
}
- } else if ($params[0] == 'routes') {
- return $this->getRoutes();
} else {
return $this->getColoredString('Gecersiz komut. "' . $params[0] . '"', 'white', 'red');
}
@@ -114,8 +112,6 @@ private function help()
$this->getColoredString('[clear:logs]', 'light_blue') . "\t\t" . $this->getColoredString('/App/Storage/Logs dizinini temizlemek icin kullanilir.') . "\n" .
$this->getColoredString('[clear:session]', 'light_blue') . "\t\t" . $this->getColoredString('Tum session verisini temizlemek icin kullanilir.') . "\n\n" .
- $this->getColoredString('[routes]', 'light_blue') . "\t\t" . $this->getColoredString('Tum rotalari listelemek icin kullanilir.') . "\n\n" .
-
$this->getColoredString('[-v]', 'light_blue') . "\t\t\t" . $this->getColoredString('Titan Web Framework versiyon bilgisini verir.') . "\n" .
$this->getColoredString('[-h]', 'light_blue') . "\t\t\t" . $this->getColoredString('Tum console komutlari ile ilgili bilgi verir.') . "\n\n" .
$this->getColoredString('// ========== [-] Titan Web Framework Console Komutlari ========== //', 'red');
@@ -346,27 +342,6 @@ private function clearSession()
return $this->getColoredString('Tum session verisi silindi.', 'light_blue');
}
- /**
- * Get Route List
- *
- * @return string
- */
- private function getRoutes()
- {
- // Getting Routes
- Import::config('routes');
- $routes = Router::getRoutes();
- $list = '';
-
- foreach ($routes as $method => $paths) {
- foreach ($paths as $key => $val) {
- $list .= str_pad($method, 10) . str_pad($val['pattern'], 30) . str_pad($val['fn'], 10) . "\n";
- }
- }
-
- return $list;
- }
-
/**
* Returns colored string
* @param string $string
diff --git a/System/Libs/Router/Router.php b/System/Libs/Router/Router.php
index f9f9de4..d72d2b0 100644
--- a/System/Libs/Router/Router.php
+++ b/System/Libs/Router/Router.php
@@ -1,285 +1,329 @@
$pattern,
- 'fn' => $fn
- );
+ // Routes
+ private static $routes = [];
+
+ // Middlewares
+ private static $middlewares = [];
+
+ // Base Route
+ private static $baseRoute = '/';
+
+ // Namespace
+ private static $namespace = '';
+
+ // Domain
+ private static $domain = '';
+
+ // Not Found Callback
+ private static $notFound = '';
+
+ // Groups
+ private static $groups = [];
+
+ // Group Counter
+ private static $groupped = 0;
+
+ // Patterns
+ private static $patterns = [
+ '{all}' => '([^/]+)',
+ '{num}' => '([0-9]+)',
+ '{alpha}' => '([a-zA-Z]+)',
+ '{alnum}' => '([a-zA-Z0-9_-]+)'
+ ];
+
+ // Namespaces
+ private static $namespaces = [
+ 'controllers' => 'App\\Controllers',
+ 'middlewares' => 'App\\Middlewares'
+ ];
+
+ private function __construct() {}
+ private function __clone() {}
+
+ /**
+ * Routing Groups
+ *
+ * @param callable $callback
+ */
+ public static function group($callback)
+ {
+ self::$groupped++;
+
+ self::$groups[] = [
+ 'baseRoute' => self::$baseRoute,
+ 'middlewares' => self::$middlewares,
+ 'namespace' => self::$namespace,
+ 'domain' => self::$domain
+ ];
+
+ // Call the Callable
+ call_user_func($callback);
+
+ self::$groupped--;
+
+ if (self::$groupped > 0) {
+ self::$baseRoute = self::$groups[self::$groupped-1]['baseRoute'];
+ self::$middlewares = self::$groups[self::$groupped-1]['middlewares'];
+ self::$namespace = self::$groups[self::$groupped-1]['namespace'];
+ self::$domain = self::$groups[self::$groupped-1]['domain'];
+ } else {
+ // Reset Base Route
+ self::$baseRoute = '/';
+
+ // Reset Middlewares
+ self::$middlewares = [];
+
+ // Reset Namespace
+ self::$namespace = '';
+
+ // Reset Domain
+ self::$domain = '';
+
+ // Reset Group Counter
+ self::$groupped = 0;
}
- }
-
- /**
- * Store a route and a handling function to be executed when accessed using one of the specified methods
- *
- * @param string $methods
- * @param string $pattern
- * @param object|callable $fn
- */
- public static function match($methods, $pattern, $fn, $params = [])
- {
- $pattern = self::$baseRoute . '/' . trim($pattern, '/');
- $pattern = self::$baseRoute ? rtrim($pattern, '/') : $pattern;
-
- if (is_callable($fn))
- $closure = $fn;
- elseif (stripos($fn, '@') !== false) {
-
- // Set Namespace if exist
- if (array_key_exists('namespace', $params))
- $closure = 'App\\Controllers\\' . $params['namespace'] . '\\' . $fn;
- else
- $closure = 'App\\Controllers\\' . $fn;
-
+ }
+
+ /**
+ * Defining Namespace
+ *
+ * @param string $namespace
+ */
+ public static function namespacer($namespace)
+ {
+ // Set Namespace
+ self::$namespace = $namespace;
+
+ return new self;
+ }
+
+ /**
+ * Defining Middlewares
+ *
+ * @param array $middlewares
+ */
+ public static function middleware($middlewares)
+ {
+ foreach ($middlewares as $middleware) {
+ self::$middlewares[$middleware] = [
+ 'callback' => self::$namespaces['middlewares'] . '\\' . ucfirst($middleware) . '@handle'
+ ];
}
- foreach (explode('|', $methods) as $method) {
-
- // Set Middleware if exist
- if (array_key_exists('middleware', $params)) {
- foreach ($params['middleware'] as $middleware) {
- self::$beforeRoutes[$method][] = [
- 'pattern' => $pattern,
- 'fn' => 'App\\Middlewares\\' . $middleware . '@handle'
- ];
- }
- }
-
- self::$afterRoutes[$method][] = array(
- 'pattern' => $pattern,
- 'fn' => $closure
- );
+ return new self;
+ }
+
+ /**
+ * Defining Prefix
+ *
+ * @param string $prefix
+ */
+ public static function prefix($prefix)
+ {
+ // Set Base Route
+ self::$baseRoute = '/' . $prefix;
+
+ return new self;
+ }
+
+ /**
+ * Defining Domain
+ *
+ * @param string $domain
+ */
+ public static function domain($domain)
+ {
+ self::$domain = $domain;
+ return new self;
+ }
+
+ /**
+ * Add Route
+ *
+ * @param string $method
+ * @param string $pattern
+ * @param string|callable $callback
+ */
+ public static function route($method, $pattern, $callback)
+ {
+ if ($pattern == '/')
+ $pattern = self::$baseRoute . trim($pattern, '/');
+ else {
+ if (self::$baseRoute == '/')
+ $pattern = self::$baseRoute . trim($pattern, '/');
+ else
+ $pattern = self::$baseRoute . $pattern;
}
- }
-
- /**
- * Shorthand for a route accessed using any method
- *
- * @param string $pattern
- * @param string $fn
- */
- public static function all($pattern, $fn, $params = [])
- {
- self::match('GET|POST|PUT|DELETE|OPTIONS|PATCH|HEAD', $pattern, $fn, $params);
- }
-
- /**
- * Shorthand for a route accessed using GET
- *
- * @param string $pattern
- * @param object|callable $fn
- */
- public static function get($pattern, $fn, $params = [])
- {
- self::match('GET', $pattern, $fn, $params);
- }
-
- /**
- * Shorthand for a route accessed using POST
- *
- * @param string $pattern
- * @param object|callable $fn
- */
- public static function post($pattern, $fn, $params = [])
- {
- self::match('POST', $pattern, $fn, $params);
- }
-
- /**
- * Shorthand for a route accessed using PATCH
- *
- * @param string $pattern
- * @param object|callable $fn
- */
- public static function patch($pattern, $fn, $params = [])
- {
- self::match('PATCH', $pattern, $fn, $params);
- }
-
- /**
- * Shorthand for a route accessed using DELETE
- *
- * @param string $pattern
- * @param object|callable $fn
- */
- public static function delete($pattern, $fn, $params = [])
- {
- self::match('DELETE', $pattern, $fn, $params);
- }
-
- /**
- * Shorthand for a route accessed using PUT
- *
- * @param string $pattern
- * @param object|callable $fn
- */
- public static function put($pattern, $fn, $params = [])
- {
- self::match('PUT', $pattern, $fn, $params);
- }
-
- /**
- * Shorthand for a route accessed using OPTIONS
- *
- * @param string $pattern
- * @param object|callable $fn
- */
- public static function options($pattern, $fn, $params = [])
- {
- self::match('OPTIONS', $pattern, $fn, $params);
- }
-
- /**
- * Mounts a collection of callbacks onto a base route
- *
- * @param string $baseRoute
- * @param callable $fn
- */
- public static function group($baseRoute, $fn, $params = [])
- {
- // Track current base route
- $curBaseRoute = self::$baseRoute;
-
- // Build new base route string
- self::$baseRoute .= $baseRoute;
- // Call the callable
- call_user_func($fn);
+ $uri = $pattern;
+ $pattern = str_replace(array_keys(self::$patterns), array_values(self::$patterns), $pattern);
+ $pattern = '/^' . str_replace('/', '\/', $pattern) . '$/';
+
+ if (is_callable($callback)) {
+ $closure = $callback;
+ } elseif (stripos($callback, '@') !== false) {
+ if (self::$namespace)
+ $closure = self::$namespaces['controllers'] . '\\' . ucfirst(self::$namespace) . '\\' . $callback;
+ else
+ $closure = self::$namespaces['controllers'] . '\\' . $callback;
+ }
- // Restore original base route
- self::$baseRoute = $curBaseRoute;
+ $routeArray = [
+ 'uri' => $uri,
+ 'method' => $method,
+ 'pattern' => $pattern,
+ 'callback' => $closure
+ ];
- // Set Middlewares
- if (!empty($params)) {
+ if (self::$namespace)
+ $routeArray['namespace'] = ucfirst(self::$namespace);
- // If namespace defined
- if (array_key_exists('namespace', $params)) {
- $methods = 'GET|POST|PUT|DELETE|OPTIONS|PATCH|HEAD';
+ if (!empty(self::$middlewares))
+ $routeArray['middlewares'] = self::$middlewares;
- foreach (explode('|', $methods) as $method) {
+ if (self::$domain)
+ $routeArray['domain'] = self::$domain;
- if (array_key_exists($method, self::$afterRoutes)) {
+ self::$routes[] = $routeArray;
+ }
- foreach (self::$afterRoutes[$method] as $key => $value) {
+ /**
+ * Execute Routing
+ */
+ public static function run()
+ {
+ $matched = 0;
+ $methodCheck = true;
+ $domainCheck = true;
- $patternExists = strpos($value['pattern'], $baseRoute);
+ foreach (self::$routes as $key => $val) {
- if ($patternExists !== false) {
- $fnParts = explode('App\Controllers', $value['fn']);
- $fnWithNameSpace = 'App\Controllers\\' . $params['namespace'] . $fnParts[1];
- self::$afterRoutes[$method][$key]['fn'] = $fnWithNameSpace;
- }
+ if (preg_match($val['pattern'], self::getCurrentUri(), $params)) {
- }
+ // Checking domain
+ if (array_key_exists('domain', $val)) {
+ if ($val['domain'] !== trim(str_replace('www.', '', $_SERVER['SERVER_NAME']), '/')) {
+ $domainCheck = false;
+ } else {
+ $domainCheck = true;
+ }
+ }
- }
+ // Checking request method
+ if ($val['method'] !== self::getRequestMethod()) {
+ $methodCheck = false;
+ } else {
+ $methodCheck = true;
+ }
- }
+ if ($domainCheck && $methodCheck) {
+ $matched++;
- }
+ array_shift($params);
- // If middleware defined
- if (array_key_exists('middleware', $params)) {
- $methods = 'GET|POST|PUT|DELETE|OPTIONS|PATCH|HEAD';
+ // Checking middlewares
+ if (array_key_exists('middlewares', $val)) {
+ foreach ($val['middlewares'] as $midKey => $midVal) {
+ list($controller, $method) = explode('@', $midVal['callback']);
- foreach ($params['middleware'] as $middleware) {
+ if (class_exists($controller)) {
+ call_user_func_array([new $controller, $method], []);
+ }
+ }
+ }
- foreach (explode('|', $methods) as $method) {
- self::$beforeRoutes[$method][] = [
- 'pattern' => $baseRoute,
- 'fn' => 'App\\Middlewares\\' . $middleware . '@handle'
- ];
+ if (is_callable($val['callback'])) {
+ call_user_func_array($val['callback'], array_values($params));
+ } else if (stripos($val['callback'], '@') !== false) {
+ list($controller, $method) = explode('@', $val['callback']);
- self::$beforeRoutes[$method][] = [
- 'pattern' => $baseRoute . '/.*',
- 'fn' => 'App\\Middlewares\\' . $middleware . '@handle'
- ];
- }
+ if (class_exists($controller)) {
+ call_user_func_array([new $controller, $method], array_values($params));
+ } else {
+ self::pageNotFound();
+ }
+ }
- }
- }
+ break;
+ }
+
+ }
- }
- }
+ }
- /**
- * Middleware
- *
- * @param string
- * @return void
- */
- public static function middleware($methods, $pattern, $fn)
- {
- $pattern = self::$baseRoute . '/' . trim($pattern, '/');
- $pattern = self::$baseRoute ? rtrim($pattern, '/') : $pattern;
-
- foreach (explode('|', $methods) as $method) {
- self::$beforeRoutes[$method][] = array(
- 'pattern' => $pattern,
- 'fn' => 'App\\Middlewares\\' . $fn . '@handle'
- );
+ if ($matched === 0)
+ self::pageNotFound();
+ }
+
+ /**
+ * Page Not Found Redirection
+ */
+ private static function pageNotFound()
+ {
+ if (self::$notFound && is_callable(self::$notFound)) {
+ call_user_func(self::$notFound);
+ } else {
+ header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
+ throw new \Exception("Hata: Controller bulunamadı");
+ }
+ }
+
+ /**
+ * Get Current URI
+ *
+ * @return string
+ */
+ public static function getCurrentUri()
+ {
+ // Get the current Request URI and remove rewrite base path from it
+ $uri = substr($_SERVER['REQUEST_URI'], strlen(self::getBasePath()));
+
+ // Don't take query params into account on the URL
+ if (strstr($uri, '?')) {
+ $uri = substr($uri, 0, strpos($uri, '?'));
}
- }
- /**
- * Get all request headers
- *
- * @return array
- */
- public static function getRequestHeaders()
- {
- // If getallheaders() is available, use that
- if (function_exists('getallheaders')) {
+ // Remove trailing slash + enforce a slash at the start
+ return '/' . trim($uri, '/');
+ }
+
+ /**
+ * Get Base Path
+ *
+ * @return string
+ */
+ public static function getBasePath()
+ {
+ $scriptName = array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1);
+ return implode('/', $scriptName) . '/';
+ }
+
+ /**
+ * Get All Request Headers
+ *
+ * @return array
+ */
+ public static function getRequestHeaders()
+ {
+ // If getallheaders() is available, use that
+ if (function_exists('getallheaders')) {
return getallheaders();
}
- // Method getallheaders() not available: manually extract 'm
+ // If getallheaders() is not available, use that
$headers = [];
foreach ($_SERVER as $name => $value) {
if ((substr($name, 0, 5) == 'HTTP_') || ($name == 'CONTENT_TYPE') || ($name == 'CONTENT_LENGTH')) {
@@ -288,23 +332,24 @@ public static function getRequestHeaders()
}
return $headers;
- }
-
- /**
- * Get the request method used, taking overrides into account
- *
- * @return string
- */
- public static function getRequestMethod()
- {
- // Take the method as found in $_SERVER
- $method = $_SERVER['REQUEST_METHOD'];
-
- // If it's a HEAD request override it to being GET and prevent any output, as per HTTP Specification
- if ($_SERVER['REQUEST_METHOD'] == 'HEAD') {
+ }
+
+ /**
+ * Get Request Method
+ *
+ * @return string
+ */
+ public static function getRequestMethod()
+ {
+ // Take the method as found in $_SERVER
+ $method = $_SERVER['REQUEST_METHOD'];
+
+ // If it's a HEAD request override it to being GET and prevent any output, as per HTTP Specification
+ if ($_SERVER['REQUEST_METHOD'] == 'HEAD') {
ob_start();
$method = 'GET';
}
+
// If it's a POST request, check for a method override header
elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
$headers = self::getRequestHeaders();
@@ -314,190 +359,114 @@ public static function getRequestMethod()
}
return $method;
- }
-
- /**
- * Execute the router: Loop all defined before middleware's and routes, and execute the handling function if a match was found
- *
- * @param object|callable $callback
- * @return bool
- */
- public static function run($callback = null)
- {
- // Define which method we need to handle
- self::$requestedMethod = self::getRequestMethod();
-
- // Handle all before middlewares
- if (isset(self::$beforeRoutes[self::$requestedMethod])) {
- self::handle(self::$beforeRoutes[self::$requestedMethod]);
- }
-
- // Handle all routes
- $numHandled = 0;
- if (isset(self::$afterRoutes[self::$requestedMethod])) {
- $numHandled = self::handle(self::$afterRoutes[self::$requestedMethod], true);
- }
-
- // If no route was handled, trigger the 404 (if any)
- if ($numHandled === 0) {
- if (self::$notFoundCallback && is_callable(self::$notFoundCallback)) {
- call_user_func(self::$notFoundCallback);
- } else {
- header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
- throw new ExceptionHandler("Hata", "Controller bulunamadı");
-
- }
- }
- // If a route was handled, perform the finish callback (if any)
- else {
- if ($callback) {
- $callback();
- }
- }
-
- // If it originally was a HEAD request, clean up after ourselves by emptying the output buffer
- if ($_SERVER['REQUEST_METHOD'] == 'HEAD') {
- ob_end_clean();
- }
-
- // Return true if a route was handled, false otherwise
- if ($numHandled === 0) {
- return false;
- }
-
- return true;
- }
-
- /**
- * Set the 404 handling function
- *
- * @param object|callable $fn
- */
- public static function set404($fn)
- {
- self::$notFoundCallback = $fn;
- }
-
- /**
- * List all routes
- *
- * @return string
- */
- public static function listRoutes()
- {
- return dd(self::$afterRoutes);
- }
-
- /**
- * Get route list
- *
- * @return array
- */
- public static function getRoutes()
- {
- return self::$afterRoutes;
- }
-
+ }
+
+ /**
+ * Add a Route Using Get Method
+ *
+ * @param string $pattern
+ * @param string|callable $callback
+ */
+ public static function get($pattern, $callback)
+ {
+ self::route('GET', $pattern, $callback);
+ return new self;
+ }
+
+ /**
+ * Add a Route Using Post Method
+ *
+ * @param string $pattern
+ * @param string|callable $callback
+ */
+ public static function post($pattern, $callback)
+ {
+ self::route('POST', $pattern, $callback);
+ }
+
+ /**
+ * Add a Route Using Patch Method
+ *
+ * @param string $pattern
+ * @param string|callable $callback
+ */
+ public static function patch($pattern, $callback)
+ {
+ self::route('PATCH', $pattern, $callback);
+ }
+
+ /**
+ * Add a Route Using Delete Method
+ *
+ * @param string $pattern
+ * @param string|callable $callback
+ */
+ public static function delete($pattern, $callback)
+ {
+ self::route('DELETE', $pattern, $callback);
+ }
+
+ /**
+ * Add a Route Using Put Method
+ *
+ * @param string $pattern
+ * @param string|callable $callback
+ */
+ public static function put($pattern, $callback)
+ {
+ self::route('PUT', $pattern, $callback);
+ }
+
+ /**
+ * Add a Route Using Options Method
+ *
+ * @param string $pattern
+ * @param string|callable $callback
+ */
+ public static function options($pattern, $callback)
+ {
+ self::route('OPTIONS', $pattern, $callback);
+ }
+
/**
- * Handle a set of routes: if a match is found, execute the relating handling function
+ * Add a Route Using Multiple Methods
*
- * @param array $routes
- * @param boolean $quitAfterRun
- * @return int
+ * @param array $methods
+ * @param string $pattern
+ * @param string|callable $callback
*/
- private static function handle($routes, $quitAfterRun = false)
+ public static function match($methods, $pattern, $callback)
{
- // Counter to keep track of the number of routes we've handled
- $numHandled = 0;
-
- // The current page URL
- $uri = self::getCurrentUri();
-
- // Loop all routes
- foreach ($routes as $route) {
-
- // we have a match!
- if (preg_match_all('#^' . $route['pattern'] . '$#', $uri, $matches, PREG_OFFSET_CAPTURE)) {
- // Rework matches to only contain the matches, not the orig string
- $matches = array_slice($matches, 1);
-
- // Extract the matched URL parameters (and only the parameters)
- $params = array_map(function ($match, $index) use ($matches) {
- // We have a following parameter: take the substring from the current param position until the next one's position
- if (isset($matches[$index + 1]) && isset($matches[$index + 1][0]) && is_array($matches[$index + 1][0])) {
- return trim(substr($match[0][0], 0, $matches[$index + 1][0][1] - $match[0][1]), '/');
- }
- // We have no following parameters: return the whole lot
- else {
- return (isset($match[0][0]) ? trim($match[0][0], '/') : null);
- }
- }, $matches, array_keys($matches));
-
- // Call the handling function with the URL parameters if the desired input is callable
- if (is_callable($route['fn'])) {
- call_user_func_array($route['fn'], $params);
- }
- // if not, check the existence of special parameters
- elseif (stripos($route['fn'], '@') !== false) {
- // explode segments of given route
- list($controller, $method) = explode('@', $route['fn']);
-
- // check if class exists, if not just ignore.
- if (class_exists($controller)) {
- // first check if is a static method, directly trying to invoke it. if isn't a valid static method, we will try as a normal method invocation.
- if (call_user_func_array(array(new $controller, $method), $params) === false) {
- // try call the method as an non-static method. (the if does nothing, only avoids the notice)
- if (forward_static_call_array(array($controller, $method), $params) === false) ;
- }
- }
- }
-
- $numHandled++;
-
- // If we need to quit, then quit
- if ($quitAfterRun) {
- break;
- }
- }
-
+ foreach ($methods as $method) {
+ self::route(strtoupper($method), $pattern, $callback);
}
-
- // Return the number of routes handled
- return $numHandled;
- }
-
- /**
- * Define the current relative URI
- *
- * @return string
- */
- protected static function getCurrentUri()
- {
- // Get the current Request URI and remove rewrite base path from it (= allows one to run the router in a sub folder)
- $uri = substr($_SERVER['REQUEST_URI'], strlen(self::getBasePath()));
-
- // Don't take query params into account on the URL
- if (strstr($uri, '?')) {
- $uri = substr($uri, 0, strpos($uri, '?'));
- }
-
- // Remove trailing slash + enforce a slash at the start
- return '/' . trim($uri, '/');
}
- /**
- * Return server base Path, and define it if isn't defined.
- *
- * @return string
- */
- protected static function getBasePath()
- {
- // Check if server base path is defined, if not define it.
- if (null === self::$serverBasePath) {
- self::$serverBasePath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/';
+ /**
+ * List All Routes
+ *
+ * @return array
+ */
+ public static function getRoutes()
+ {
+ return self::$routes;
+ }
+
+ /**
+ * Set the 404 handling function
+ *
+ * @param object|callable $callback
+ */
+ public static function set404($callback)
+ {
+ self::$notFound = $callback;
+ }
+
+ public static function __callStatic($method, $args)
+ {
+ if ($method == 'namespace') {
+ self::namespacer($args[0]);
+ return new self;
}
-
- return self::$serverBasePath;
- }
-
-}
+ }
+}
\ No newline at end of file
diff --git a/index.php b/index.php
index 069ae1a..7007da6 100644
--- a/index.php
+++ b/index.php
@@ -6,7 +6,7 @@
* Author : Turan Karatuğ
* Web : http://www.titanphp.com
* Docs : http://kilavuz.titanphp.com
- * Version : 2.0.4
+ * Version : 2.0.5
* Github : http://github.com/tkaratug/titan2
* License : MIT
*