<commit_before><?php

defined('SYSPATH') or die('No direct script access.');

/**
 * The Aurora Routing class. Defines REST API URL and Reverse Routing
 *
 * @package Aurora
 * @author <NAME>
 * @copyright (c) 2013, <NAME>
 * @license http://enov.mit-license.org MIT
 *
 */
class Aurora_Aurora_Route
{

	/**
	 * @var array REST types
	 */
	protected static $_action_map = array
		(
		HTTP_Request::GET => 'index',
		HTTP_Request::PUT => 'update',
		HTTP_Request::POST => 'create',
		HTTP_Request::DELETE => 'delete',
	);

	/**
	 * Define routing scheme.
	 * To be used in init.php of this module
	 *
	 * @param string $uri
	 * @return array/boolean
	 */
	public static function map($route, $params, $request) {
		// find the controller
		$params = static::map_path($route, $params, $request);
		// find the action
		$params = static::map_method($route, $params, $request);
		// return
		return $params;
	}

	/**
	 * Maps `<path>` (or `$params['path']`) to the respective controller.
	 * Also, allows config routing
	 *
	 * Usage: In your bootstrap.php set the following route:
	 *
	 *     Route::set('rest-api', 'api/<path>', array('path' => '.*'))
	 *       ->filter(array('Aurora_Route', 'map_path'));
	 *
	 * @param Route $route
	 * @param array $params
	 * @param Request $request
	 * @return array
	 * @throws Kohana_Exception
	 */
	public static function map_path($route, $params, $request) {
		// test if `<path>` exists in params
		if (!isset($params['path']))
			throw new Kohana_Exception('Params should include path.');
		// explode path to pieces
		$pieces = explode('/', $params['path']);
		// Get the last piece of the uri and test if it contains a numeric ID
		$last = array_pop($pieces);
		if (Valid::digit($last)) {
			$id = $last;
			$controller = ucfirst(array_pop($pieces));
		} else {
			$id = NULL;
			$controller = ucfirst($last);
		}
		// construct $directory and cname
		$directory = 'API';
		$common_name = '';
		foreach ($pieces as $folder) {
			$directory .= DIRECTORY_SEPARATOR . ucfirst($folder);
			$common_name = ucfirst($folder) . '_';
		}
		$common_name .= $controller;
		// Test for the existance of a controller `Controller_API_Common_Name`
		$file = 'Controller' . DIRECTORY_SEPARATOR .
		  $directory . DIRECTORY_SEPARATOR .
		  $controller;
		if (Kohana::find_file('classes', $file)) {
			$params['directory'] = $directory;
			$params['controller'] = $controller;
			$params['id'] = $id;
		}
		// if no `Controller_API_Common_Name` test if routing via config exists
		else if (in_array($common_name, Kohana::$config->load('routes.api'))) {
			$params['directory'] = NULL;
			$params['controller'] = 'API';
			$params['common_name'] = $common_name;
			$params['id'] = $id;
		}
		// Throw HTTP 404 as the resource not available to the RESTful API
		else {
			throw new HTTP_Exception_404('Could not find a resource for ' . $common_name);
		}
		// return $params
		return $params;
	}

	/**
	 * Maps $request->method() to the controller action
	 *
	 * @param Route $route
	 * @param array $params
	 * @param Request $request
	 * @return array
	 * @throws Kohana_Exception
	 */
	public static function map_method($route, $params, $request) {
		// get the method from request
		$method = $request->method();
		// throw 'not allowed' http exception if method not available
		if (!isset(static::$_action_map[$method]))
			throw HTTP_Exception::factory(405)->allowed(array_keys(static::$_action_map));
		// set the action
		$params['action'] = static::$_action_map[$method];
		//return $params
		return $params;
	}

	/**
	 * Reverse Routing
	 * Get the uri from cname
	 *
	 * @return string URI of Model/Collection/Aurora/cname
	 */
	public static function reverse($object) {
		$cname = Aurora_Type::cname($object);
		return 'api/' . str_replace(
			'_', '/', strtolower($cname)
		);
	}

}
<commit_msg>Aurora_Route return false instead of throwing exceptions<commit_after><?php

defined('SYSPATH') or die('No direct script access.');

