PrivateBin\Configuration::__construct PHP Метод

__construct() публичный Метод

parse configuration file and ensure default configuration values are present
public __construct ( )
    public function __construct()
    {
        $config = array();
        $configFile = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini';
        if (is_readable($configFile)) {
            $config = parse_ini_file($configFile, true);
            foreach (array('main', 'model', 'model_options') as $section) {
                if (!array_key_exists($section, $config)) {
                    throw new Exception(I18n::_('PrivateBin requires configuration section [%s] to be present in configuration file.', $section), 2);
                }
            }
        }
        $opts = '_options';
        foreach (self::getDefaults() as $section => $values) {
            // fill missing sections with default values
            if (!array_key_exists($section, $config) || count($config[$section]) == 0) {
                $this->_configuration[$section] = $values;
                if (array_key_exists('dir', $this->_configuration[$section])) {
                    $this->_configuration[$section]['dir'] = PATH . $this->_configuration[$section]['dir'];
                }
                continue;
            } elseif ($section == 'model_options' && in_array($this->_configuration['model']['class'], array('Database', 'privatebin_db', 'zerobin_db'))) {
                $values = array('dsn' => 'sqlite:' . PATH . 'data' . DIRECTORY_SEPARATOR . 'db.sq3', 'tbl' => null, 'usr' => null, 'pwd' => null, 'opt' => array(PDO::ATTR_PERSISTENT => true));
            }
            // "*_options" sections don't require all defaults to be set
            if ($section !== 'model_options' && ($from = strlen($section) - strlen($opts)) >= 0 && strpos($section, $opts, $from) !== false) {
                if (is_int(current($values))) {
                    $config[$section] = array_map('intval', $config[$section]);
                }
                $this->_configuration[$section] = $config[$section];
            } else {
                foreach ($values as $key => $val) {
                    if ($key == 'dir') {
                        $val = PATH . $val;
                    }
                    $result = $val;
                    if (array_key_exists($key, $config[$section])) {
                        if ($val === null) {
                            $result = $config[$section][$key];
                        } elseif (is_bool($val)) {
                            $val = strtolower($config[$section][$key]);
                            if (in_array($val, array('true', 'yes', 'on'))) {
                                $result = true;
                            } elseif (in_array($val, array('false', 'no', 'off'))) {
                                $result = false;
                            } else {
                                $result = (bool) $config[$section][$key];
                            }
                        } elseif (is_int($val)) {
                            $result = (int) $config[$section][$key];
                        } elseif (is_string($val) && !empty($config[$section][$key])) {
                            $result = (string) $config[$section][$key];
                        }
                    }
                    $this->_configuration[$section][$key] = $result;
                }
            }
        }
        // support for old config file format, before the fork was renamed and PSR-4 introduced
        $this->_configuration['model']['class'] = str_replace('zerobin_', 'privatebin_', $this->_configuration['model']['class']);
        $this->_configuration['model']['class'] = str_replace(array('privatebin_data', 'privatebin_db'), array('Filesystem', 'Database'), $this->_configuration['model']['class']);
        // ensure a valid expire default key is set
        if (!array_key_exists($this->_configuration['expire']['default'], $this->_configuration['expire_options'])) {
            $this->_configuration['expire']['default'] = key($this->_configuration['expire_options']);
        }
    }