<commit_before><?php
namespace packages\base\frontend;
use \packages\base\json;
use \packages\base\event;
use \packages\base\autoloader;
use \packages\base\translator;
use \packages\base\translator\InvalidLangCode;
use \packages\base\translator\LangAlreadyExists;
use \packages\base\translator\language;
class source{
	private $path;
	private $name;
	private $parent;
	private $autoload;
	private $assets = array();
	private $views = array();
	private $langs = array();
	private $family = array();
	private $events = array();
	public function setPath($path){
		if(is_dir($path)){
			$this->path = $path;
			return true;
		}
		return false;
	}
	public function getPath(){
		return $this->path;
	}
	public function loadConfigFile(){
		if(is_file("{$this->path}/theme.json") and is_readable("{$this->path}/theme.json")){
			$json = file_get_contents("{$this->path}/theme.json");
			if($theme = json\decode($json)){
				if(isset($theme['parent'])){
					$this->setParent($theme['parent']);
				}
				if(isset($theme['name'])){
					$this->setName($theme['name']);
				}
				if(isset($theme['assets'])){
					foreach($theme['assets'] as $asset){
						$this->addAsset($asset);
					}
				}
				//print_r($this);
				if(isset($theme['autoload'])){
					$this->setAutoload($theme['autoload']);
					$this->register_autoload();
				}
				if(isset($theme['views'])){
					foreach($theme['views'] as $view){
						$this->addView($view);
					}
				}
				if(isset($theme['languages'])){
					foreach($theme['languages'] as $lang => $file){
						$this->addLang($lang, $file);
					}
				}
				if(isset($theme['events'])){
					foreach($theme['events'] as $event){
						if(isset($event['name'], $event['listener'])){
							$this->addEvent($event['name'], $event['listener']);
						}else{
							throw new SourceConfigException("Event", $this->path);
						}
					}
				}
				return true;
			}else{
				throw new SourceConfigException("Json Parse", $this->path);
			}
		}
		return false;
	}
	public function setParent($parent){
		$this->parent = $parent;
	}
	public function getParent(){
		return $this->parent;
	}
	public function setName($name){
		$this->name = $name;
	}
	public function getName(){
		return $this->name;
	}
	public function addAsset($asset){
		switch($asset['type']){
			case('js'):
			case('css'):
			case('less'):
			case('sass'):
			case('ts'):
				$this->addCodeAsset($asset);
				break;
			case('package'):
				$this->addNodePackageAsset($asset);
				break;
			default:
				throw new SourceAssetException("Unkown asset type", $this->path);
		}
	}
	private function addCodeAsset(array $asset){
		$assetData = array(
			'type' => $asset['type']
		);
		if(isset($asset['name'])){
			$assetData['name'] = $asset['name'];
		}
		if(isset($asset['file'])){
			if(substr($asset['file'], 0, 13) == 'node_modules/' or is_file("{$this->path}/{$asset['file']}")){
				$assetData['file'] = $asset['file'];
			}else{
				throw new SourceAssetFileException($asset['file'], $this->path);
			}
		}elseif(isset($asset['inline'])){
			$assetData['inline'] = $asset['inline'];
		}else{
			throw new SourceAssetException("No file and no Code for asset",$this->path);
		}
		$this->assets[] = $assetData;
	}
	private function addNodePackageAsset(array $asset){
		if(!isset($asset['name'])){
			throw new SourceAssetException("No node package name",$this->path);
		}
		if(isset($asset['version'])){
			if(!preg_match("/^[\\d\\w\\.\\-]+$/", $asset['version'])){
				throw new SourceAssetException("invalid node package version",$this->path);
			}
		}
		$this->assets[] = $asset;
	}
	
