<commit_before><?php
/**
 *
 * PHP 5
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the below copyright notice.
 *
 * @copyright     Copyright 2013, <NAME> (http://frankfoerster.com)
 * @link          http://github.com/frankfoerster/wasabi
 * @package       Wasabi
 * @subpackage    Wasabi.Plugin.Core.Routing.Route
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('CakeResponse', 'Network');
App::uses('CakeRoute', 'Routing/Route');
App::uses('ClassRegistry', 'Utility');

class WasabiRoute extends CakeRoute {

	/**
	 * CakeResponse object
	 *
	 * @var CakeResponse
	 */
	public $response = null;

	/**
	 * Flag for disabling exit() when this route parses a url.
	 *
	 * @var boolean
	 */
	public $stop = true;

	/**
	 * Reverse routing method to generate a url from given url array
	 *
	 * @param array $url
	 * @return boolean|mixed
	 */
	public function match($url) {
		if (empty($url)) {
			return false;
		}

		$identifier = md5(serialize($url));

		if ($result = Cache::read($identifier, 'routes')) {
			return $result;
		}

		$conditions = array(
			'Route.plugin' => ($url['plugin'] == null || $url['plugin'] == false) ? '' : $url['plugin'],
			'Route.controller' => $url['controller'],
			'Route.action' => $url['action'],
			'Route.status_code' => null
		);

		unset($url['plugin'], $url['controller'], $url['action']);

		$passed_params = array();
		$named_params = array();

		foreach ($url as $key => $value) {
			if (is_int($key)) {
				$passed_params[] = $value;
			}
			if (is_string($key)) {
				$named_params[] = $key . ':' . $value;
			}
		}

		$conditions['Route.params'] = implode('|', $passed_params);

		$route = ClassRegistry::init('Core.Route')->find('first', array(
			'conditions' => $conditions
		));

		if (!$route) {
			return false;
		}

		if (!empty($named_params)) {
			$route['Route']['url'] .= '/' . implode('/', $named_params);
		}

		Cache::write($identifier, $route['Route']['url'], 'routes');

		return $route['Route']['url'];
	}

	/**
	 * Parse a requested url and create routing parameters from the routes table
	 *
	 * @param string $url
	 * @return array|boolean|mixed
	 */
	public function parse($url) {
		if (!$url) {
			$url = '/';
		}

		$params = array(
			'pass' => array(),
			'named' => array(),
			'plugin' => null
		);

		// check for named params
		$url_parts = explode('/', $url);
		foreach ($url_parts as $key => $value) {<commit_msg>add core prefix to cache calls<commit_after><?php
/**
 *
 * PHP 5
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the below copyright notice.
 *
 * @copyright     Copyright 2013, <NAME> (http://frankfoerster.com)
 * @link          http://github.com/frankfoerster/wasabi
 * @package       Wasabi
 * @subpackage    Wasabi.Plugin.Core.Routing.Route
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('CakeResponse', 'Network');
App::uses('CakeRoute', 'Routing/Route');
App::uses('ClassRegistry', 'Utility');

class WasabiRoute extends CakeRoute {

	/**
	 * CakeResponse object
	 *
	 * @var CakeResponse
	 */
	public $response = null;

	/**
	 * Flag for disabling exit() when this route parses a url.
	 *
	 * @var boolean
	 */
	public $stop = true;

	/**
	 * Reverse routing method to generate a url from given url array
	 *
	 * @param array $url
	 * @return boolean|mixed
	 */
	public function match($url) {
		if (empty($url)) {
			return false;
		}

		$identifier = md5(serialize($url));

		if ($result = Cache::read($identifier, 'core.routes')) {
			return $result;
		}

		$conditions = array(
			'Route.plugin' => ($url['plugin'] == null || $url['plugin'] == false) ? '' : $url['plugin'],
			'Route.controller' => $url['controller'],
			'Route.action' => $url['action'],
			'Route.status_code' => null
		);

		unset($url['plugin'], $url['controller'], $url['action']);

		$passed_params = array();
		$named_params = array();

		foreach ($url as $key => $value) {
			if (is_int($key)) {
				$passed_params[] = $value;
			}
			if (is_string($key)) {
				$named_params[] = $key . ':' . $value;
			}
		}

		$conditions['Route.params'] = implode('|', $passed_params);

		$route = ClassRegistry::init('Core.Route')->find('first', array(
			'conditions' => $conditions
		));

		if (!$route) {
			return false;
		}

		if (!empty($named_params)) {
			$route['Route']['url'] .= '/' . implode('/', $named_params);
		}

		Cache::write($identifier, $route['Route']['url'], 'core.routes');

		return $route['Route']['url'];
	}

	/**
	 * Parse a requested url and create routing parameters from the routes table
	 *
	 * @param string $url
	 * @return array|boolean|mixed
	 */
	public function parse($url) {
		if (!$url) {
			$url = '/';
		}

		$params = array(
			'pass' => array(),
			'named' => array(),
			'plugin' => null
		);

		// check for named params
		$url_parts = explode('/', $url);
		foreach ($url_parts as $key => $value) {
{'fraction_non_alphanumeric': 0.19400281746830347, 'fraction_numerical': 0.002414972831555645, 'mean_word_length': 3.398230088495575, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4140048', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1603, 'n_words': 395}