<commit_before><?php
/*
 * Created by <NAME> on 2011-03-06.
 * Copyright (c) 2011 <NAME>.
 * All rights reserved.
 */

namespace Inject\Web\Router;

use \Inject\Core\Engine;

use \Inject\RouterGenerator;

/**
 * 
 */
class GeneratorWrapper
{
	/**
	 * The application engine.
	 * 
	 * @var \Inject\Core\Engine
	 */
	protected $engine;
	
	/**
	 * The CodeGenerator instance used by this Generator.
	 * 
	 * @var \Inject\Web\Router\Generator\CodeGenerator
	 */
	protected $generator;
	
	/**
	 * The cached generated code, so we don't have to generate it anew for the
	 * cache file.
	 * 
	 * @var string
	 */
	protected $code;
	
	// ------------------------------------------------------------------------
	
	public function __construct(Engine $engine)
	{
		$this->engine        = $engine;
		$this->mapping_scope = new RouterGenerator\Scope(new RouterGenerator\Mapping());
		$this->generator     = new RouterGenerator\CodeGenerator();
		
		$this->generator->registerDestinationHandlers(array(
			'Inject\Web\Router\DestinationHandler\Controller',
			'Inject\Web\Router\DestinationHandler\Callback',
			'Inject\Web\Router\DestinationHandler\Engine',
			'Inject\Web\Router\DestinationHandler\Redirect'
			));
		
		$this->generator->setVariableNameContainer(new VariableNameContainer());
		
		$this->generator->setFailCode('return array(404, array(\'X-Cascade\' => \'pass\'), \'\');');
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Loads a routing config file.
	 * 
	 * @param  string  A path to the router configuration file
	 * @return void
	 */
	public function loadFile($file)
	{
		if( ! empty($this->code))
		{
			throw new \RuntimeException('Router has already been generated.');
		}
		
		$this->mapping_scope->loadFile($file);
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Generates the routing code.
	 * 
	 * @return string  The routing code
	 */
	public function generateCode()
	{
		if(empty($this->code))
		{
			$this->generator->compileDefinitions($this->mapping_scope->getDefinitions(), array('engine' => $this->engine));
			
			$code = '$controllers = '.var_export($this->engine->getAvailableControllers(), true).';
$router 	= '.$this->generator->generateRouterCode().';';
			
			$code .= "\n\n\$reverse = ".$this->generator->createReverseRouter();
			
			$this->code = $code."\n\nreturn array(\$router, \$reverse);";
		}<commit_msg>Web\Router: Adjusted because of a method name change in RouterGenerator<commit_after><?php
/*
 * Created by <NAME> on 2011-03-06.
 * Copyright (c) 2011 <NAME>.
 * All rights reserved.
 */

namespace Inject\Web\Router;

use \Inject\Core\Engine;

use \Inject\RouterGenerator;

/**
 * 
 */
class GeneratorWrapper
{
	/**
	 * The application engine.
	 * 
	 * @var \Inject\Core\Engine
	 */
	protected $engine;
	
	/**
	 * The CodeGenerator instance used by this Generator.
	 * 
	 * @var \Inject\Web\Router\Generator\CodeGenerator
	 */
	protected $generator;
	
	/**
	 * The cached generated code, so we don't have to generate it anew for the
	 * cache file.
	 * 
	 * @var string
	 */
	protected $code;
	
	// ------------------------------------------------------------------------
	
	public function __construct(Engine $engine)
	{
		$this->engine        = $engine;
		$this->mapping_scope = new RouterGenerator\Scope(new RouterGenerator\Mapping());
		$this->generator     = new RouterGenerator\CodeGenerator();
		
		$this->generator->registerDestinationHandlers(array(
			'Inject\Web\Router\DestinationHandler\Controller',
			'Inject\Web\Router\DestinationHandler\Callback',
			'Inject\Web\Router\DestinationHandler\Engine',
			'Inject\Web\Router\DestinationHandler\Redirect'
			));
		
		$this->generator->setVariableNameContainer(new VariableNameContainer());
		
		$this->generator->setFailCode('return array(404, array(\'X-Cascade\' => \'pass\'), \'\');');
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Loads a routing config file.
	 * 
	 * @param  string  A path to the router configuration file
	 * @return void
	 */
	public function loadFile($file)
	{
		if( ! empty($this->code))
		{
			throw new \RuntimeException('Router has already been generated.');
		}
		
		$this->mapping_scope->loadFile($file);
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Generates the routing code.
	 * 
	 * @return string  The routing code
	 */
	public function generateCode()
	{
		if(empty($this->code))
		{
			$this->generator->compileDefinitions($this->mapping_scope->getDefinitions(), array('engine' => $this->engine));
			
			$code = '$controllers = '.var_export($this->engine->getAvailableControllers(), true).';
$router 	= '.$this->generator->generateRouterCode().';';
			
			$code .= "\n\n\$reverse = ".$this->generator->generateReverseRouterCode();
			
			$this->code = $code."\n\nreturn array(\$router, \$reverse);";
		}
{'fraction_non_alphanumeric': 0.23660714285714285, 'fraction_numerical': 0.006087662337662338, 'mean_word_length': 2.3922918100481763, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5518341', 'n_tokens_mistral': 1590, 'n_tokens_neox': 1381, 'n_words': 291}