<commit_before><?php

namespace Neptune\Assets;

use Neptune\Helpers\Html;
use Neptune\Core\Config;

/**
 * Assets
 * @author <NAME> <EMAIL>
 **/
class Assets {

	protected static $instance;
	protected $js = array();
	protected $css = array();

	public static function getInstance() {
		if(!self::$instance) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	protected function __construct() {
	}

	public function addJs($name, $src, $dependencies = array(), $options = array()) {
		$this->js[$name] = array('src' => str_replace('#', '/', $src),
								 'deps' => (array) $dependencies,
								 'opts' => (array) $options);
	}

	public function removeJs($name) {
		unset($this->js[$name]);
	}

	public static function js() {
		$content ='';
		$me = self::getInstance();
		foreach($me->sort($me->js) as $k => $v) {
			$content .= Html::js($v, $me->js[$k]['opts']);
		}
		return $content;
	}

	public function addCss($name, $src, $dependencies = array(), $options = array()) {
		$this->css[$name] = array('src' => str_replace('#', '/', $src),
								  'deps' => (array) $dependencies,
								  'opts' => (array) $options);
	}

	public function removeCss($name) {
		unset($this->css[$name]);
	}

	public static function css() {
		$content = '';
		$me = self::getInstance();
		foreach($me->sort($me->css) as $k => $v) {
			$content .= Html::css($v, $me->css[$k]['opts']);
		}
		return $content;
	}

	public function clear() {
		$this->js = array();
		$this->css = array();
	}

	protected function sort($assets) {
		$sorted = array();
		foreach($assets as $k => $v) {
			foreach($v['deps'] as $dep) {
				if($dep !== $k) {
					if(isset($assets[$dep])) {
						$this->addDeps($dep, $assets[$dep], $sorted, $assets);
					}
				}
			}
			if(strpos($v['src'], '://')) {
				$sorted[$k] = $v['src'];
			} else {
				$sorted[$k] = Config::load()->get('assets.url') . $v['src'];
			}
		}
		return $sorted;
	}

	protected function addDeps($key, $value, &$sorted, &$assets) {
		if(!empty($value['deps'])) {
			foreach($value['deps'] as $k => $dep) {
				if($dep !== $key) {
					if(isset($assets[$dep])) {
						$this->addDeps($dep, $assets[$dep], $sorted, $assets);
					}
				}
			}
		}
		$sorted[$key] = Config::load()->get('assets.url') . $value['src'];
		unset($assets[$key]);
	}
}<commit_msg>Bug fix for Assets not creating asset urls properly.<commit_after><?php

namespace Neptune\Assets;

use Neptune\Helpers\Html;
use Neptune\Core\Config;

/**
 * Assets
 * @author <NAME> <EMAIL>
 **/
class Assets {

	protected static $instance;
	protected $js = array();
	protected $css = array();

	public static function getInstance() {
		if(!self::$instance) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	protected function __construct() {
	}

	public function addJs($name, $src, $dependencies = array(), $options = array()) {
		$this->js[$name] = array('src' => str_replace('#', '/', $src),
								 'deps' => (array) $dependencies,
								 'opts' => (array) $options);
	}

	public function removeJs($name) {
		unset($this->js[$name]);
	}

	public static function js() {
		$content ='';
		$me = self::getInstance();
		foreach($me->sort($me->js) as $k => $v) {
			$content .= Html::js($v, $me->js[$k]['opts']);
		}
		return $content;
	}

	public function addCss($name, $src, $dependencies = array(), $options = array()) {
		$this->css[$name] = array('src' => str_replace('#', '/', $src),
								  'deps' => (array) $dependencies,
								  'opts' => (array) $options);
	}

	public function removeCss($name) {
		unset($this->css[$name]);
	}

	public static function css() {
		$content = '';
		$me = self::getInstance();
		foreach($me->sort($me->css) as $k => $v) {
			$content .= Html::css($v, $me->css[$k]['opts']);
		}
		return $content;
	}

	public function clear() {
		$this->js = array();
		$this->css = array();
	}

	protected function sort($assets) {
		$sorted = array();
		foreach($assets as $k => $v) {
			foreach($v['deps'] as $dep) {
				if($dep !== $k) {
					if(isset($assets[$dep])) {
						$this->addDeps($dep, $assets[$dep], $sorted, $assets);
					}
				}
			}
			$sorted[$k] = $this->createUrl($v['src']);
		}
		return $sorted;
	}

	protected function addDeps($key, $value, &$sorted, &$assets) {
		if(!empty($value['deps'])) {
			foreach($value['deps'] as $k => $dep) {
				if($dep !== $key) {
					if(isset($assets[$dep])) {
						$this->addDeps($dep, $assets[$dep], $sorted, $assets);
					}
				}
			}
		}
		$sorted[$key] = $this->createUrl($value['src']);
		unset($assets[$key]);
	}

	protected function createUrl($src) {
		if(strpos($src, '://')) {
			return $src;
		} else {
			return Config::load()->get('assets.url') . $src;
		}
	}

}
{'fraction_non_alphanumeric': 0.23668512995313165, 'fraction_numerical': 0.0, 'mean_word_length': 3.50143815915628, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 60, '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': '4912928', 'n_tokens_mistral': 1977, 'n_tokens_neox': 1702, 'n_words': 348}