<commit_before><?php
/**
 * @title            Config Class
 * @desc             Loading and management config files.
 *
 * @author           <NAME> <<EMAIL>>
 * @copyright        (c) 2011-2017, <NAME>. All Rights Reserved.
 * @license          GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
 * @package          PH7 / Framework / Config
 * @version          1.2
 */

namespace PH7\Framework\Config;

defined('PH7') or exit('Restricted access');

use PH7\Framework\Error\CException\PH7InvalidArgumentException;
use PH7\Framework\File\File;
use PH7\Framework\Pattern\Singleton;

/**
 * @class Singleton Class
 */
class Config implements Configurable
{
    const DEVELOPMENT_MODE = 'development';
    const PRODUCTION_MODE = 'production';

    /**
     * @var array $values;
     */
    public $values = array();

   /**
    * @var string $_sConfigAppFilePath
    */
    private $_sConfigAppFilePath;

    /**
     * @var string $_sConfigAppFilePath
     */
    private $_sConfigSysFilePath;

    /**
     * Import the Singleton trait.
     */
    use Singleton;

    /**
     * Set to private so nobody can create a new instance using new.
     */
    private function __construct()
    {
        $this->_sConfigAppFilePath = PH7_PATH_APP_CONFIG . PH7_CONFIG_FILE;
        $this->_sConfigSysFilePath = PH7_PATH_SYS . PH7_CONFIG . PH7_CONFIG_FILE;

        $this->_read();
    }

    /**
     * Load ini file.
     *
     * @param string $sFile
     *
     * @return boolean Returne FALSE if the file doesn't exist, TRUE otherwise.
     */
    public function load($sFile)
    {
        if (!is_file($sFile)) return false;

        $aContents = parse_ini_file($sFile, true);
        $this->values = array_merge($this->values, $aContents);
        return true;
    }

    /**
     * Get a config option by key.
     *
     * @param string $sKey The configuration setting key.
     *
     * @return string
     */
    public function getValue($sKey)
    {
        return $this->values[$sKey];
    }

    /**
     * Set dynamically a value to config data.
     *
     * @param string $sKey A unique config key.
     * @param string $sValue The value to add.
     *
     * @return void
     *
     * @throws PH7InvalidArgumentException
     */
    public function setValue($sKey, $sValue)
    {
        if (!array_key_exists($sKey, $this->values)) {
            $this->values[$sKey] = $sValue;
        } else {
            throw new PH7InvalidArgumentException(sprintf('%s already exists. You cannot reassign a config key.', $sKey));
        }
    }

    /**
     * Set Production Mode site.
     *
     * @return void
     */
    public function setProductionMode()
    {
        $this->_setMode(self::PRODUCTION_MODE);
    }

    /**
     * Set Development Mode site.
     *
     * @return void
     */
    public function setDevelopmentMode()
    {
        $this->_setMode(self::DEVELOPMENT_MODE);
    }

    /**
     * Set a Mode (Generic method).
     *
     * @param string $sReplace The Mode site.
     * @see PH7\Framework\Config\Config::setProductionMode()
     * @see PH7\Framework\Config\Config::setDevelopmentMode()
     * @return void
     */
    private function _setMode($sReplace)
    {
        $sSearch = ($sReplace === self::DEVELOPMENT_MODE) ? self::PRODUCTION_MODE : self::DEVELOPMENT_MODE;

        $oFile = new \PH7\Framework\File\File;

        // Check and correct the file permission if necessary.
        $oFile->chmod($this->_sConfigAppFilePath, 0666);

        $sContents = $oFile->getFile($this->_sConfigAppFilePath);
        $sNewContents = str_replace('environment = ' . $sSearch .  ' ; production or development', 'environment = ' . $sReplace . ' ; production or development', $sContents);
        $oFile->putFile($this->_sConfigAppFilePath, $sNewContents);

        // Check and correct the file permission if necessary.
        $oFile->chmod($this->_sConfigAppFilePath, 0644);

        unset($oFile, $sContents);
    }

