<commit_before><?php
# Copyright (C) 2015, 2018 <NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Site associative options handled by a specific database table (if you use it)
 */
class Options {

 	/**
	 * Cached options
	 * @var array
	 */
	private $cache = [];

	/**
	 * List of formally registered options.
	 * @var array
	*/
	private $optionsRegistered = [];

	/**
	 * Get the singleton instance
	 *
	 * @return self
	 */
	public static function instance() {
		static $me = false;
		if( ! $me ) {
			$me = new self();
		}
		return $me;
	}

	/**
	 * Load options with autoload in cache
	 */
	public function autoload() {
		static $todo = true;
		if( $todo ) {
			$options = Query::factory()
				->select( [
					'option_name',
					'option_value',
				] )
				->from( 'option' )
				->whereInt( 'option_autoload', 1 )
				->queryResults();

			foreach( $options as $option ) {
				$this->override( $option->option_name, $option->option_value );
			}
			$todo = false;
		}
	}

	/**
	 * Formally register an option and know if it's already registered
	 *
	 * @param string $name Option name
	 * @return bool Successfully or not
	 */
	public function register( $name ) {
		if( in_array( $name, $this->optionsRegistered, true) ) {
			DEBUG and error( sprintf(
				'error registering the option "%s" because it is already registered',
				$name
			) );
			return false;
		}
		$this->optionsRegistered[] = $name;
		return true;
	}

	/**
	 *	Get all formally registered options
	 *
	 *	@return array
	 */
	public function getRegistereds() {
		return $this->optionsRegistered;
	}

	/**
	 * Get the value of an option
	 *
	 * @param string $name Option name
	 * @param string $defalut Default option value
	 */
	public function get( $name, $default = '' ) {
		$value = '';
		$this->autoload();
		if( array_key_exists( $name, $this->cache ) ) {
			$value = $this->cache[ $name ];
		} else {
			$value = Query::factory()
				->from( 'option' )
				->whereStr( 'option_name', $name )
				->queryValue( 'option_value' );
<commit_msg>cosmetic change in Options class, minor DEBUG refactor<commit_after><?php
# Copyright (C) 2015, 2018, 2019 <NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Site associative options handled by a specific database table (if you use it)
 */
class Options {

 	/**
	 * Cached options
	 * @var array
	 */
	private $cache = [];

	/**
	 * List of formally registered options.
	 * @var array
	*/
	private $opts = [];

	/**
	 * Get the singleton instance
	 *
	 * @return self
	 */
	public static function instance() {
		static $me = false;
		if( ! $me ) {
			$me = new self();
		}
		return $me;
	}

	/**
	 * Load options with autoload in cache
	 */
	public function autoload() {
		static $todo = true;
		if( $todo ) {
			$options = Query::factory()
				->select( [
					'option_name',
					'option_value',
				] )
				->from( 'option' )
				->whereInt( 'option_autoload', 1 )
				->queryResults();

			foreach( $options as $option ) {
				$this->override( $option->option_name, $option->option_value );
			}
			$todo = false;
		}
	}

	/**
	 * Formally register an option and know if it's already registered
	 *
	 * @param string $name Option name
	 * @return bool Successfully or not
	 */
	public function register( $name ) {
		if( in_array( $name, $this->opts, true) ) {
			return error( "error registering the option $name because it is already registered" );
		}
		$this->opts[] = $name;
		return true;
	}

	/**
	 *	Get all formally registered options
	 *
	 *	@return array
	 */
	public function getRegistereds() {
		return $this->opts;
	}

	/**
	 * Get the value of an option
	 *
	 * @param string $name Option name
	 * @param string $defalut Default option value
	 */
	public function get( $name, $default = '' ) {
		$value = '';
		$this->autoload();
		if( array_key_exists( $name, $this->cache ) ) {
			$value = $this->cache[ $name ];
		} else {
			$value = Query::factory()
				->from( 'option' )
				->whereStr( 'option_name', $name )
				->queryValue( 'option_value' );

{'fraction_non_alphanumeric': 0.12911294977871848, 'fraction_numerical': 0.004618048874350587, 'mean_word_length': 3.070477682067345, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '718464', 'n_tokens_mistral': 1811, 'n_tokens_neox': 1570, 'n_words': 587}