<commit_before><?php include_once('_config.php');

include_once(PRESTO_BASE.'/_helpers.php');
include_once(PRESTO_BASE.'/api.php');

/** Presto micro web services framework

*/
class Presto extends REST {
	public $call;
	public static $v; // version
	
	/** Initialize with the request, and start delegation */
	public function __construct($v = '' /* version */) {

		$this->_base = $_SERVER['DOCUMENT_ROOT'];
		set_error_handler(array($this, 'fail'));
		
		self::$v = $v;
		self::$req = new request();

		try {
			$this->filter(); // request filtering
			$this->dispatch(); // dispatch to loaded class->member based on $req	
		} catch (Exception $e) {
			throw $e; // errors are handled by delegator
		}
	}
	
	/** Apply request filters */
	private function filter() {	
	}
	
	/* Dispatch requests to classes and class methods */
	private function dispatch() {
		
		try {
	
			$obj = self::$req->uri->component('error' /* default to an error route */);
			$o = new $obj();	
			$action = self::$req->action;	// determines the request action (method)
			$thing = self::$req->uri->thing(); // determine the thing (resource)
	
			// validate that the concept noun is valid
			if (!$o->is_valid_concept($thing))
				$thing = ''; // no thing (resource) available, assume root action
	
			// validate that the content type is supported
			if (!$o->is_valid_contentType(self::$req->uri->type()))
				throw new Exception("Unsupported media type: $action $thing.", 415);

			// build the call pseudo object
			$method = (strlen($thing)) ? "{$action}_{$thing}" : $action;	
			$this->call = (object) array(
				'class' 	=> $obj,
				'method' 	=> $method,
				'res' 		=> self::$req->uri->type(), 
				'params' 	=> self::$req->uri->parameters,
				'exists' 	=> false);			
	
			// build the response object
			self::$resp = new response($this->call, self::$v);
	
			// verify the request
			
			if ($obj == 'error') // disallow root component access
				throw new Exception('Root access not allowed', 403);
			
			if (!method_exists($obj, $method)) // check that the resource is valid
				throw new Exception("Can't find $obj->$method()", 404);	

			$this->call->exists = true; 
			
			self::_trace("Dispatching to $obj :: $method");		
		
			// delegate
			$this->call->data = $o->$method( $this->call, self::$req->body() );
		
			// 
			if (is_object($this->call->data) || is_array($this->call->data))
				return self::$resp->ok($this->call->data, self::$req);
			else
				return $this->call->data;

		} catch (Exception $e) {			
			self::$resp->hdr($e->getCode());
			throw $e;
		}
	}

	/** Presto failures (including many PHP failures) */
	static public function fail($n, $text, $file, $line, $ctx) {
		$call = (object) array('res' => 'json');
		self::$resp = new response($call);

		$details = array(
			'code' => $n,
			'text' => $text, 
			'file' => $file,
			'line' => $line,
			'context' => $ctx
		);

		$codes = array(2 => '404');
		self::$resp->hdr(coalesce(@$codes[$n], '500'));
		
		print json_encode($details);
	}

	/** Debugging dump of Presto delegator */
	public function __toString() { return print_r($this, true); }
}

/** REST base class

	Handy constants and base methods
*/
class REST {

	public static $METHODS = array(
		'get', 'put', 'post', 'delete', 'options', 'head');
	public static $TYPES = array(
		'json', 'xml');
		
	public static $req;
	public static $resp; 
	public static $sess; 
	
	public static function _trace() {
		if (PRESTO_DEBUG == 0) return;
		
		print "TRACE: \n\t".implode("\n\t", func_get_args()) . "\n\n";
	}
	<commit_msg>Improved error generation for standard cases.<commit_after><?php include_once('_config.php');

include_once(PRESTO_BASE.'/_helpers.php');
include_once(PRESTO_BASE.'/api.php');

/** Presto micro web services framework

*/
class Presto extends REST {
	public $call;
	public static $v; // version
	
	/** Initialize with the request, and start delegation */
	public function __construct($v = '' /* version */) {

		$this->_base = $_SERVER['DOCUMENT_ROOT'];
		set_error_handler(array($this, 'fail'));
		
		self::$v = $v;
		self::$req = new request();

		try {
			$this->filter(); // request filtering
			$this->dispatch(); // dispatch to loaded class->member based on $req	
		} catch (Exception $e) {
			throw $e; // errors are handled by delegator
		}
	}
	
	/** Apply request filters */
	private function filter() {	
	}
	
	/* Dispatch requests to classes and class methods */
	private function dispatch() {
		
		try {
	
			$obj = self::$req->uri->component('error' /* default to an error route */);
			$o = new $obj();	
			$action = self::$req->action;	// determines the request action (method)
			$thing = self::$req->uri->thing(); // determine the thing (resource)
	
			// validate that the concept noun is valid
			if (!$o->is_valid_concept($thing))
				$thing = ''; // no thing (resource) available, assume root action
	
			// validate that the content type is supported
			if (!$o->is_valid_contentType(self::$req->uri->type()))
				throw new Exception("Unsupported media type: $action $thing.", 415);

			// build the call pseudo object
			$method = (strlen($thing)) ? "{$action}_{$thing}" : $action;	
			$this->call = (object) array(
				'class' 	=> $obj,
				'method' 	=> $method,
				'res' 		=> self::$req->uri->type(), 
				'params' 	=> self::$req->uri->parameters,
				'exists' 	=> false);			
	
			// build the response object
			self::$resp = new response($this->call, self::$v);
	
			// verify the request
			
			if ($obj == 'error') // disallow root component access
				throw new Exception('Root access not allowed', 403);
			
			if (!method_exists($obj, $method)) // check that the resource is valid
				throw new Exception("Can't find $obj->$method()", 404);	

			$this->call->exists = true; 
			
			self::_trace("Dispatching to $obj :: $method");		
		
			// delegate
			$this->call->data = $o->$method( $this->call, self::$req->body() );
		
			// 
			if (is_object($this->call->data) || is_array($this->call->data))
				return self::$resp->ok($this->call->data, self::$req);
			else
				return $this->call->data;

		} catch (Exception $e) {			
			self::$resp->hdr($e->getCode());
			throw $e;
		}
	}

	/** Presto failures (including many PHP failures) */
	static public function fail($n, $text, $file, $line, $ctx) {

		// set up pseudo call and response
		$call = (object) array('res' => 'json');
		self::$resp = new response($call);
		
		// generate useful HTTP status
		switch ($n) {
			case 2: $status = 404; break;
			default: $status = 500;
		}

		// build the resulting error object
		$details = (object) array(
			'status' => $status,
			'code' => $n,
			'error' => $text, 
			'file' => $file,
			'line' => $line,
			'ctx' => $ctx
		);

		self::$resp->hdr($status);
		print json_encode($details);
	}

	/** Debugging dump of Presto delegator */
	public function __toString() { return print_r($this, true); }
}

/** REST base class

	Handy constants and base methods
*/
class REST {

	public static $METHODS = array(
		'get', 'put', 'post', 'delete', 'options', 'head');
	public static $TYPES = array(
		'json', 'xml');
		
	public static $req;
	public static $resp; 
	public static $sess; 
	
	public static function _trace() {
		if (PRESTO_DEBUG == 0) return;
		
		print "TRACE: \n\t".implode("\n\t", func_get_args()) . "\n\n";
	}
	
{'fraction_non_alphanumeric': 0.1849671412924425, 'fraction_numerical': 0.004654983570646221, 'mean_word_length': 3.2545136866627837, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '2042805', 'n_tokens_mistral': 2745, 'n_tokens_neox': 2383, 'n_words': 673}