From 84c64e39d96889e6c75c4df5f3e98af68ec687ea Mon Sep 17 00:00:00 2001 From: Stefano Azzolini Date: Mon, 17 Nov 2014 18:27:02 +0100 Subject: [PATCH] Multiple version support Signed-off-by: Stefano Azzolini --- classes/API.php | 23 +++++++++++------------ composer.json | 2 +- config.sample.php | 3 ++- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/classes/API.php b/classes/API.php index 56956da..087eae2 100644 --- a/classes/API.php +++ b/classes/API.php @@ -19,22 +19,21 @@ public static function error($message,$status=501){ public static function run(callable $main = null){ $API_VERS = Options::get('base.api_version',''); // Load Routes - $route_file = rtrim(Options::get('base.endpoints',APP_DIR.'/routes.php'),'/'); + $route = rtrim(Options::get('base.endpoints',APP_DIR.'/routes.php'),'/'); // Single file - if (is_file($route_file)){ - include $route_file; + if (is_file($route)){ + include $route; } else { // Load directory - $route_file .= rtrim('/'.$API_VERS,'/'); - if (is_dir($route_file)){ - Route::group("/$API_VERS",function() use ($route_file,$API_VERS){ - Route::any('/',function() use ($API_VERS){ - Event::trigger('api.index',[$API_VERS]); + foreach((array)$API_VERS as $API_NAMESPACE){ + $routes = $route . rtrim('/'.$API_NAMESPACE,'/'); + if (is_dir($routes)){ + Route::group("/$API_NAMESPACE",function() use ($routes,$API_NAMESPACE){ + Event::trigger('api.before'); + array_map(function($f){include $f;},glob($routes.'/*.php')); + Event::trigger('api.after'); }); - Event::trigger('api.before'); - array_map(function($f){include $f;},glob($route_file.'/*.php')); - Event::trigger('api.after'); - }); + } } } Event::trigger('api.run',[Options::get('base.api_version','')]); diff --git a/composer.json b/composer.json index 91cb3f2..3e28718 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "description": "API extensions for Caffeina Core", "keywords": ["core","api","rest","server"], "homepage": "http://labs.caffeina.co/tech/core", - "version": "1.0.2", + "version": "1.0.5", "license": "MIT", "authors": [ { diff --git a/config.sample.php b/config.sample.php index 3f5cf2b..ded8a6b 100644 --- a/config.sample.php +++ b/config.sample.php @@ -9,7 +9,8 @@ // Pass the endpoints directory. // API routes will be loaded in {endpoints}/{api_version}/*.php 'endpoints' => APP_DIR.'/endpoints', - 'api_version' => 'v1', + // Enable versions + 'api_version' => ['v1'], // can have multiple values ], /**