<commit_before><?php

namespace Mapbender\CoreBundle\Component;

use Mapbender\CoreBundle\Component\ApplicationInterface;
use Symfony\Component\HttpFoundation\Response;

class Application implements ApplicationInterface {
	protected $container;
	protected $configuration;
	protected $layer_factories;

	public function __construct($container, $configuration) {
		$this->container = $container;
		$this->configuration = $configuration;
		$this->layer_factories = array();
		
		// A title is required, otherwise it would be hard to select an
		// application in a list
		if(!$this->getTitle()) {
			throw new \Exception('No title set for application');
		}
	}

	public function getTitle() {
		return $this->configuration['title'];
	}

	public function getDescription() {
		return array_key_exists('description', $this->configuration) ? $this->configuration['description'] : NULL;
	}

	public function getTemplate() {
		$template_name = $this->configuration['template'];
		return $this->container->get($template_name);
	}
	
	public function getElements() {
		
	}
		
	public function getLayers() {
		if(!array_key_exists('layers', $this->configuration)) {
			return array();
		}

		$layers = array();
		foreach($this->configuration['layers'] as $name => $configuration) {
			$factory = $this->getLayerFactory($configuration['type']);
			if(!$factory) {
				continue;
			}

			$layers[$name] = $factory->create($name, $configuration);
		}
		print_r($layers);
		die();
		return $layers;
	}
	
	public function render(Response $response = NULL) {
		if($response == NULL) {
			$response = new Response();
		}

		$response->setContent($this->getTemplate()->render(array(
			'title' => $this->getTitle(),
			'layers' => $this->getLayers(),
		)));

		return $response;
	}

	/**
	 * Get the layer factory for the specified type and cache it
	 *
	 * @param string $type The layer class identifier to get the layer factory for
	 * @return LayerFactoryInterface The layer factory
	 */
	protected function getLayerFactory($type) {
		if(array_key_exists($type, $this->layer_factories)) {
			return $this->layer_factories[$type];
		} else {
			$factory = $this->container->get('mapbender.layer_factory.' . $type);
			if($factory) {
				$this->layer_factories[$type] = $factory;
				return $factory;
			}
		}
	}
}
<commit_msg>Modified Application class<commit_after><?php

namespace Mapbender\CoreBundle\Component;

use Mapbender\CoreBundle\Component\ApplicationInterface;
use Symfony\Component\HttpFoundation\Response;

class Application implements ApplicationInterface {
	protected $container;
	protected $configuration;
	protected $regions;
	protected $layersets;
	protected $template;

	public function __construct($container, $configuration) {
		$this->container = $container;
		$this->configuration = $configuration;
		
		// A title is required, otherwise it would be hard to select an
		// application in a list
		if(!$this->getTitle()) {
			throw new \Exception('No title set for application');
		}

		$this->loadTemplate();
		$this->loadElements();
		$this->loadLayers();
	}

	public function getTitle() {
		return $this->configuration['title'];
	}

	public function getDescription() {
		return array_key_exists('description', $this->configuration) ? $this->configuration['description'] : NULL;
	}

	public function getTemplate() {
		return $this->template;
	}
	
	public function getLayersets() {
		return $this->layersets;
	}
	
	public function render(Response $response = NULL) {
		if($response == NULL) {
			$response = new Response();
		}

		$layersets = array();
		foreach($this->layersets as $title => $layers) {
			$layersets[$title] = array();
			foreach($layers as $layer) {
				$layersets[$title][] = $layer->render();
			}
		}
	
		// Get all assets we need to include
		// First the template assets
		$template_metadata = $this->getTemplate()->getMetadata();
		$css = array_merge(array(), $template_metadata['css']);
		$js  = array_merge(array(), $template_metadata['js']);
		// Then merge in all element assets
		// We also grab the element confs here
		$elements_confs = array();
		foreach($this->regions as $region => $elements) {
			foreach($elements as $element) {
				$assets = $element->getAssets();
				$css = array_merge($css, $assets['css']);
				$js  = array_merge($js,  $assets['js']);
				$element_confs[$element->getId()] = $element->getConfiguration();
			}
		}	
	
		$configuration = array(
			'title' => $this->getTitle(),
			'layersets' => $layersets,
			'elements' => $element_confs
		);


		$response->setContent($this->getTemplate()->render(array(
			'title' => $this->getTitle(),
			'configuration' => "Mapbender = {}; Mapbender.configuration = " . json_encode($configuration),
			'assets' => array(
				'css' => $css,
				'js' => $js),
			'regions' => $this->regions
		)));
		
		return $response;
	}

	/**
	 * Get the layer factory for the specified type and cache it
	 *
	 * @param string $type The layer class identifier to get the layer factory for
	 * @return LayerFactoryInterface The layer factory
	 */
	protected function getLayerFactory($type) {
		if(array_key_exists($type, $this->layer_factories)) {
			return $this->layer_factories[$type];
		} else {
			//TODO: Search the services tagged mapbender.layer_factory for given type
			$factory = $this->container->get('mapbender.' . $type . '.layer_factory');
			if($factory) {
				$this->layer_factories[$type] = $factory;
				return $factory;
			}
		}
	}

	/**
	 * Load the template
	 */
	private function loadTemplate() {
		$templating = $this->container->get('templating');
		$this->template = new $this->configuration['template']($templating);
	}

	/**
	 * Using the configuration, load (instantiate) all elements defined for the application
	 */
	private function loadElements() {
		$template_metadata = $this->getTemplate()->getMetadata();
		$this->elements = array();
		$counter = 0;
		foreach($template_metadata['regions'] as $region) {
			// Only iterate over regions defined in the app
			if(!array_key_exists($region, $this->configuration['elements'])) {
				continue;
			}
			foreach($this->configuration['elements'][$region] as $element) {
				// Extract and unset class, so we can use the remains as configuration
				$class = $element['class'];
				unset($element['class']);
				$id = "element-" . $counter++;
				$this->regions[$region][] = new $class($id, $element);
			}
		}
	}

	/**
	 * Using the configuration, load (instantiate all layers defined for the application
	 */
	private function loadLayers() {
		$this->layersets = array();
		foreach($this->configuration['layersets'] as $name => $layers) {
			$this->layersets[$name] = array();
			foreach($layers as $title => $layer) {
				//Extract and unset class, so we can use the remains as configuration
				$class = $layer['class'];
				unset($layer['class']);
				$this->layersets[$name][] = new $class($title, $layer);
			}
		}
	}
}

{'fraction_non_alphanumeric': 0.15604587022789956, 'fraction_numerical': 0.00014515894904920887, 'mean_word_length': 4.196078431372549, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 87, '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': '2268738', 'n_tokens_mistral': 2314, 'n_tokens_neox': 2024, 'n_words': 510}