<commit_before><?php
namespace puffin;

class view
{
	public static $params = [];

	public static $title = '';
	public static $title_template = '';

	public static $css = [];
	public static $css_template = '';

	public static $js = [];
	public static $js_template = '';

	public static $nonblocking_js = [];
	public static $nonblocking_js_template = '';

	public static $template = '';
	public static $template_template = '';

	public static $layout = '';
	public static $layout_template = '';

	public static function title( $title = '' )
	{
		if( !empty($title) )
		{
			self::$title = $title;
			self::$title_template = '{{$ TITLE }}'. $title .'{{/ TITLE }}';
		}
	}

	public static function template( $new_template = '' )
	{
		if( empty($new_template) )
		{
			return self::$template;
		}
		else
		{
			$path = SCRIPT_PATH . "/$new_template" . MUSTACHE_EXT;
			self::$template = $new_template;
			self::$template_template = '{{$ CONTENTS }}' . file_get_contents( $path ) . '{{/ CONTENTS }}';
		}
	}

	public static function layout( $new_layout = '' )
	{
		if( empty($new_layout) )
		{
			return self::$layout;
		}
		else
		{
			$path = LAYOUT_PATH . "/$new_layout" . MUSTACHE_EXT;
			self::$layout = $new_layout;
			self::$layout_template = file_get_contents( $path );
		}
	}

	public static function add_css( $path, $media='all', $condition=false )
	{
		$temp['src'] = $path;
		$temp['media'] = $media;
		$temp['condition'] = $condition;

		self::$css []= $temp;

		self::prepare_css();
	}

	public static function prepare_css()
	{
		$css_string = '';

		foreach( self::$css as $stylesheet )
		{
			if( $stylesheet['condition'] )
			{
				$css_string .= '<!--[if '.$stylesheet['condition'].']>' . chr(10) . chr(13);
			}
			$css_string .= '<link rel="stylesheet" type="text/css" href="'. $stylesheet['src'] . '" media="'.$stylesheet['media'].'" />' . chr(10) . chr(13);
			if( $stylesheet['condition'] )
			{
				$css_string .= '<![endif]-->' . chr(10) . chr(13);
			}
		}

		self::$css_template = '{{$ CSS }}' . $css_string . '{{/ CSS }}';
	}

	#######################

	public static function add_js( $path, $nonblocking = false )
	{
		if( $nonblocking )
		{
			self::$nonblocking_js []= '<script type="text/javascript" src="'.$path.'"></script>' . chr(10) . chr(13);
		}
		else
		{
			self::$js []= '<script type="text/javascript" src="'.$path.'"></script>' . chr(10) . chr(13);
		}

		self::prepare_js( $nonblocking );
	}

	public static function prepare_js( $nonblocking )
	{
		if( $nonblocking )
		{
			self::$nonblocking_js_template = '{{$ NONBLOCKING_JS }}' . implode(self::$nonblocking_js, '') . '{{/ NONBLOCKING_JS }}';
		}
		else
		{
			self::$js_template = '{{$ JS }}'. implode(self::$js, '') . '{{/ JS }}';
		}
	}

	public static function prepare_nonblocking_js()
	{

	}

	#######################

	public static function add_param( $key, $value )
	{
		if( !is_numeric($key) )
		{
			self::$params[$key] = $value;
		}
	}

	public static function add_params( $array )
	{
		if( is_array( $array ) )
		{
			foreach( $array as $k=>$v )
			{
				self::add_param( $k, $v );
			}
		}
	}

	public static function render_json( $json = '' )
	{
		header('Content-Type: application/json');
		echo json_encode($json);
		exit;
	}

