Skip to content

Instantly share code, notes, and snippets.

@emjayess
Created October 5, 2012 21:19
Show Gist options
  • Save emjayess/3842482 to your computer and use it in GitHub Desktop.
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
<?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";
}
name = Sandbox: Api Forker
description = Testing ways to fork code paths for api versioning
core = 7.x
package = Sandbox
php = 5.4
<?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();
}
}
<?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";
}
@emjayess
Copy link
Author

emjayess commented Oct 5, 2012

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment