Exemplo n.º 1
0
 /**
  * Instantiate the Config object.
  *
  * It accepts either an array with the configuration settings, or a
  * filename pointing to a PHP file it can include.
  *
  * @since 0.1.0
  * @since 0.1.6 Accepts a delimiter to parse configuration keys.
  *
  * @param array|string         $config    Array with settings or filename for the
  *                                        settings file.
  * @param Schema|null          $schema    Optional. Config that contains default
  *                                        values that can get overwritten.
  * @param Validator|null       $validator Optional. Validator class that does the
  *                                        actual validation.
  * @param string[]|string|null $delimiter A string or array of strings that are used as delimiters to parse
  *                                        configuration keys. Defaults to "\", "/" & ".".
  *
  * @throws InvalidConfigurationSourceException If the config source is not a string or array.
  * @throws FailedToInstantiateParentException  If the parent class could not be instantiated.
  * @throws FailedToLoadConfigException         If loading of the config source failed.
  * @throws FailedToResolveConfigException      If the config file could not be resolved.
  * @throws InvalidConfigException              If the config file is not valid.
  */
 public function __construct($config, Schema $schema = null, Validator $validator = null, $delimiter = null)
 {
     $this->schema = $schema;
     $this->validator = $validator;
     // Make sure $config is either a string or array.
     if (!(is_string($config) || is_array($config))) {
         throw new InvalidConfigurationSourceException(sprintf(_('Invalid configuration source: %1$s'), print_r($config, true)));
     }
     if (is_string($config)) {
         $config = Loader::load($config);
     }
     // Run the $config through the OptionsResolver.
     $config = $this->resolveOptions($config);
     // Instantiate the parent class.
     try {
         parent::__construct($config, $delimiter);
     } catch (Exception $exception) {
         throw new FailedToInstantiateParentException(sprintf(_('Could not instantiate the configuration through its parent. Reason: %1$s'), $exception->getMessage()));
     }
     // Finally, validate the resulting config.
     if (!$this->isValid()) {
         throw new InvalidConfigException(sprintf(_('ConfigInterface file is not valid: %1$s'), print_r($config, true)));
     }
 }
Exemplo n.º 2
0
 /**
  * Create a new ConfigInterface object by merging data from several files.
  *
  * If a comma-separated list of files is provided, they are loaded in sequence and later files override settings in
  * earlier files.
  *
  * @since 0.4.6
  *
  * @param string|array $_ List of files.
  *
  * @return ConfigInterface Instance of a ConfigInterface implementation.
  */
 public static function mergeFromFiles($_)
 {
     $files = array_reverse(func_get_args());
     $data = [];
     if (is_array($files[0])) {
         $files = array_reverse($files[0]);
     }
     while (count($files) > 0) {
         try {
             $file = array_pop($files);
             if (!is_readable($file)) {
                 continue;
             }
             $new_data = static::getFromCache($file, function ($file) {
                 return Loader::load($file);
             });
             if (null === $data) {
                 continue;
             }
             $data = array_replace_recursive($data, $new_data);
         } catch (Exception $exception) {
             // Fail silently and try next file.
         }
     }
     return static::createFromArray($data);
 }