	public static function prepare()
	{
		$layout = self::$layout;
		$prepared_template = "{{% BLOCKS }}{{< $layout }}"
							. self::$title_template
							. self::$css_template
							. self::$js_template
							. self::$template_template
							. self::$nonblocking_js_template
							. "{{/ $layout }}";<commit_msg>Ability to return alternate response codes with the render_json view<commit_after><?php
namespace puffin;

class view
{
	public static $params = [];

	public static $title = '';
	public static $title_template = '';

	public static $css = [];
	public static $css_template = '';

	public static $js = [];
	public static $js_template = '';

	public static $nonblocking_js = [];
	public static $nonblocking_js_template = '';

	public static $template = '';
	public static $template_template = '';

	public static $layout = '';
	public static $layout_template = '';

	public static function title( $title = '' )
	{
		if( !empty($title) )
		{
			self::$title = $title;
			self::$title_template = '{{$ TITLE }}'. $title .'{{/ TITLE }}';
		}
	}

	public static function template( $new_template = '' )
	{
		if( empty($new_template) )
		{
			return self::$template;
		}
		else
		{
			$path = SCRIPT_PATH . "/$new_template" . MUSTACHE_EXT;
			self::$template = $new_template;
			self::$template_template = '{{$ CONTENTS }}' . file_get_contents( $path ) . '{{/ CONTENTS }}';
		}
	}

	public static function layout( $new_layout = '' )
	{
		if( empty($new_layout) )
		{
			return self::$layout;
		}
		else
		{
			$path = LAYOUT_PATH . "/$new_layout" . MUSTACHE_EXT;
			self::$layout = $new_layout;
			self::$layout_template = file_get_contents( $path );
		}
	}

	public static function add_css( $path, $media='all', $condition=false )
	{
		$temp['src'] = $path;
		$temp['media'] = $media;
		$temp['condition'] = $condition;

		self::$css []= $temp;

		self::prepare_css();
	}

	public static function prepare_css()
	{
		$css_string = '';

		foreach( self::$css as $stylesheet )
		{
			if( $stylesheet['condition'] )
			{
				$css_string .= '<!--[if '.$stylesheet['condition'].']>' . chr(10) . chr(13);
			}
			$css_string .= '<link rel="stylesheet" type="text/css" href="'. $stylesheet['src'] . '" media="'.$stylesheet['media'].'" />' . chr(10) . chr(13);
			if( $stylesheet['condition'] )
			{
				$css_string .= '<![endif]-->' . chr(10) . chr(13);
			}
		}

		self::$css_template = '{{$ CSS }}' . $css_string . '{{/ CSS }}';
	}

	#######################

	public static function add_js( $path, $nonblocking = false )
	{
		if( $nonblocking )
		{
			self::$nonblocking_js []= '<script type="text/javascript" src="'.$path.'"></script>' . chr(10) . chr(13);
		}
		else
		{
			self::$js []= '<script type="text/javascript" src="'.$path.'"></script>' . chr(10) . chr(13);
		}

		self::prepare_js( $nonblocking );
	}

	public static function prepare_js( $nonblocking )
	{
		if( $nonblocking )
		{
			self::$nonblocking_js_template = '{{$ NONBLOCKING_JS }}' . implode(self::$nonblocking_js, '') . '{{/ NONBLOCKING_JS }}';
		}
		else
		{
			self::$js_template = '{{$ JS }}'. implode(self::$js, '') . '{{/ JS }}';
		}
	}

	public static function prepare_nonblocking_js()
	{

	}

	#######################

	public static function add_param( $key, $value )
	{
		if( !is_numeric($key) )
		{
			self::$params[$key] = $value;
		}
	}

	public static function add_params( $array )
	{
		if( is_array( $array ) )
		{
			foreach( $array as $k=>$v )
			{
				self::add_param( $k, $v );
			}
		}
	}

	public static function render_json( $json = '', $response_code = '' )
	{
		if ($response_code != '') {
			http_response_code($response_code);
		}
		header('Content-Type: application/json');
		echo json_encode($json);
		exit;
	}

	public static function prepare()
	{
		$layout = self::$layout;
		$prepared_template = "{{% BLOCKS }}{{< $layout }}"
							. self::$title_template
							. self::$css_template
							. self::$js_template
							. self::$template_template
							. self::$nonblocking_js_template
							. "{{/ $layout }}";
{'fraction_non_alphanumeric': 0.21011995036536604, 'fraction_numerical': 0.005514959327174962, 'mean_word_length': 3.425869432580842, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, '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': '4686689', 'n_tokens_mistral': 2940, 'n_tokens_neox': 2579, 'n_words': 497}