<commit_before><?php

require __DIR__ . '/Autoloader.php';
require __DIR__ . '/Bootstrapper/Exception.php';

class EngineBlock_Application_Bootstrapper
{
    const CONFIG_FILE_DEFAULT       = 'configs/application.ini';
    // @todo correct typo
    const CONFIG_FILE_ENVIORNMENT   = '/etc/surfconext/engineblock.ini';

    /**
     * @var EngineBlock_ApplicationSingleton
     */
    protected $_application;

    /**
     * @var bool
     */
    protected $_bootstrapped = false;

    /**
     * @param EngineBlock_ApplicationSingleton $application
     */
    public function __construct(EngineBlock_ApplicationSingleton $application)
    {
        $this->_application = $application;
    }

    /**
     * Bootstrap the application.
     *
     * Note that the order or bootstrapping is very important.
     *
     * @return EngineBlock_ApplicationSingleton Bootstrapped application singleton
     */
    public function bootstrap()
    {
        if ($this->_bootstrapped) {
            return $this;
        }

        $this->_bootstrapAutoLoading();

        $this->_setEnvironmentIdByEnvironment();

        $this->_bootstrapDiContainer();

        $this->_bootstrapConfiguration();

        $this->_setEnvironmentIdByDetection();

        $this->_bootstrapEnvironmentConfiguration();

        $this->_bootstrapPhpSettings();
        $this->_bootstrapErrorReporting();
        $this->_bootstrapLogging();
        $this->_bootstrapHttpCommunication();

        $this->_bootstrapLayout();
        $this->_bootstrapTranslations();

        $this->_bootstrapped = true;

        return $this;
    }

    protected function _bootstrapDiContainer() {
        if (ENGINEBLOCK_ENV == 'testing') {
            $this->_application->setDiContainer(new EngineBlock_Application_TestDiContainer());
        } else {
            $this->_application->setDiContainer(new EngineBlock_Application_DiContainer());
        }
    }

    protected function _bootstrapAutoLoading()
    {
        require_once ENGINEBLOCK_FOLDER_ROOT . "vendor/autoload.php";

        if (!function_exists('spl_autoload_register')) {
            throw new EngineBlock_Application_Bootstrapper_Exception(
                'SPL Autoload not available! Please use PHP > v5.1.2',
                EngineBlock_Exception::CODE_ALERT
            );
        }

        $autoLoader = new EngineBlock_Application_Autoloader();
        spl_autoload_register(array($autoLoader, 'load'));
    }

    protected function _bootstrapConfiguration()
    {
        $this->_application->setConfiguration(
            $this->_getConfigurationLoader(
                $this->_getAllConfigFiles()
            )
        );
    }

    /**
     * return a list of config files (default and environment overrides) that shoud be loaded
     * @return array
     */
    protected function _getAllConfigFiles()
    {
        return array(
            ENGINEBLOCK_FOLDER_APPLICATION . self::CONFIG_FILE_DEFAULT,
            self::CONFIG_FILE_ENVIORNMENT,
        );
    }

    /**
     * Merges content of given config files
     *
     * @param array $configFiles
     * @return string
     */
    protected function _mergeConfigFiles(array $configFiles)
    {
        $configFileContents = "";
        foreach($configFiles as $configFile) {
            $configFileContents .= file_get_contents($configFile);
        }
        return $configFileContents;
    }

    /**
     * Tries to parse config files, if this fails each file will be verified to provide more debug information
     *
     * @param array $configFiles
     * @return EngineBlock_Config_Ini
     */
    protected function _getConfigurationLoader(array $configFiles)
    {
        try {
            $config = new EngineBlock_Config_Ini($this->_mergeConfigFiles($configFiles));
        } catch (EngineBlock_Exception $ex) {
            $this->_verifyConfigFiles($configFiles);
        }

        return $config;
    }

    /**
     * Tries to parse config files, if this fails an exception will be thrown in EngineBlock_Config_Ini, this is useful
     * to determine which of the files contains an error
     *
     * @param array $configFiles<commit_msg>BACKLOG-923 Reverted some differences with original code #2<commit_after><?php

require __DIR__ . '/Autoloader.php';
require __DIR__ . '/Bootstrapper/Exception.php';

class EngineBlock_Application_Bootstrapper
{
    const CONFIG_FILE_DEFAULT       = 'configs/application.ini';
    // @todo correct typo
    const CONFIG_FILE_ENVIORNMENT   = '/etc/surfconext/engineblock.ini';

    /**
     * @var EngineBlock_ApplicationSingleton
     */
    protected $_application;

