emjayess
10/5/2012 - 9:19 PM

Testing ways to fork code [drupal] paths for api versioning, in which supporting code files live in subfolders

Testing ways to fork code [drupal] paths for api versioning, in which supporting code files live in subfolders

<?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";
}
<?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();
  }
}
name = Sandbox: Api Forker
description = Testing ways to fork code paths for api versioning
core = 7.x
package = Sandbox
php = 5.4
<?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";
}