Exemplo n.º 1
0
 /**
  * This method reads the configuration from a file.
  *
  * @param array $options - Options.
  *
  * @throws MissingOptionException
  * @throws InvalidOptionTypeException
  * @throws FileDoesNotExistException
  * @throws FileIsNotReadableException
  *
  * @return array
  */
 public function read(array $options)
 {
     if (!isset($options['file'])) {
         throw MissingOptionException::create('file');
     }
     if (!is_string($options['file'])) {
         throw InvalidOptionTypeException::create('file', 'string');
     }
     if (!is_file($options['file'])) {
         throw FileDoesNotExistException::create($options['file']);
     }
     if (!is_readable($options['file'])) {
         throw FileIsNotReadableException::create($options['file']);
     }
     $file = $this->_factory->createInstance($options['file'], null, $options);
     return $file->getContents();
 }
Exemplo n.º 2
0
 /**
  * Loads the configuration using the reader instance.
  *
  * @param array $options - Options.
  *
  * @throws InvalidOptionTypeException
  * @throws ImportException
  *
  * @return $this
  */
 public function load(array $options = [])
 {
     $options = array_replace_recursive($this->getOption('loadOptions', []), $options);
     // Simple shortcuts
     $options['readerOptions']['file'] = $options['readerOptions']['file'] ?? $options['file'];
     $options['readerOptions']['data'] = $options['readerOptions']['data'] ?? $options['data'];
     $reader = $this->getReader();
     $data = $reader->read($options['readerOptions']);
     if ($options['processImports'] && isset($data['import'])) {
         if (!is_array($data['import'])) {
             throw ImportException::create('"import" parameter must be an array.');
         }
         foreach ($data['import'] as $importData) {
             if (!is_array($importData)) {
                 throw ImportException::create('Each "import" array element must be an array.');
             }
             if (isset($importData['file'])) {
                 if ($options['file']) {
                     $importData['file'] = $importData['file'][0] === '/' ? $importData['file'] : dirname($options['file']) . '/' . $importData['file'];
                 }
                 if (!is_file($importData['file'])) {
                     continue;
                 }
             }
             $readOptions = array_replace_recursive($options['readerOptions'], $importData);
             $data = array_replace_recursive($data, $reader->read($readOptions));
         }
     }
     if ($options['clearFirst']) {
         $this->setData([], false);
     }
     if ($options['loadInKey'] !== null) {
         if (!is_string($options['loadInKey'])) {
             throw InvalidOptionTypeException::create('loadInKey', 'string');
         }
         $this->set($options['loadInKey'], $this->replaceTemplateVariables(array_replace_recursive($this->get($options['loadInKey'], []), $data)));
     } else {
         $this->setData(array_replace_recursive($this->getData(), $data));
     }
     /** @var Callable $callable */
     $callable = $this->getOption('onAfterLoad');
     if (!is_callable($callable)) {
         throw new \RuntimeException('Option "onAfterLoad" must be a callable.');
     }
     call_user_func_array($callable, [$this, $options]);
     return $this;
 }