<commit_before><?php

/**
 * Developement evnviroment
 */
define('IS_DEV', in_array(@$_SERVER['HTTP_HOST'], ['localhost']));

/**
 * Include path
 */
set_include_path(__DIR__.'/src'. PATH_SEPARATOR . get_include_path());

/**
 * Locale setting
 */
// List of supported timezones: http://php.net/manual/en/timezones.php
date_default_timezone_set('UTC');
setlocale(LC_ALL, 'en_US.UTF-8');

/**
 * Autoloading
 */
$loader = require __DIR__ . '/vendor/autoload.php';
$loader->add('', __DIR__ . '/src');

/**
 * Error handler
 */
error_reporting(E_ALL);
ini_set('display_errors', 'Off');
ini_set('log_errors', 'On');
ini_set('error_log', __DIR__ . '/tmp/logs/php_errors.txt');

if (php_sapi_name() !== 'cli') {

	$whoops = new \Whoops\Run;
	if (IS_DEV) {
		$handler = new \Whoops\Handler\PrettyPageHandler;
		$handler->setEditor('sublime');
		$whoops->pushHandler($handler);

	} else {
		$whoops->pushHandler(function($e){
			new Errors\Error(500, $e);
		});

	}
	$whoops->register();

} else {
	ini_set('display_errors', 'On');
}

/**
 * Container
 */
$container = new Pimple\Container;
$container['BASE_PATH'] = '/sos2.0/';

$container['router'] = function($c) {
	return new Socrate\Router($c);
};
$container['slugify'] = function($c) {
	return new Slugify();
};

/**
 * Interlinking functions
 */
function path($path = null) 
{
	global $container;
	return $container['BASE_PATH'] . $path;
}

function url($path = null, $schema = 'http://') 
{
	return $schema . $_SERVER['HTTP_HOST'] . path($path);
}

function url_for_home() 
{
	return url();
}

function url_for($name, $args = null) 
{
	global $container;
	return url($container['router']->getPath($name, $args));
}

/**
 * Utility functions
 */
function escape($input) 
{
	return htmlspecialchars($input, ENT_QUOTES|ENT_HTML5, 'UTF-8');
}

function slugify($text, $separator = '-') 
{
	global $container;
	return $container['slugify']->slugify($text, $separator);
}

function abort()
{
	throw new \Socrate\Http404();
}

/**
 * The dispatcher
 *//*
$dispatch = function($request, $rules) use($container) 
{

	foreach ($rules as $rule) 
	{
	
		if (preg_match($rule[0], $request, $match)) 
		{

			$_GET += $match;

			if (is_callable($rule[1])) 
			{
				return $rule[1]($container);

			}

			elseif (class_exists($rule[1]))
			{
				return new $rule[1]($container);
			}

			elseif (strpos($rule[1], '@'))
			{

				$subRule = explode('@', $rule[1]);
				
				$controller = new $subRule[0]($container);
				return $controller->{$subRule[1]}();
			}

			else 
			{
				require $rule[1];
			}

		}

	}

	throw new Http404;

};*/<commit_msg>Review of error handling/Bugs fix/New helper functions<commit_after><?php

/**
 * Developement evnviroment
 */
define('IS_DEV', in_array(@$_SERVER['HTTP_HOST'], ['localhost']));

/**
 * Include path
 */
set_include_path(__DIR__.'/src'. PATH_SEPARATOR . get_include_path());

/**
 * Locale setting
 */
// List of supported timezones: http://php.net/manual/en/timezones.php
date_default_timezone_set('UTC');
setlocale(LC_ALL, 'en_US.UTF-8');

/**
 * Autoloading
 */
$loader = require __DIR__ . '/vendor/autoload.php';
$loader->add('', __DIR__ . '/src');

/**
 * Error handler
 */
error_reporting(E_ALL);
ini_set('display_errors', 'On');
ini_set('log_errors', 'On');
ini_set('error_log', sprintf(__DIR__ . '/tmp/logs/php_errors-%s.txt', date('Y-d-m')));

if (php_sapi_name() !== 'cli') {

	if (IS_DEV) {
		$whoops = new \Whoops\Run;
		$handler = new \Whoops\Handler\PrettyPageHandler;
		$handler->setEditor('sublime');
		$whoops->pushHandler($handler);
		$whoops->register();

	} else {

		set_error_handler(function($errno, $errstr, $errfile, $errline) {
		    if (error_reporting() & $errno) {
				throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
		    }
		});

		set_exception_handler(function($e) {
			abort(500);
		});

		register_shutdown_function(function() {
		    $e = error_get_last();
		    if ($e['type'] === E_ERROR) {
		        abort(500);
		    }
		});

	}

}

/**
 * Service Container
 */
$container = new Pimple\Container;

if (IS_DEV) {
	$container['BASE_PATH'] = '/philosopher-project/';
} else {
	$container['BASE_PATH'] = '/';
}

$container['router'] = function($c) {
	return new Socrate\Router($c);
};
$container['slugify'] = function($c) {
	return new Cocur\Slugify\Slugify();
};
$container['db'] = function($c) {
	return new Socrate\Pdo('sqlite:'.__DIR__.'/data/db.sqlite');
};
$container['cache'] = function($c) {
	$cache = IS_DEV? new Doctrine\Common\Cache\ArrayCache(): new Doctrine\Common\Cache\PhpFileCache(__DIR__.'/tmp/cache');
	$cache->setNamespace(@$_SERVER['HTTP_HOST']);
	return $cache;
};

/**
 * Interlinking functions
 */
function path($path = null) 
{
	global $container;
	return $container['BASE_PATH'] . $path;
}

function url($path = null, $schema = 'http://') 
{
	return $schema . $_SERVER['HTTP_HOST'] . path($path);
}

function url_for_home() 
{
	return url();
}

function path_for($name, $args = null) 
{
	global $container;
	return path($container['router']->getPath($name, $args));
}

function url_for($name, $args = null) 
{
	global $container;
	return url($container['router']->getPath($name, $args));
}

function url_for_paginated($name, $args = null, $page = 1)
{
	$url = url_for($name, $args);
	if ($page > 1) {
		$url .= '?page='.$page;
	}

	return $url;
}

/**
 * Utility functions
 */
function escape($input) 
{
	return htmlspecialchars($input, ENT_QUOTES|ENT_HTML5, 'UTF-8');
}

function slugify($text, $separator = '-') 
{
	global $container;
	return $container['slugify']->slugify($text, $separator);
}

function abort($statusCode = 404, Exception $exception = null)
{
	global $container;
	return new Socrate\ErrorPage($container, $statusCode, $exception);
}

function isAjax()
{
	return !empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
		strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';     
}

/**
 * Caching helpers
 */
function cacheStart($id, $lifeTime = 0)
{
	global $container;

	$cache = $container['cache'];
	
	if ($cache->contains($id)) {
		echo $cache->fetch($id);
		return false;
	}

	ob_start(function($content) use ($cache, $id, $lifeTime){
		$cache->save($id, $content, $lifeTime);
		return $content;
	});

	return true;
}

function cacheStop() 
{
	echo ob_get_clean();
}

/**
 * Let's start
 */
ob_start();
{'fraction_non_alphanumeric': 0.20387424579231503, 'fraction_numerical': 0.006351222610352493, 'mean_word_length': 4.3654173764906306, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5128154', 'n_tokens_mistral': 2474, 'n_tokens_neox': 2324, 'n_words': 419}