SimpleSAML_Configuration::loadFromFile PHP Method

loadFromFile() private static method

Load the given configuration file.
private static loadFromFile ( string $filename, boolean $required ) : SimpleSAML_Configuration
$filename string The full path of the configuration file.
$required boolean Whether the file is required.
return SimpleSAML_Configuration The configuration file. An exception will be thrown if the configuration file is missing.
    private static function loadFromFile($filename, $required)
    {
        assert('is_string($filename)');
        assert('is_bool($required)');
        if (array_key_exists($filename, self::$loadedConfigs)) {
            return self::$loadedConfigs[$filename];
        }
        if (file_exists($filename)) {
            $config = 'UNINITIALIZED';
            // the file initializes a variable named '$config'
            ob_start();
            if (interface_exists('Throwable')) {
                try {
                    require $filename;
                } catch (ParseError $e) {
                    self::$loadedConfigs[$filename] = self::loadFromArray(array(), '[ARRAY]', 'simplesaml');
                    throw new SimpleSAML\Error\ConfigurationError($e->getMessage(), $filename, array());
                }
            } else {
                require $filename;
            }
            $spurious_output = ob_get_length() > 0;
            ob_end_clean();
            // check that $config exists
            if (!isset($config)) {
                throw new \SimpleSAML\Error\ConfigurationError('$config is not defined in the configuration file.', $filename);
            }
            // check that $config is initialized to an array
            if (!is_array($config)) {
                throw new \SimpleSAML\Error\ConfigurationError('$config is not an array.', $filename);
            }
            // check that $config is not empty
            if (empty($config)) {
                throw new \SimpleSAML\Error\ConfigurationError('$config is empty.', $filename);
            }
        } elseif ($required) {
            // file does not exist, but is required
            throw new \SimpleSAML\Error\ConfigurationError('Missing configuration file', $filename);
        } else {
            // file does not exist, but is optional, so return an empty configuration object without saving it
            $cfg = new SimpleSAML_Configuration(array(), $filename);
            $cfg->filename = $filename;
            return $cfg;
        }
        $cfg = new SimpleSAML_Configuration($config, $filename);
        $cfg->filename = $filename;
        self::$loadedConfigs[$filename] = $cfg;
        if ($spurious_output) {
            SimpleSAML\Logger::warning("The configuration file '{$filename}' generates output. Please review your configuration.");
        }
        return $cfg;
    }