    /**
     * Read Config File.
     *
     * @return void
     */
    private function _read()<commit_msg>Make code nicer & shorter<commit_after><?php
/**
 * @title            Config Class
 * @desc             Loading and management config files.
 *
 * @author           <NAME> <<EMAIL>>
 * @copyright        (c) 2011-2017, <NAME>. All Rights Reserved.
 * @license          GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
 * @package          PH7 / Framework / Config
 * @version          1.2
 */

namespace PH7\Framework\Config;

defined('PH7') or exit('Restricted access');

use PH7\Framework\Error\CException\PH7InvalidArgumentException;
use PH7\Framework\File\File;
use PH7\Framework\Pattern\Singleton;

/**
 * @class Singleton Class
 */
class Config implements Configurable
{
    const DEVELOPMENT_MODE = 'development';
    const PRODUCTION_MODE = 'production';

    /**
     * @var array $values;
     */
    public $values = array();

   /**
    * @var string $_sConfigAppFilePath
    */
    private $_sConfigAppFilePath;

    /**
     * @var string $_sConfigAppFilePath
     */
    private $_sConfigSysFilePath;

    /**
     * Import the Singleton trait.
     */
    use Singleton;

    /**
     * Set to private so nobody can create a new instance using new.
     */
    private function __construct()
    {
        $this->_sConfigAppFilePath = PH7_PATH_APP_CONFIG . PH7_CONFIG_FILE;
        $this->_sConfigSysFilePath = PH7_PATH_SYS . PH7_CONFIG . PH7_CONFIG_FILE;

        $this->_read();
    }

    /**
     * Load ini file.
     *
     * @param string $sFile
     *
     * @return boolean Returne FALSE if the file doesn't exist, TRUE otherwise.
     */
    public function load($sFile)
    {
        if (!is_file($sFile)) return false;

        $aContents = parse_ini_file($sFile, true);
        $this->values = array_merge($this->values, $aContents);
        return true;
    }

    /**
     * Get a config option by key.
     *
     * @param string $sKey The configuration setting key.
     *
     * @return string
     */
    public function getValue($sKey)
    {
        return $this->values[$sKey];
    }

    /**
     * Set dynamically a value to config data.
     *
     * @param string $sKey A unique config key.
     * @param string $sValue The value to add.
     *
     * @return void
     *
     * @throws PH7InvalidArgumentException
     */
    public function setValue($sKey, $sValue)
    {
        if (!array_key_exists($sKey, $this->values)) {
            $this->values[$sKey] = $sValue;
        } else {
            throw new PH7InvalidArgumentException(sprintf('%s already exists. You cannot reassign a config key.', $sKey));
        }
    }

    /**
     * Set Production Mode site.
     *
     * @return void
     */
    public function setProductionMode()
    {
        $this->_setMode(self::PRODUCTION_MODE);
    }

    /**
     * Set Development Mode site.
     *
     * @return void
     */
    public function setDevelopmentMode()
    {
        $this->_setMode(self::DEVELOPMENT_MODE);
    }

    /**
     * Set a Mode (Generic method).
     *
     * @param string $sReplace The Mode site.
     * @see PH7\Framework\Config\Config::setProductionMode()
     * @see PH7\Framework\Config\Config::setDevelopmentMode()
     *
     * @return void
     */
    private function _setMode($sReplace)
    {
        $sSearch = ($sReplace === self::DEVELOPMENT_MODE) ? self::PRODUCTION_MODE : self::DEVELOPMENT_MODE;

        $oFile = new File;

        // Check and correct the file permission if necessary.
        $oFile->chmod($this->_sConfigAppFilePath, 0666);

        $sFileContents = $oFile->getFile($this->_sConfigAppFilePath);
        $sSearchContents = 'environment = ' . $sSearch .  ' ; production or development';
        $sReplaceContents = 'environment = ' . $sReplace . ' ; production or development';
        $sNewContents = str_replace($sSearchContents, $sReplaceContents, $sFileContents);
        $oFile->putFile($this->_sConfigAppFilePath, $sNewContents);

        // Check and correct the file permission if necessary.
        $oFile->chmod($this->_sConfigAppFilePath, 0644);

        unset($oFile, $sFileContents);
    }

    /**
     * Read Config File.
     *
     * @return void
     */
    private function _read()
{'fraction_non_alphanumeric': 0.1213726670680313, 'fraction_numerical': 0.008789885611077665, 'mean_word_length': 2.271366679795195, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2129972', 'n_tokens_mistral': 2540, 'n_tokens_neox': 2430, 'n_words': 612}