<commit_before><?php

/**
 * LH Core
 *
 * The core of any web-project by <NAME>
 *
 * @author		<NAME> <<EMAIL>>
 * @copyright	Copyright (C) 2015, <NAME>. All rights reserved. Unauthorized copying of this file, via any medium is strictly prohibited. Proprietary and confidential.
 * @license		http://LicenseUrl
 * @since		Version 0.1
 */

namespace LH\Core\Helpers;

use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use LH\Core\Exceptions\ConfigNotFoundException;

/**
 * The helper responsible for the routing
 * To use it, just add the following to routes.php:
 *
	Route::any('{all}', [
		'uses' => '\LH\Core\Helpers\RouteHelper@route'
	])->where('all', '.*');
 *
 *
 * @author		<NAME> <<EMAIL>>
 * @copyright	Copyright (C) 2015, <NAME>. All rights reserved. Unauthorized copying of this file, via any medium is strictly prohibited. Proprietary and confidential.
 * @license		http://LicenseUrl
 * @since		Version 0.1
 */
class RouteHelper extends Controller
{
	/**
	 * Do the route logic
	 * @param string $route
	 * @return mixed
	 */
	public function route($route)
	{
		$this->checkConfig();

		$response = $this->routeHandler($route);

		return $response;
	}

	/**
	 * Do the route logic
	 * @param string $route
	 * @return mixed
	 */
	private function routeHandler($route)
	{
		//Check if it is a valid route
		if (!$this->isValidRoute($route))
		{
			return abort(404);
		}

		//Get all the constants
		list($namespace, $defaultControllerName, $homeControllerName, $requestMethod, $requestAjax, $segments) = $this->getAllConstants();

		//Get the controller and make instance of defaultController
		list($controllerName, $remainingSegments) = $this->getController($namespace, $defaultControllerName, $homeControllerName, $requestAjax, $segments);
		if (!$controllerName)
		{
			return abort(404);
		}
		$this->prepareController($controllerName);

		//Get the name of the method
		list($methodName, $parameterSpecs, $parameters, $requiredRoles) = $this->getMethodName($controllerName, $requestMethod, $remainingSegments);
		if (!$methodName
			|| !$this->areParametersValid($parameterSpecs, $parameters))
		{
			return abort(404);
		}

		//Call the method
		return $this->callMethod($defaultControllerName, $controllerName, $methodName, $parameters, $requiredRoles);
	}

	/**
	 * Validate the route
	 * @param string $route
	 * @return bool
	 */
	private function isValidRoute($route)
	{
		//Only let the accepted routes through
		return (preg_match('/^[\w\d\/$\-_\.\+!\*]*$/', $route) === 1);
	}

	/**
	 * Get all the constants to do the routing
	 * @return array
	 */
	private function getAllConstants()
	{
		//Fetch namespace
		$namespace = Config::get('app.routing.namespace');
		$namespace = trim($namespace, '\\');
		//Fetch defaultController
		$defaultController = Config::get('app.routing.default');
		$defaultController = $namespace . '\\' . $defaultController;
		//Fetch homeController
		$homeController = Config::get('app.routing.home');
		$homeController = $namespace . '\\' . $homeController;

		//Get the current request
		$request = $this->getRouter()->getCurrentRequest();
		//Get the call-method
		$requestMethod = strtolower($request->method());
		if ($requestMethod == 'patch')
		{
			$requestMethod = 'put';
		}
		//Check if ajax-call
		$requestAjax = $request->ajax();
		//Get route and its parts
		$segments = $request->segments();

		return array($namespace, $defaultController, $homeController, $requestMethod, $requestAjax, $segments);
	}