    /**
     * @var bool
     */
    protected $_bootstrapped = false;

    /**
     * @param EngineBlock_ApplicationSingleton $application
     */
    public function __construct(EngineBlock_ApplicationSingleton $application)
    {
        $this->_application = $application;
    }

    /**
     * Bootstrap the application.
     *
     * Note that the order or bootstrapping is very important.
     *
     * @return EngineBlock_ApplicationSingleton Bootstrapped application singleton
     */
    public function bootstrap()
    {
        if ($this->_bootstrapped) {
            return $this;
        }

        $this->_bootstrapAutoLoading();

        $this->_setEnvironmentIdByEnvironment();

        $this->_bootstrapDiContainer();

        $this->_bootstrapConfiguration();

        $this->_setEnvironmentIdByDetection();

        $this->_bootstrapEnvironmentConfiguration();

        $this->_bootstrapPhpSettings();
        $this->_bootstrapErrorReporting();
        $this->_bootstrapLogging();
        $this->_bootstrapHttpCommunication();

        $this->_bootstrapLayout();
        $this->_bootstrapTranslations();

        $this->_bootstrapped = true;

        return $this;
    }

    protected function _bootstrapDiContainer() {
        if (ENGINEBLOCK_ENV == 'testing') {
            $this->_application->setDiContainer(new EngineBlock_Application_TestDiContainer());
        } else {
            $this->_application->setDiContainer(new EngineBlock_Application_DiContainer());
        }
    }

    protected function _bootstrapAutoLoading()
    {
        require_once ENGINEBLOCK_FOLDER_ROOT . "vendor/autoload.php";

        if (!function_exists('spl_autoload_register')) {
            throw new EngineBlock_Application_Bootstrapper_Exception(
                'SPL Autoload not available! Please use PHP > v5.1.2',
                EngineBlock_Exception::CODE_ALERT
            );
        }

        $autoLoader = new EngineBlock_Application_Autoloader();
        spl_autoload_register(array($autoLoader, 'load'));
    }

    protected function _bootstrapConfiguration()
    {
        $this->_application->setConfiguration(
            $this->_getConfigurationLoader(
                $this->_getAllConfigFiles()
            )
        );
    }

    /**
     * return a list of config files (default and environment overrides) that shoud be loaded
     * @return array
     */
    protected function _getAllConfigFiles()
    {
        return array(
            ENGINEBLOCK_FOLDER_APPLICATION . self::CONFIG_FILE_DEFAULT,
            self::CONFIG_FILE_ENVIORNMENT,
        );
    }

    /**
     * Merges content of given config files
     *
     * @param array $configFiles
     * @return string
     */
    protected function _mergeConfigFiles(array $configFiles)
    {
        $configFileContents = "";
        foreach($configFiles as $configFile) {
            $configFileContents .= file_get_contents($configFile) .PHP_EOL;
        }
        return $configFileContents;
    }

    /**
     * Tries to parse config files, if this fails each file will be verified to provide more debug information
     *
     * @param array $configFiles
     * @return EngineBlock_Config_Ini
     */
    protected function _getConfigurationLoader(array $configFiles)
    {
        try {
            $config = new EngineBlock_Config_Ini($this->_mergeConfigFiles($configFiles));
        } catch (EngineBlock_Exception $ex) {
            $this->_verifyConfigFiles($configFiles);
        }

        return $config;
    }

    /**
     * Tries to parse config files, if this fails an exception will be thrown in EngineBlock_Config_Ini, this is useful
     * to determine which of the files contains an error
     *
     * @param array $configFiles
{'fraction_non_alphanumeric': 0.09534633329345615, 'fraction_numerical': 0.001196315348725924, 'mean_word_length': 2.4818825489379424, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, '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': '3513910', 'n_tokens_mistral': 2231, 'n_tokens_neox': 2160, 'n_words': 471}