Created
October 5, 2012 21:19
-
-
Save emjayess/3842482 to your computer and use it in GitHub Desktop.
Testing ways to fork code [drupal] paths for api versioning, in which supporting code files live in subfolders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// resource.inc in api_forker/1.0/ | |
/** | |
* api forker callbacks for the beta version of the api | |
*/ | |
function api_forker_callback($api_version, $api_resource) { | |
$debug = '<pre>generated at line: ' . __LINE__ . ' in ' . __FILE__ . '</pre>'; | |
return "<p>Thanks for using Api Version <strong>$api_version!</strong></p> $debug"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name = Sandbox: Api Forker | |
description = Testing ways to fork code paths for api versioning | |
core = 7.x | |
package = Sandbox | |
php = 5.4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_menu() | |
*/ | |
function api_forker_menu() { | |
return [ | |
'api/%api_forker_version/%' => [ | |
'access callback' => true, | |
'load arguments' => array(2), | |
'page callback' => 'api_forker_callback', | |
'page arguments' => array(1, 2), | |
], | |
]; | |
} | |
/** | |
* api_forker_version_load callback | |
* @param $v api version in the request arguments | |
* @param $r api resource in the request arguments | |
* @return supported api version if both version and | |
* resource are valid; or print error and exit | |
*/ | |
function api_forker_version_load($v, $r) { | |
$supported_versions = ['beta', '1.0']; | |
if (in_array($v, $supported_versions)) { | |
$api_file = __DIR__ . "/$v/$r.inc"; | |
if (file_exists($api_file)) { | |
include_once $api_file; | |
} | |
else { | |
echo 'resource not found'; | |
drupal_exit(); | |
} | |
return $v; | |
} | |
else { | |
echo 'unsupported api version'; | |
drupal_exit(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// resource.inc in api_forker/beta/ | |
/** | |
* api callbacks for the beta version of the api | |
*/ | |
function api_forker_callback($api_version, $api_resource) { | |
$debug = '<pre>generated at line: ' . __LINE__ . ' in ' . __FILE__ . '</pre>'; | |
return "<p>Thanks for using Api Version <strong>$api_version!</strong></p> $debug"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the idea here is to support multiple versions of the [whatever] api, while keeping the code that supports each version cleanly isolated from each other.