	/**
	 * Get the controller from the input
	 * @param string $namespace
	 * @param string $defaultControllerName
	 * @param string $homeControllerName
	 * @param bool $requestAjax
	 * @param array $segments
	 * @return array
	 */
	private function getController($namespace, $defaultControllerName, $homeControllerName, $requestAjax, $segments)
	{
		//Go through all the parts back to front
		$controllerLoopCount = min(3, count($segments));
		for ($i = 0; $i <= $controllerLoopCount; ++$i)
		{
			//Make the className
			if ($i == $controllerLoopCount)
			{
				//HomeController
				$className = $homeControllerName;
			}
			else
			{
				//Form class-name
				$className = '';
				for ($j = 0; $j <= $i; ++$j)
				{
					$className .= '\\' . ucfirst($segments[$j]);
				}
				$className = $namespace . $className . ($requestAjax ? 'Ajax' : '') . 'Controller';

				//Home controller only approachable when called from '/'
				//Default controller not approachable
				if (in_array($className, array($homeControllerName, $defaultControllerName)))
				{
					break;<commit_msg>CORE-45: Added xml and json to the other possible controllers. So BlablaJsonController can be the controller if the request asks for json.<commit_after><?php

/**
 * LH Core
 *
 * The core of any web-project by Low<NAME>yghe
 *
 * @author		<NAME> <<EMAIL>>
 * @copyright	Copyright (C) 2015, <NAME>. All rights reserved. Unauthorized copying of this file, via any medium is strictly prohibited. Proprietary and confidential.
 * @license		http://LicenseUrl
 * @since		Version 0.1
 */

namespace LH\Core\Helpers;

use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use LH\Core\Exceptions\ConfigNotFoundException;

/**
 * The helper responsible for the routing
 * To use it, just add the following to routes.php:
 *
	Route::any('{all}', [
		'uses' => '\LH\Core\Helpers\RouteHelper@route'
	])->where('all', '.*');
 *
 *
 * @author		<NAME> <<EMAIL>>
 * @copyright	Copyright (C) 2015, <NAME>. All rights reserved. Unauthorized copying of this file, via any medium is strictly prohibited. Proprietary and confidential.
 * @license		http://LicenseUrl
 * @since		Version 0.1
 */
class RouteHelper extends Controller
{
	/**
	 * Do the route logic
	 * @param string $route
	 * @return mixed
	 */
	public function route($route)
	{
		$this->checkConfig();

		$response = $this->routeHandler($route);

		return $response;
	}

	/**
	 * Do the route logic
	 * @param string $route
	 * @return mixed
	 */
	private function routeHandler($route)
	{
		//Check if it is a valid route
		if (!$this->isValidRoute($route))
		{
			return abort(404);
		}

		//Get all the constants
		list($namespace, $defaultControllerName, $homeControllerName, $requestMethod, $requestType, $segments) = $this->getAllConstants();

		//Get the controller and make instance of defaultController
		list($controllerName, $remainingSegments) = $this->getController($namespace, $defaultControllerName, $homeControllerName, $requestType, $segments);
		if (!$controllerName)
		{
			return abort(404);
		}
		$this->prepareController($controllerName);

		//Get the name of the method
		list($methodName, $parameterSpecs, $parameters, $requiredRoles) = $this->getMethodName($controllerName, $requestMethod, $remainingSegments);
		if (!$methodName
			|| !$this->areParametersValid($parameterSpecs, $parameters))
		{
			return abort(404);
		}

		//Call the method
		return $this->callMethod($defaultControllerName, $controllerName, $methodName, $parameters, $requiredRoles);
	}

	/**
	 * Validate the route
	 * @param string $route
	 * @return bool
	 */
	private function isValidRoute($route)
	{
		//Only let the accepted routes through
		return (preg_match('/^[\w\d\/$\-_\.\+!\*]*$/', $route) === 1);
	}

	/**
	 * Get all the constants to do the routing
	 * @return array
	 */
	private function getAllConstants()
	{
		//Fetch namespace
		$namespace = Config::get('app.routing.namespace');
		$namespace = trim($namespace, '\\');
		//Fetch defaultController
		$defaultController = Config::get('app.routing.default');
		$defaultController = $namespace . '\\' . $defaultController;
		//Fetch homeController
		$homeController = Config::get('app.routing.home');
		$homeController = $namespace . '\\' . $homeController;

		//Get the current request
		$request = $this->getRouter()->getCurrentRequest();
		//Get the call-method
		$requestMethod = strtolower($request->method());
		if ($requestMethod == 'patch')
		{
			$requestMethod = 'put';
		}
		$requestType = null;
		//Check if ajax-call
		if ($request->ajax())
		{
			$requestType = 'ajax';
		}
		elseif ($request->wantsJson() || (InputHelper::getInstance()->has('output') && strtolower(InputHelper::getInstance()->get('output')) == 'json'))
		{
			$requestType = 'json';
		}
		elseif ($request->accepts(array('application/xml', 'text/xml')) || InputHelper::getInstance()->has('output') && strtolower(InputHelper::getInstance()->get('output')) == 'xml')
		{
			$requestType = 'xml';
		}
		//Get route and its parts
		$segments = $request->segments();

		return array($namespace, $defaultController, $homeController, $requestMethod, $requestType, $segments);
	}

	/**
	 * Get the controller from the input
	 * @param string $namespace
	 * @param string $defaultControllerName
	 * @param string $homeControllerName
	 * @param string $requestType
	 * @param array $segments
	 * @return array
	 */
	private function getController($namespace, $defaultControllerName, $homeControllerName, $requestType, $segments)
	{
		//Go through all the parts back to front
		$controllerLoopCount = min(3, count($segments));
		for ($i = 0; $i <= $controllerLoopCount; ++$i)
		{
			//Make the className
			if ($i == $controllerLoopCount)
			{
				//HomeController
				$className = $homeControllerName;
			}
			else
			{
				//Form class-name
				$className = '';
				for ($j = 0; $j <= $i; ++$j)
				{
					$className .= '\\' . ucfirst($segments[$j]);
				}
				$className = $namespace . $className . ($requestType ? ucfirst($requestType) : '') . 'Controller';

				//Home controller only approachable when called from '/'
				//Default controller not approachable
				if (in_array($className, array($homeControllerName, $defaultControllerName)))
				{
					break;
{'fraction_non_alphanumeric': 0.15226925038245792, 'fraction_numerical': 0.005303416624171341, 'mean_word_length': 3.957532861476239, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 5}, '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': '3200946', 'n_tokens_mistral': 3290, 'n_tokens_neox': 2942, 'n_words': 801}