Skip to content
Snippets Groups Projects
Configuration.php 48.8 KiB
Newer Older
declare(strict_types=1);

use Exception;
use ParseError;
use SimpleSAML\Assert\Assert;
Tim van Dijen's avatar
Tim van Dijen committed
use SimpleSAML\Assert\AssertionFailedException;
use SimpleSAML\Error;
use SimpleSAML\Utils;
use Symfony\Component\Filesystem\Filesystem;

use function array_key_exists;
use function array_keys;
use function dirname;
use function implode;
use function interface_exists;
use function in_array;
use function is_array;
use function is_bool;
use function is_int;
use function is_string;
use function ob_end_clean;
use function ob_get_length;
use function ob_start;
use function preg_match;
use function preg_replace;
use function rtrim;
use function substr;
use function var_export;
/**
 * Configuration of SimpleSAMLphp
 * @package SimpleSAMLphp
class Configuration implements Utils\ClearableState
    /**
     * The release version of this package
     */
    public const VERSION = 'master';

    /**
     * A default value which means that the given option is required.
    public const REQUIRED_OPTION = '___REQUIRED_OPTION___';

    /**
     * Associative array with mappings from instance-names to configuration objects.
     * @var array<string, \SimpleSAML\Configuration>
    private static array $instance = [];
     * Configuration directories.
     *
     * This associative array contains the mappings from configuration sets to
     * configuration directories.
     * @var array<string, string>
    private static array $configDirs = [];

    /**
     * Cache of loaded configuration files.
     *
     * The index in the array is the full path to the file.
    private static array $loadedConfigs = [];

    /**
     * The configuration array.
    private array $configuration;

    /**
     * The location which will be given when an error occurs.
    private string $location;

    /**
     * The file this configuration was loaded from.
    private ?string $filename = null;

    /**
     * Initializes a configuration from the given array.
     *
     * @param array $config The configuration array.
     * @param string $location The location which will be given when an error occurs.
     */
Tim van Dijen's avatar
Tim van Dijen committed
    public function __construct(array $config, string $location)
    {
        $this->configuration = $config;
        $this->location = $location;
    }

    /**
     * Load the given configuration file.
     *
     * @param string $filename The full path of the configuration file.
     * @param bool $required Whether the file is required.
     * @return \SimpleSAML\Configuration The configuration file. An exception will be thrown if the
     *                                   configuration file is missing.
     * @throws \Exception If the configuration file is invalid or missing.
    private static function loadFromFile(string $filename, bool $required): Configuration
    {
        if (array_key_exists($filename, self::$loadedConfigs)) {
            return self::$loadedConfigs[$filename];
        }

        $fileSystem = new Filesystem();
        if ($fileSystem->exists($filename)) {
            /** @psalm-var mixed $config */
            $config = 'UNINITIALIZED';

            // the file initializes a variable named '$config'
            if (interface_exists('Throwable', false)) {
                try {
                    require($filename);
                } catch (ParseError $e) {
                    self::$loadedConfigs[$filename] = self::loadFromArray([], '[ARRAY]', 'simplesaml');
                    throw new Error\ConfigurationError($e->getMessage(), $filename, []);
                }
            } else {
                require($filename);
            }

            // check that $config exists
            if (!isset($config)) {
                throw new 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 Error\ConfigurationError(
                    '$config is not an array.',
                    $filename
                );
            }

            // check that $config is not empty
            if (empty($config)) {
                throw new Error\ConfigurationError(
            }
        } elseif ($required) {
            // file does not exist, but is required
            throw new Error\ConfigurationError('Missing configuration file', $filename);
            // file does not exist, but is optional, so return an empty configuration object without saving it
            $cfg = new Configuration([], $filename);
        $cfg = new Configuration($config, $filename);
        $cfg->filename = $filename;

        self::$loadedConfigs[$filename] = $cfg;

                "The configuration file '$filename' generates output. Please review your configuration."
            );
        }

Loading
Loading full blame...