<commit_before><?php

namespace Lava;

/*!
 * https://github.com/illogical-ru/lava-php
 *
 * Copyright 2015 illogical
 * Released under the MIT license
 */

// PHP >= 5.3
if (version_compare(phpversion(), '5.3') < 0)
	die('PHP 5.3+ is required');


class App {

	public  $conf, $env, $args,
		$stash,

		$safe;

	private $routes = array(),

		$types  = array(
			'text'  => 'text/plain',
			'html'  => 'text/html',
			'js'    => 'text/javascript',
			'json'  => 'application/json',
			'jsonp' => 'application/javascript',
			'xml'   => 'application/xml',
		);

	public function __construct ($conf = NULL) {

		$this->conf   = new Stash ($conf);
		$this->env    = new ENV;
		$this->args   = new Args;
		$this->cookie = new Cookie;
		$this->stash  = new Stash;
		$this->safe   = new Safe  ($this->conf->safe());

		if (method_exists($this, 'init')) $this->init();
	}

	public function host ($scheme = NULL) {

		$host = $this->env->host;

		if ($scheme === TRUE) {
			$secure =  $this->env->https
				&& $this->env->https != 'off';
			$scheme = 'http' . ($secure ? 's' : '');
		}
		if (isset($scheme)) $host = "${scheme}://${host}";

		return  $host;
	}

	public function home () {
		$home = func_get_args();
		array_unshift(
			$home, $this->conf->home ? $this->conf->home : getcwd()
		);
		return  join('/', $home);
	}

	public function pub  () {
		$pub  = func_get_args();
		array_unshift(
			$pub,  $this->conf->pub	? $this->conf->pub
						: preg_replace(
							'/\/[^\/]*$/', '',
							$this->env->script
						  )
		);
		return  join('/', $pub);
	}

	public function uri  ($uri = NULL, $data = NULL, $append = FALSE) {

		if     (! isset($uri))
			$uri  = $this->env->uri;
		elseif (  isset($this->routes[$uri]))
			$uri  = $this->routes[$uri]->uri($data);
		elseif (  isset($data) || $append) {
			$data = $this->args->_query($data, $append);
			if ($data) $uri	.= (strpos($uri, '?') ? '&' : '?')
					.   $data;
		}

		if     (! preg_match('/^(?:[a-zA-Z]+:\/)?\//', $uri))
			$uri  = $this->pub($uri);

		return  $uri;
	}
	public function url  () {

		$url  = call_user_func_array(
			array($this, 'uri'), func_get_args()
		);
		if (! preg_match('/^[a-zA-Z]+:\/\//', $url))
			$url = $this->host(TRUE) . $url;

		return  $url;
	}

	public function type ($type = NULL) {

		if     (  isset($type)) {

			$type = strtolower($type);

			if (isset($this->types[$type]))
				$type  = $this->types[$type];
			if (      $this->conf->charset)
				$type .= "; charset={$this->conf->charset}";

			return "Content-Type: ${type}";
		}
		elseif (  $this->env->is_rewrite) {
			if (preg_match('/\.(\w*)$/', $this->env->uri, $match))
				$type  = end($match);
		}
		else	$type = $this->args->type;

		if     (! isset($type))
			$type = $this->conf->type;

		return	strtolower($type);
	}

	public function render ($handler) {

		$type = $this->type();

		if     (  isset($handler[$type]))	$handler = $handler[$type];
		elseif (  isset($handler[    0])) 	$handler = $handler[    0];
		else					return;

		if     (! headers_sent()) {
			header($this->type($type));
			header('Expires: 0');
			header('Cache-Control: no-store, no-cache, must-revalidate');
			header('Pragma: no-cache');
		}

		$data = Test::is_fn($handler)	? call_user_func ($handler, $this)
						:                 $handler;

		if     (isset($data)) {
			if   ($type == 'json')	echo json_encode($data);
			else			echo             $data;
		}

		return  TRUE;
	}

	public function redirect () {
		$url = call_user_func_array(
			array($this, 'url'), func_get_args()
		);
		$this->render(array(
			'html' => function () use ($url) {
				header("Location: ${url}", TRUE, 301);
			},
			'json' => array('url'  =>  $url),
		));
	}

