<commit_before><?php
namespace models;

class Module extends \Core\Model
{
	const TYPE_TEMPLATE = 'templates';
	const TYPE_OBSERVER = 'observers';
	const TYPE_ROUTE = 'routes';

	protected $guarded = array();
	protected $primaryKey = '_id';

	public function app() {
		return $this->belongsTo('models\App');
	}

	/**
	 * Get a route module instance
	 * @param string name
	 */
	public static function route($name) {
		return static::get(self::TYPE_ROUTE, $name.'.php');
	}

	/**
	 * Get a observer module instance
	 * @param string name
	 */
	public static function observer($name) {
		return static::get(self::TYPE_OBSERVER, $name.'.php');
	}

	/**
	 * Get a template module instance, trying to fallback to a general template
	 * @param string name
	 */
	public static function template($name) {
		$template = null;

		//
		// It's a template name or template contents?
		//
		if (preg_match('/^[A-Za-z0-9_\.\-\/]+\.html$/', $name)) {
			// It's a template name!
			$template = static::get(self::TYPE_TEMPLATE, $name);

			if (!$template) {
				$fallback_template_path = __DIR__ . '/../storage/default/templates/' . $name;
				// try to retrieve local fallback template
				if (!file_exists($fallback_template_path)) {
					throw new \MethodFailureException("Template not found: '{$name}'. Please run `dl-api generate:template {$name}` to generate one.");
				}

				// use local template if can't find
				$template = new static(array(
					'type' => self::TYPE_TEMPLATE,
					'code' => file_get_contents($fallback_template_path),
					'name' => $name
				));
			}
		}

		if (!$template) {
			$template = new static(array(
				'type' => self::TYPE_TEMPLATE,
				'code' => $name,
				'name' => 'inline-template.html'
			));
		}

		return $template;
	}

	/**
	 * Get a module instance
	 * @param string type
	 * @param string name
	 */
	public static function get($type, $name) {
		return static::currentApp()->where('type', $type)->where('name', $name)->first();
	}

	/**
	 * Compile module code
	 * @return mixed
	 */
	public function compile($options=array()) {
		$extension = '.' . pathinfo($this->name, PATHINFO_EXTENSION);
		$name = basename($this->name, $extension);

		if ($extension === ".php") {
			//
			// Expose handy aliases for modules
			//
			$aliases = "use models\App as App;";
			$aliases.= "use models\AuthToken as AuthToken;";
			$aliases.= "use models\Collection as Collection;\n";<commit_msg>fix evaluated code to respect error line numbers<commit_after><?php
namespace models;

class Module extends \Core\Model
{
	const TYPE_TEMPLATE = 'templates';
	const TYPE_OBSERVER = 'observers';
	const TYPE_ROUTE = 'routes';

	protected $guarded = array();
	protected $primaryKey = '_id';

	public function app() {
		return $this->belongsTo('models\App');
	}

	/**
	 * Get a route module instance
	 * @param string name
	 */
	public static function route($name) {
		return static::get(self::TYPE_ROUTE, $name.'.php');
	}

	/**
	 * Get a observer module instance
	 * @param string name
	 */
	public static function observer($name) {
		return static::get(self::TYPE_OBSERVER, $name.'.php');
	}

	/**
	 * Get a template module instance, trying to fallback to a general template
	 * @param string name
	 */
	public static function template($name) {
		$template = null;

		//
		// It's a template name or template contents?
		//
		if (preg_match('/^[A-Za-z0-9_\.\-\/]+\.html$/', $name)) {
			// It's a template name!
			$template = static::get(self::TYPE_TEMPLATE, $name);

			if (!$template) {
				$fallback_template_path = __DIR__ . '/../storage/default/templates/' . $name;
				// try to retrieve local fallback template
				if (!file_exists($fallback_template_path)) {
					throw new \MethodFailureException("Template not found: '{$name}'. Please run `dl-api generate:template {$name}` to generate one.");
				}

				// use local template if can't find
				$template = new static(array(
					'type' => self::TYPE_TEMPLATE,
					'code' => file_get_contents($fallback_template_path),
					'name' => $name
				));
			}
		}

		if (!$template) {
			$template = new static(array(
				'type' => self::TYPE_TEMPLATE,
				'code' => $name,
				'name' => 'inline-template.html'
			));
		}

		return $template;
	}

	/**
	 * Get a module instance
	 * @param string type
	 * @param string name
	 */
	public static function get($type, $name) {
		return static::currentApp()->where('type', $type)->where('name', $name)->first();
	}

	/**
	 * Compile module code
	 * @return mixed
	 */
	public function compile($options=array()) {
		$extension = '.' . pathinfo($this->name, PATHINFO_EXTENSION);
		$name = basename($this->name, $extension);

		if ($extension === ".php") {
			//
			// Expose handy aliases for modules
			//
			$aliases = "use models\App as App;";
			$aliases.= "use models\AuthToken as AuthToken;";
			$aliases.= "use models\Collection as Collection;";
{'fraction_non_alphanumeric': 0.16505652620760533, 'fraction_numerical': 0.0008221993833504625, 'mean_word_length': 3.3485254691689006, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, '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': '4899593', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1554, 'n_words': 420}