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

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

/* Presto micro web services framework

*/
class Presto extends REST {
	public $call;

	/* Initialize with the request, and start delegation */
	public function __construct() {

		$this->_base = $_SERVER['DOCUMENT_ROOT'];
		set_error_handler(array($this, 'fail'));

		self::$req = new request();

		try {
			$this->dispatch(); // dispatch to loaded class->member based on $req
		} catch (Exception $e) {
			throw $e; // errors are handled by delegator
		}
	}

	/* Dispatch requests to classes and class methods */
	private function dispatch() {

		try {

			$this->call = self::$req->scheme();

			$action = self::$req->action;	// the request action (method)
			$obj = $this->call->class;
			$method = $this->call->method;
			$preflight = $this->call->preflight;
			$type = self::$req->type;
			$model = null;

			$res = $this->call->resource; // the root resource

			presto_lib::_trace('REQUEST', "[{$this->call->file}] $obj::$method ({$this->call->type})", 
				json_encode($this->call->params), json_encode($this->call->options));

			// Create an an instance of the API subclass (autoloaded)
			
			autoload_delegate($this->call);
			
			if (!class_exists($obj))
				throw new Exception("API class not found for $obj::$method", 404);
				
			$o = new $obj();

			// Start the response setup
			
			self::$resp = new response($this->call);

			// Verify the request

			if ($obj == 'error') // disallow root component access
				throw new Exception('Root access not allowed', 403);

			$o->attach( $this->call, self::$resp, self::$req );

			if (!method_exists($obj, $preflight)) {
				
				// skip + trace missing preflight functions (data will be passed as standard HTTP params)
				
				presto_lib::_trace('PREFLIGHT', 'skipped', 
					"[{$this->call->file}] $obj::$preflight ({$this->call->type})", 
					json_encode($this->call->params), json_encode($this->call->options));
					
			} else {
			
				// attempt a "preflight" call (into the user class to generate a model)
			
				$model = $o->$preflight(
					$this->call->params, 
					$this->call->options, 
					self::$req->body(), 
					$this->call->type );
			}

			if (!method_exists($obj, $method)) // valid route?
				throw new Exception("Resource $obj->$method not found.", 404);

			$this->call->exists = true;

			// Perform the actual sub delegation
			
			if (isset($model))
				$this->call->data = $o->$method( $model, $this->call->type );
			else
				$this->call->data = $o->$method( 
					$this->call->params, 
					$this->call->options, 
					self::$req->body(), 
					$this->call->type );

			// Produce a response for the client
			
			presto_lib::_trace( PRESTO_TRACE_KEY, json_encode(Presto::trace_info()) );
			presto_lib::_trace( PRESTO_TRACE_KEY, json_encode(Profiler::profiles()) );

			$encode = (is_object($this->call->data) || is_array($this->call->data));
			return self::$resp->ok( $this->call, $encode, $o->status(), $o->headers() );

		} catch (Exception $e) {
			if (self::$resp === null) self::$resp = new response();

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

	/* Handle PrestoPHP failures (including all catchable PHP failures) */
	static public function fail($n, $text, $file, $line, $ctx) {

		// set up pseudo call and response
		if (self::$resp === null)
			self::$resp = new response($ctx);
<commit_msg>Issue #47. Updated to only add profiler output to trace if some exists. Also changed trace kkeyinng for the profile data.<commit_after><?php include_once('_config.php');

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

/* Presto micro web services framework

*/
class Presto extends REST {
	public $call;

	/* Initialize with the request, and start delegation */
	public function __construct() {

		$this->_base = $_SERVER['DOCUMENT_ROOT'];
		set_error_handler(array($this, 'fail'));

		self::$req = new request();

		try {
			$this->dispatch(); // dispatch to loaded class->member based on $req
		} catch (Exception $e) {
			throw $e; // errors are handled by delegator
		}
	}

	/* Dispatch requests to classes and class methods */
	private function dispatch() {

		try {

			$this->call = self::$req->scheme();

			$action = self::$req->action;	// the request action (method)
			$obj = $this->call->class;
			$method = $this->call->method;
			$preflight = $this->call->preflight;
			$type = self::$req->type;
			$model = null;

			$res = $this->call->resource; // the root resource

			presto_lib::_trace('REQUEST', "[{$this->call->file}] $obj::$method ({$this->call->type})", 
				json_encode($this->call->params), json_encode($this->call->options));

			// Create an an instance of the API subclass (autoloaded)
			
			autoload_delegate($this->call);
			
			if (!class_exists($obj))
				throw new Exception("API class not found for $obj::$method", 404);
				
			$o = new $obj();

			// Start the response setup
			
			self::$resp = new response($this->call);

			// Verify the request

			if ($obj == 'error') // disallow root component access
				throw new Exception('Root access not allowed', 403);

			$o->attach( $this->call, self::$resp, self::$req );

			if (!method_exists($obj, $preflight)) {
				
				// skip + trace missing preflight functions (data will be passed as standard HTTP params)
				
				presto_lib::_trace('PREFLIGHT', 'skipped', 
					"[{$this->call->file}] $obj::$preflight ({$this->call->type})", 
					json_encode($this->call->params), json_encode($this->call->options));
					
			} else {
			
				// attempt a "preflight" call (into the user class to generate a model)
			
				$model = $o->$preflight(
					$this->call->params, 
					$this->call->options, 
					self::$req->body(), 
					$this->call->type );
			}

			if (!method_exists($obj, $method)) // valid route?
				throw new Exception("Resource $obj->$method not found.", 404);

			$this->call->exists = true;

			// Perform the actual sub delegation
			
			if (isset($model))
				$this->call->data = $o->$method( $model, $this->call->type );
			else
				$this->call->data = $o->$method( 
					$this->call->params, 
					$this->call->options, 
					self::$req->body(), 
					$this->call->type );

			// Produce a response for the client
			
			presto_lib::_trace( PRESTO_TRACE_KEY, json_encode(Presto::trace_info()) );
			$profiles = Profiler::profiles(); // add any process profiling to trace
			if (!empty($profiles))
				presto_lib::_trace( PROFILER_TRACE_KEY, json_encode($profiles) );

			$encode = (is_object($this->call->data) || is_array($this->call->data));
			return self::$resp->ok( $this->call, $encode, $o->status(), $o->headers() );

		} catch (Exception $e) {
			if (self::$resp === null) self::$resp = new response();

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

	/* Handle PrestoPHP failures (including all catchable PHP failures) */
	static public function fail($n, $text, $file, $line, $ctx) {

		// set up pseudo call and response
		if (self::$resp === null)
			self::$resp = new response($ctx);

{'fraction_non_alphanumeric': 0.18267760322535798, 'fraction_numerical': 0.0027804810232170164, 'mean_word_length': 3.443483631871526, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 145, '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': '862723', 'n_tokens_mistral': 2733, 'n_tokens_neox': 2322, 'n_words': 588}