	public function getAssets($type = null){
		$assets = array();
		foreach($this->assets as $asset){
			if($type === null or $asset['type'] == $type){
				$assets[] = $asset;
			}
		}
		return $assets;
	}
	public function hasFileAsset($file){
		if(is_file("{$this->path}/{$file}")){
			return true;
		}
		foreach($this->assets as $asset){
			if(isset($asset['file']) and $asset['file'] == $file){<commit_msg>allow version controller for package assets<commit_after><?php
namespace packages\base\frontend;
use \packages\base\json;
use \packages\base\event;
use \packages\base\autoloader;
use \packages\base\translator;
use \packages\base\translator\InvalidLangCode;
use \packages\base\translator\LangAlreadyExists;
use \packages\base\translator\language;
class source{
	private $path;
	private $name;
	private $parent;
	private $autoload;
	private $assets = array();
	private $views = array();
	private $langs = array();
	private $family = array();
	private $events = array();
	public function setPath($path){
		if(is_dir($path)){
			$this->path = $path;
			return true;
		}
		return false;
	}
	public function getPath(){
		return $this->path;
	}
	public function loadConfigFile(){
		if(is_file("{$this->path}/theme.json") and is_readable("{$this->path}/theme.json")){
			$json = file_get_contents("{$this->path}/theme.json");
			if($theme = json\decode($json)){
				if(isset($theme['parent'])){
					$this->setParent($theme['parent']);
				}
				if(isset($theme['name'])){
					$this->setName($theme['name']);
				}
				if(isset($theme['assets'])){
					foreach($theme['assets'] as $asset){
						$this->addAsset($asset);
					}
				}
				//print_r($this);
				if(isset($theme['autoload'])){
					$this->setAutoload($theme['autoload']);
					$this->register_autoload();
				}
				if(isset($theme['views'])){
					foreach($theme['views'] as $view){
						$this->addView($view);
					}
				}
				if(isset($theme['languages'])){
					foreach($theme['languages'] as $lang => $file){
						$this->addLang($lang, $file);
					}
				}
				if(isset($theme['events'])){
					foreach($theme['events'] as $event){
						if(isset($event['name'], $event['listener'])){
							$this->addEvent($event['name'], $event['listener']);
						}else{
							throw new SourceConfigException("Event", $this->path);
						}
					}
				}
				return true;
			}else{
				throw new SourceConfigException("Json Parse", $this->path);
			}
		}
		return false;
	}
	public function setParent($parent){
		$this->parent = $parent;
	}
	public function getParent(){
		return $this->parent;
	}
	public function setName($name){
		$this->name = $name;
	}
	public function getName(){
		return $this->name;
	}
	public function addAsset($asset){
		switch($asset['type']){
			case('js'):
			case('css'):
			case('less'):
			case('sass'):
			case('ts'):
				$this->addCodeAsset($asset);
				break;
			case('package'):
				$this->addNodePackageAsset($asset);
				break;
			default:
				throw new SourceAssetException("Unkown asset type", $this->path);
		}
	}
	private function addCodeAsset(array $asset){
		$assetData = array(
			'type' => $asset['type']
		);
		if(isset($asset['name'])){
			$assetData['name'] = $asset['name'];
		}
		if(isset($asset['file'])){
			if(substr($asset['file'], 0, 13) == 'node_modules/' or is_file("{$this->path}/{$asset['file']}")){
				$assetData['file'] = $asset['file'];
			}else{
				throw new SourceAssetFileException($asset['file'], $this->path);
			}
		}elseif(isset($asset['inline'])){
			$assetData['inline'] = $asset['inline'];
		}else{
			throw new SourceAssetException("No file and no Code for asset",$this->path);
		}
		$this->assets[] = $assetData;
	}
	private function addNodePackageAsset(array $asset){
		if(!isset($asset['name'])){
			throw new SourceAssetException("No node package name",$this->path);
		}
		if(isset($asset['version'])){
			if(!preg_match("/^[\^\>\=\~\<\*]*[\\d\\w\\.\\-]+$/", $asset['version'])){
				throw new SourceAssetException("invalid node package version",$this->path);
			}
		}
		$this->assets[] = $asset;
	}
	
	public function getAssets($type = null){
		$assets = array();
		foreach($this->assets as $asset){
			if($type === null or $asset['type'] == $type){
				$assets[] = $asset;
			}
		}
		return $assets;
	}
	public function hasFileAsset($file){
		if(is_file("{$this->path}/{$file}")){
			return true;
		}
		foreach($this->assets as $asset){
			if(isset($asset['file']) and $asset['file'] == $file){
{'fraction_non_alphanumeric': 0.21199250468457215, 'fraction_numerical': 0.0007495315427857589, 'mean_word_length': 4.0352201257861635, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 72, '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': '1876526', 'n_tokens_mistral': 3210, 'n_tokens_neox': 2684, 'n_words': 502}