/**
 * The Aurora Routing class. Defines REST API URL and Reverse Routing
 *
 * @package Aurora
 * @author <NAME>
 * @copyright (c) 2013, <NAME>
 * @license http://enov.mit-license.org MIT
 *
 */
class Aurora_Aurora_Route
{

	/**
	 * @var array REST types
	 */
	protected static $_action_map = array
		(
		HTTP_Request::GET => 'index',
		HTTP_Request::PUT => 'update',
		HTTP_Request::POST => 'create',
		HTTP_Request::DELETE => 'delete',
	);

	/**
	 * Define routing scheme.
	 * To be used in init.php of this module
	 *
	 * @param string $uri
	 * @return array/boolean
	 */
	public static function map($route, $params, $request) {
		// find the controller
		$params = static::map_path($route, $params, $request);
		// find the action
		if ($params !== FALSE)
			$params = static::map_method($route, $params, $request);
		// return
		return $params;
	}

	/**
	 * Maps `<path>` (or `$params['path']`) to the respective controller.
	 * Also, allows config routing
	 *
	 * Usage: In your bootstrap.php set the following route:
	 *
	 *     Route::set('rest-api', 'api/<path>', array('path' => '.*'))
	 *       ->filter(array('Aurora_Route', 'map_path'));
	 *
	 * @param Route $route
	 * @param array $params
	 * @param Request $request
	 * @return array
	 * @throws Kohana_Exception
	 */
	public static function map_path($route, $params, $request) {
		// test if `<path>` exists in params
		if (!isset($params['path']))
			return false;
		// explode path to pieces
		$pieces = explode('/', $params['path']);
		// Get the last piece of the uri and test if it contains a numeric ID
		$last = array_pop($pieces);
		if (Valid::digit($last)) {
			$id = $last;
			$controller = ucfirst(array_pop($pieces));
		} else {
			$id = NULL;
			$controller = ucfirst($last);
		}
		// construct $directory and cname
		$directory = 'API';
		$common_name = '';
		foreach ($pieces as $folder) {
			$directory .= DIRECTORY_SEPARATOR . ucfirst($folder);
			$common_name = ucfirst($folder) . '_';
		}
		$common_name .= $controller;
		// Test for the existance of a controller `Controller_API_Common_Name`
		$file = 'Controller' . DIRECTORY_SEPARATOR .
		  $directory . DIRECTORY_SEPARATOR .
		  $controller;
		if (Kohana::find_file('classes', $file)) {
			$params['directory'] = $directory;
			$params['controller'] = $controller;
			$params['id'] = $id;
		} else if (in_array($common_name, (array) Kohana::$config->load('routes.api'))) {
			// if no `Controller_API_Common_Name` test if routing via config exists
			$params['directory'] = NULL;
			$params['controller'] = 'API';
			$params['common_name'] = $common_name;
			$params['id'] = $id;
		} else {
			// it's not a good idea to throw exceptions from parsing routes
			// because it would interfere with the work of other routes
			//
			// throw new HTTP_Exception_404('Could not find a resource for ' . $common_name);
			$params = false;
		}
		// return $params
		return $params;
	}

	/**
	 * Maps $request->method() to the controller action
	 *
	 * @param Route $route
	 * @param array $params
	 * @param Request $request
	 * @return array
	 * @throws Kohana_Exception
	 */
	public static function map_method($route, $params, $request) {
		// get the method from request
		$method = $request->method();
		// throw 'not allowed' http exception if method not available
		if (!isset(static::$_action_map[$method]))
			throw HTTP_Exception::factory(405)->allowed(array_keys(static::$_action_map));
		// set the action
		$params['action'] = static::$_action_map[$method];
		//return $params
		return $params;
	}

	/**
	 * Reverse Routing
	 * Get the uri from cname
	 *
	 * @return string URI of Model/Collection/Aurora/cname
	 */
	public static function reverse($object) {
		$cname = Aurora_Type::cname($object);
		return 'api/' . str_replace(
			'_', '/', strtolower($cname)
		);
	}

}

{'fraction_non_alphanumeric': 0.15816523191571374, 'fraction_numerical': 0.0029551586791725557, 'mean_word_length': 3.6223277909738716, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5144250', 'n_tokens_mistral': 2791, 'n_tokens_neox': 2448, 'n_words': 703}