<commit_before><?php
/*
 * psx
 * A object oriented and modular based PHP framework for developing
 * dynamic web applications. For the current version and informations
 * visit <http://phpsx.org>
 *
 * Copyright (c) 2010-2013 <NAME> <<EMAIL>>
 *
 * This file is part of psx. psx is free software: you can
 * redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 *
 * psx is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with psx. If not, see <http://www.gnu.org/licenses/>.
 */

namespace PSX;

use InvalidArgumentException;
use PSX\Dispatch\FilterInterface;
use PSX\Http\Request;
use PSX\Http\Response;
use PSX\Loader\CallbackResolverInterface;
use PSX\Loader\Location;
use PSX\Loader\LocationFinderInterface;
use PSX\Loader\LocationFinder\RoutingFile;
use PSX\Loader\InvalidPathException;
use UnexpectedValueException;

/**
 * Loader
 *
 * @author  <NAME> <<EMAIL>>
 * @license http://www.gnu.org/licenses/gpl.html GPLv3
 * @link    http://phpsx.org
 */
class Loader implements LoaderInterface
{
	protected $locationFinder;
	protected $callbackResolver;

	protected $loaded = array();
	protected $routes = array();

	public function __construct(LocationFinderInterface $locationFinder, CallbackResolverInterface $callbackResolver)
	{
		$this->locationFinder   = $locationFinder;
		$this->callbackResolver = $callbackResolver;
	}

	/**
	 * Loads the location of the controller through the defined location finder. 
	 * Then calls the executor to create a new instance of the controller and
	 * call the fitting methods. Locations which gets resolved by the location 
	 * finder can only be loaded once
	 *
	 * @param PSX\Http\Request $request
	 * @param PSX\Http\Response $response
	 * @return PSX\ControllerAbstract
	 */
	public function load(Request $request, Response $response)
	{
		$path = $request->getUrl()->getPath();

		if(isset($this->routes[md5($path)]))
		{
			$path = $this->routes[md5($path)];
		}

		$location = $this->locationFinder->resolve($request->getMethod(), $path);

		if($location instanceof Location)
		{
			if(!in_array($location->getId(), $this->loaded))
			{
				$callback   = $this->callbackResolver->resolve($location, $request, $response);
				$controller = $this->runControllerLifecycle($callback, $request, $response);

				$this->loaded[] = $location->getId();

				return $controller;
			}
		}
		else
		{
			throw new InvalidPathException('Unkown module "' . $path . '"', 404);
		}
	}

	public function addRoute($sourcePath, $destPath)
	{
		$key = md5($path);

		$this->routes[$sourcePath] = $destPath;
	}

	public function getRoute($path)
	{
		$key = md5($path);

		return isset($this->routes[$key]) ? $this->routes[$key] : false;
	}

	protected function runControllerLifecycle($callback, Request $request, Response $response)
	{<commit_msg>fix variable name<commit_after><?php
/*
 * psx
 * A object oriented and modular based PHP framework for developing
 * dynamic web applications. For the current version and informations
 * visit <http://phpsx.org>
 *
 * Copyright (c) 2010-2013 <NAME> <<EMAIL>>
 *
 * This file is part of psx. psx is free software: you can
 * redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 *
 * psx is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with psx. If not, see <http://www.gnu.org/licenses/>.
 */

namespace PSX;

use InvalidArgumentException;
use PSX\Dispatch\FilterInterface;
use PSX\Http\Request;
use PSX\Http\Response;
use PSX\Loader\CallbackResolverInterface;
use PSX\Loader\Location;
use PSX\Loader\LocationFinderInterface;
use PSX\Loader\LocationFinder\RoutingFile;
use PSX\Loader\InvalidPathException;
use UnexpectedValueException;

/**
 * Loader
 *
 * @author  <NAME> <<EMAIL>>
 * @license http://www.gnu.org/licenses/gpl.html GPLv3
 * @link    http://phpsx.org
 */
class Loader implements LoaderInterface
{
	protected $locationFinder;
	protected $callbackResolver;

	protected $loaded = array();
	protected $routes = array();

	public function __construct(LocationFinderInterface $locationFinder, CallbackResolverInterface $callbackResolver)
	{
		$this->locationFinder   = $locationFinder;
		$this->callbackResolver = $callbackResolver;
	}

	/**
	 * Loads the location of the controller through the defined location finder. 
	 * Then calls the executor to create a new instance of the controller and
	 * call the fitting methods. Locations which gets resolved by the location 
	 * finder can only be loaded once
	 *
	 * @param PSX\Http\Request $request
	 * @param PSX\Http\Response $response
	 * @return PSX\ControllerAbstract
	 */
	public function load(Request $request, Response $response)
	{
		$path = $request->getUrl()->getPath();

		if(isset($this->routes[md5($path)]))
		{
			$path = $this->routes[md5($path)];
		}

		$location = $this->locationFinder->resolve($request->getMethod(), $path);

		if($location instanceof Location)
		{
			if(!in_array($location->getId(), $this->loaded))
			{
				$callback   = $this->callbackResolver->resolve($location, $request, $response);
				$controller = $this->runControllerLifecycle($callback, $request, $response);

				$this->loaded[] = $location->getId();

				return $controller;
			}
		}
		else
		{
			throw new InvalidPathException('Unkown module "' . $path . '"', 404);
		}
	}

	public function addRoute($sourcePath, $destPath)
	{
		$key = md5($destPath);

		$this->routes[$sourcePath] = $destPath;
	}

	public function getRoute($path)
	{
		$key = md5($path);

		return isset($this->routes[$key]) ? $this->routes[$key] : false;
	}

	protected function runControllerLifecycle($callback, Request $request, Response $response)
	{
{'fraction_non_alphanumeric': 0.11977365608299277, 'fraction_numerical': 0.00534423137378183, 'mean_word_length': 4.28927680798005, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '7021078', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1849, 'n_words': 588}