<commit_before><?php
/**
 * Dispatcher takes the URL information, parses it for paramters and
 * tells the involved controllers what to do.
 *
 * This is the heart of Cake's operation.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       cake
 * @subpackage    cake.cake
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

/**
 * List of helpers to include
 */
App::import('Core', 'Router', false);
App::import('Core', 'CakeRequest', false);
App::import('Core', 'CakeResponse', false);
App::import('Controller', 'Controller', false);

/**
 * Dispatcher converts Requests into controller actions.  It uses the dispatched Request
 * to locate and load the correct controller.  If found, the requested action is called on 
 * the controller.
 *
 * @package       cake
 * @subpackage    cake.cake
 */
class Dispatcher {

/**
 * Current URL
 *
 * @var string
 * @access public
 */
	public $here = false;

/**
 * The request object
 *
 * @var CakeRequest
 * @access public
 */
	public $request = null;

/**
 * Response object used for asset/cached responses.
 *
 * @var CakeResponse
 */
	public $response = null;

/**
 * Constructor.
 */
	public function __construct($url = null, $base = false) {
		if ($base !== false) {
			Configure::write('App.base', $base);
		}
		if ($url !== null) {
			$this->dispatch($url);
		}
	}

/**
 * Dispatches and invokes given Request, handing over control to the involved controller. If the controller is set
 * to autoRender, via Controller::$autoRender, then Dispatcher will render the view.
 *
 * Actions in CakePHP can be any public method on a controller, that is not declared in Controller.  If you
 * want controller methods to be public and in-accesible by URL, then prefix them with a `_`.  
 * For example `public function _loadPosts() { }` would not be accessible via URL.  Private and protected methods
 * are also not accessible via URL.
 *
 * If no controller of given name can be found, invoke() will throw an exception.
 * If the controller is found, and the action is not found an exception will be thrown.
 *
 * @param CakeRequest $request Request object to dispatch.
 * @param array $additionalParams Settings array ("bare", "return") which is melded with the GET and POST params
 * @return boolean Success
 * @throws MissingControllerException, MissingActionException, PrivateActionException if any of those error states
 *    are encountered.
 */
	public function dispatch(CakeRequest $request, $additionalParams = array()) {
		$this->here = $request->here;

		if ($this->asset($request->url) || $this->cached($request->url)) {
			return;
		}

		$this->request = $this->parseParams($request, $additionalParams);
		$controller = $this->_getController($this->request);

		if (!is_object($controller)) {
			Router::setRequestInfo($request);
			throw new MissingControllerException(array(
				'controller' => Inflector::camelize($request->params['controller']) . 'Controller'
			));
		}

		Router::setRequestInfo($request);

		if ($this->_isPrivateAction($request)) {
			throw new PrivateActionException(array(
				'controller' => Inflector::camelize($request->params['controller']) . "Controller",
				'action' => $request->params['action']
			));
		}

		return $this->_invoke($controller, $request);
	}

/**
 * Check if the request's action is marked as private, with an underscore, of if the request is attempting to 
 * directly accessing a prefixed action.
 *
 * @param CakeRequest $request The request to check
 * @return boolean
 */
	protected function _isPrivateAction($request) {
		$privateAction = $request->params['action'][0] === '_';
		$prefixes = Router::prefixes();

		if (!empty($prefixes)) {
			if (isset($request->params['prefix'])) {
				$request->params['action'] = $request->params['prefix'] . '_' . $request->params['action'];
			} elseif (strpos($request->params['action'], '_') > 0) {
				list($prefix, $action) = explode('_', $request->params['action']);
				$privateAction = in_array($prefix, $prefixes);
			}
		}
		return $privateAction;
	}

/**
 * Initializes the components and models a controller will be using.
 * Triggers the controller action, and invokes the rendering if Controller::$autoRender is true and echo's the output.
 * Otherwise the return value of the controller action are returned.
 *
 * @param Controller $controller Controller to invoke
 * @param CakeRequest $request The request object to invoke the controller for.
 * @return string Output as sent by controller
 * @throws MissingActionException when the action being called is missing.
 */
	protected function _invoke(Controller $controller, CakeRequest $request) {
		$controller->constructClasses();
		$controller->startupProcess();

		$methods = array_flip($controller->methods);

		if (!isset($methods[$request->params['action']])) {
			if ($controller->scaffold !== false) {
				App::import('Controller', 'Scaffold', false);
				return new Scaffold($controller, $request);
			}
			throw new MissingActionException(array(
				'controller' => Inflector::camelize($request->params['controller']) . "Controller",
				'action' => $request->params['action']
			));
		}
		$result = call_user_func_array(array(&$controller, $request->params['action']), $request->params['pass']);
		$response = $controller->getResponse();

		if ($controller->autoRender) {
			$controller->render();
		} elseif ($response->body() === null) {
			$response->body($result);
		}
		$controller->shutdownProcess();

		if (isset($request->params['return'])) {
			return $response->body();
		}
		$response->send();
	}

/**
 * Applies Routing and additionalParameters to the request to be dispatched.
 * If Routes have not been loaded they will be loaded, and app/config/routes.php will be run.
 *
 * @param CakeRequest $request CakeRequest object to mine for parameter information.
 * @param array $additionalParams An array of additional parameters to set to the request.
 *   Useful when Object::requestAction() is involved
 * @return CakeRequest The request object with routing params set.
 */
	public function parseParams(CakeRequest $request, $additionalParams = array()) {
		if (count(Router::$routes) > 0) {
			$namedExpressions = Router::getNamedExpressions();
			extract($namedExpressions);<commit_msg>Fixing an entirely wrong condition that prevented routes from being loaded.<commit_after><?php
/**
 * Dispatcher takes the URL information, parses it for paramters and
 * tells the involved controllers what to do.
 *
 * This is the heart of Cake's operation.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       cake
 * @subpackage    cake.cake
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

/**
 * List of helpers to include
 */
App::import('Core', 'Router', false);
App::import('Core', 'CakeRequest', false);
App::import('Core', 'CakeResponse', false);
App::import('Controller', 'Controller', false);

/**
 * Dispatcher converts Requests into controller actions.  It uses the dispatched Request
 * to locate and load the correct controller.  If found, the requested action is called on 
 * the controller.
 *
 * @package       cake
 * @subpackage    cake.cake
 */
class Dispatcher {

/**
 * Current URL
 *
 * @var string
 * @access public
 */
	public $here = false;

/**
 * The request object
 *
 * @var CakeRequest
 * @access public
 */
	public $request = null;

/**
 * Response object used for asset/cached responses.
 *
 * @var CakeResponse
 */
	public $response = null;

/**
 * Constructor.
 */
	public function __construct($url = null, $base = false) {
		if ($base !== false) {
			Configure::write('App.base', $base);
		}
		if ($url !== null) {
			$this->dispatch($url);
		}
	}

/**
 * Dispatches and invokes given Request, handing over control to the involved controller. If the controller is set
 * to autoRender, via Controller::$autoRender, then Dispatcher will render the view.
 *
 * Actions in CakePHP can be any public method on a controller, that is not declared in Controller.  If you
 * want controller methods to be public and in-accesible by URL, then prefix them with a `_`.  
 * For example `public function _loadPosts() { }` would not be accessible via URL.  Private and protected methods
 * are also not accessible via URL.
 *
 * If no controller of given name can be found, invoke() will throw an exception.
 * If the controller is found, and the action is not found an exception will be thrown.
 *
 * @param CakeRequest $request Request object to dispatch.
 * @param array $additionalParams Settings array ("bare", "return") which is melded with the GET and POST params
 * @return boolean Success
 * @throws MissingControllerException, MissingActionException, PrivateActionException if any of those error states
 *    are encountered.
 */
	public function dispatch(CakeRequest $request, $additionalParams = array()) {
		$this->here = $request->here;

		if ($this->asset($request->url) || $this->cached($request->url)) {
			return;
		}

		$this->request = $this->parseParams($request, $additionalParams);
		$controller = $this->_getController($this->request);

		if (!is_object($controller)) {
			Router::setRequestInfo($request);
			throw new MissingControllerException(array(
				'controller' => Inflector::camelize($request->params['controller']) . 'Controller'
			));
		}

		Router::setRequestInfo($request);

		if ($this->_isPrivateAction($request)) {
			throw new PrivateActionException(array(
				'controller' => Inflector::camelize($request->params['controller']) . "Controller",
				'action' => $request->params['action']
			));
		}

		return $this->_invoke($controller, $request);
	}

/**
 * Check if the request's action is marked as private, with an underscore, of if the request is attempting to 
 * directly accessing a prefixed action.
 *
 * @param CakeRequest $request The request to check
 * @return boolean
 */
	protected function _isPrivateAction($request) {
		$privateAction = $request->params['action'][0] === '_';
		$prefixes = Router::prefixes();

		if (!empty($prefixes)) {
			if (isset($request->params['prefix'])) {
				$request->params['action'] = $request->params['prefix'] . '_' . $request->params['action'];
			} elseif (strpos($request->params['action'], '_') > 0) {
				list($prefix, $action) = explode('_', $request->params['action']);
				$privateAction = in_array($prefix, $prefixes);
			}
		}
		return $privateAction;
	}

/**
 * Initializes the components and models a controller will be using.
 * Triggers the controller action, and invokes the rendering if Controller::$autoRender is true and echo's the output.
 * Otherwise the return value of the controller action are returned.
 *
 * @param Controller $controller Controller to invoke
 * @param CakeRequest $request The request object to invoke the controller for.
 * @return string Output as sent by controller
 * @throws MissingActionException when the action being called is missing.
 */
	protected function _invoke(Controller $controller, CakeRequest $request) {
		$controller->constructClasses();
		$controller->startupProcess();

		$methods = array_flip($controller->methods);

		if (!isset($methods[$request->params['action']])) {
			if ($controller->scaffold !== false) {
				App::import('Controller', 'Scaffold', false);
				return new Scaffold($controller, $request);
			}
			throw new MissingActionException(array(
				'controller' => Inflector::camelize($request->params['controller']) . "Controller",
				'action' => $request->params['action']
			));
		}
		$result = call_user_func_array(array(&$controller, $request->params['action']), $request->params['pass']);
		$response = $controller->getResponse();

		if ($controller->autoRender) {
			$controller->render();
		} elseif ($response->body() === null) {
			$response->body($result);
		}
		$controller->shutdownProcess();

		if (isset($request->params['return'])) {
			return $response->body();
		}
		$response->send();
	}

/**
 * Applies Routing and additionalParameters to the request to be dispatched.
 * If Routes have not been loaded they will be loaded, and app/config/routes.php will be run.
 *
 * @param CakeRequest $request CakeRequest object to mine for parameter information.
 * @param array $additionalParams An array of additional parameters to set to the request.
 *   Useful when Object::requestAction() is involved
 * @return CakeRequest The request object with routing params set.
 */
	public function parseParams(CakeRequest $request, $additionalParams = array()) {
		if (count(Router::$routes) == 0) {
			$namedExpressions = Router::getNamedExpressions();
			extract($namedExpressions);
{'fraction_non_alphanumeric': 0.13545461303992268, 'fraction_numerical': 0.0034198200877258197, 'mean_word_length': 4.267032106499609, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 98, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '5751715', 'n_tokens_mistral': 4028, 'n_tokens_neox': 3671, 'n_words': 1239}