	public function route       ($rule, $cond = NULL) {
		if (strpos($rule, '/') !== 0)
			$rule = $this->pub($rule);
		return  $this->routes[] = new Route ($rule, $cond);
	}
	public function route_get   ($rule) {
		return  $this->route($rule, 'GET');
	}
	public function route_post  ($rule) {
		return  $this->route($rule, 'POST');
	}
	public function route_match ($uri = NULL, $env = NULL) {

		if   (isset($env))
			$this->env             = new Stash ($env);
		if   (isset($uri)) {
			$this->env->uri        = $uri;
			$this->env->is_rewrite = TRUE;
		}
		else	$uri                   = $this->env->uri;

		for ($i = count($this->routes); $i--;) {
			$route = array_shift($this->routes);
			if   ($route->name())<commit_msg>empty rule in route<commit_after><?php

namespace Lava;

/*!
 * https://github.com/illogical-ru/lava-php
 *
 * Copyright 2015 illogical
 * Released under the MIT license
 */

// PHP >= 5.3
if (version_compare(phpversion(), '5.3') < 0)
	die('PHP 5.3+ is required');


class App {

	public  $conf, $env, $args,
		$stash,

		$safe;

	private $routes = array(),

		$types  = array(
			'text'  => 'text/plain',
			'html'  => 'text/html',
			'js'    => 'text/javascript',
			'json'  => 'application/json',
			'jsonp' => 'application/javascript',
			'xml'   => 'application/xml',
		);

	public function __construct ($conf = NULL) {

		$this->conf   = new Stash ($conf);
		$this->env    = new ENV;
		$this->args   = new Args;
		$this->cookie = new Cookie;
		$this->stash  = new Stash;
		$this->safe   = new Safe  ($this->conf->safe());

		if (method_exists($this, 'init')) $this->init();
	}

	public function host ($scheme = NULL) {

		$host = $this->env->host;

		if ($scheme === TRUE) {
			$secure =  $this->env->https
				&& $this->env->https != 'off';
			$scheme = 'http' . ($secure ? 's' : '');
		}
		if (isset($scheme)) $host = "${scheme}://${host}";

		return  $host;
	}

	public function home () {
		$home = func_get_args();
		array_unshift(
			$home, $this->conf->home ? $this->conf->home : getcwd()
		);
		return  join('/', $home);
	}

	public function pub  () {
		$pub  = func_get_args();
		array_unshift(
			$pub,  $this->conf->pub	? $this->conf->pub
						: preg_replace(
							'/\/[^\/]*$/', '',
							$this->env->script
						  )
		);
		return  join('/', $pub);
	}

	public function uri  ($uri = NULL, $data = NULL, $append = FALSE) {

		if     (! isset($uri))
			$uri  = $this->env->uri;
		elseif (  isset($this->routes[$uri]))
			$uri  = $this->routes[$uri]->uri($data);
		elseif (  isset($data) || $append) {
			$data = $this->args->_query($data, $append);
			if ($data) $uri	.= (strpos($uri, '?') ? '&' : '?')
					.   $data;
		}

		if     (! preg_match('/^(?:[a-zA-Z]+:\/)?\//', $uri))
			$uri  = $this->pub($uri);

		return  $uri;
	}
	public function url  () {

		$url  = call_user_func_array(
			array($this, 'uri'), func_get_args()
		);
		if (! preg_match('/^[a-zA-Z]+:\/\//', $url))
			$url = $this->host(TRUE) . $url;

		return  $url;
	}

	public function type ($type = NULL) {

		if     (  isset($type)) {

			$type = strtolower($type);

			if (isset($this->types[$type]))
				$type  = $this->types[$type];
			if (      $this->conf->charset)
				$type .= "; charset={$this->conf->charset}";

			return "Content-Type: ${type}";
		}
		elseif (  $this->env->is_rewrite) {
			if (preg_match('/\.(\w*)$/', $this->env->uri, $match))
				$type  = end($match);
		}
		else	$type = $this->args->type;

		if     (! isset($type))
			$type = $this->conf->type;

		return	strtolower($type);
	}

	public function render ($handler) {

		$type = $this->type();

		if     (  isset($handler[$type]))	$handler = $handler[$type];
		elseif (  isset($handler[    0])) 	$handler = $handler[    0];
		else					return;

		if     (! headers_sent()) {
			header($this->type($type));
			header('Expires: 0');
			header('Cache-Control: no-store, no-cache, must-revalidate');
			header('Pragma: no-cache');
		}

		$data = Test::is_fn($handler)	? call_user_func ($handler, $this)
						:                 $handler;

		if     (isset($data)) {
			if   ($type == 'json')	echo json_encode($data);
			else			echo             $data;
		}

		return  TRUE;
	}

	public function redirect () {
		$url = call_user_func_array(
			array($this, 'url'), func_get_args()
		);
		$this->render(array(
			'html' => function () use ($url) {
				header("Location: ${url}", TRUE, 301);
			},
			'json' => array('url'  =>  $url),
		));
	}

	public function route       ($rule = '', $cond = NULL) {
		if (strpos($rule, '/') !== 0)
			$rule = $this->pub($rule);
		return  $this->routes[] = new Route ($rule, $cond);
	}
	public function route_get   ($rule = '') {
		return  $this->route($rule, 'GET');
	}
	public function route_post  ($rule = '') {
		return  $this->route($rule, 'POST');
	}
	public function route_match ($uri  = NULL, $env = NULL) {

		if   (isset($env))
			$this->env             = new Stash ($env);
		if   (isset($uri)) {
			$this->env->uri        = $uri;
			$this->env->is_rewrite = TRUE;
		}
		else	$uri                   = $this->env->uri;

		for ($i = count($this->routes); $i--;) {
			$route = array_shift($this->routes);
			if   ($route->name())
{'fraction_non_alphanumeric': 0.22776646300067888, 'fraction_numerical': 0.004073319755600814, 'mean_word_length': 2.6906054279749476, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 153, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '288202', 'n_tokens_mistral': 3800, 'n_tokens_neox': 3315, 'n_words': 688}