Inheritance: implements Kraken\Config\ConfigInterface
Example #1
0
 /**
  * @param FilesystemInterface $fs
  * @param string[] $masks
  * @param ParserInterface[] $parsers
  * @param bool $recursive
  */
 public function __construct(FilesystemInterface $fs, $masks = [], $parsers = [], $recursive = false)
 {
     parent::__construct();
     $factory = $this;
     $factory->bindParam('fs', $fs);
     $factory->bindParam('masks', $masks);
     $factory->bindParam('parsers', $parsers);
     $factory->bindParam('recursive', $recursive);
     $factory->define(function (callable $overwriteHandler = null) {
         $fs = $this->getParam('fs');
         $masks = $this->getParam('masks');
         $parsers = $this->getParam('parsers');
         $recursive = $this->getParam('recursive');
         $filters = $masks;
         $filters[] = '#(' . $this->getPatternForExt() . ')$#si';
         $files = $fs->getFiles('', $recursive, $filters);
         $data = [];
         $config = new Config($data, $overwriteHandler);
         foreach ($files as $file) {
             $ext = $file['extension'];
             $data = [];
             if ($ext === 'php') {
                 $data = $fs->req($file['path']);
             } else {
                 if (isset($parsers[$ext])) {
                     $contents = $fs->read($file['path']);
                     $data = $parsers[$ext]->decode($contents);
                 }
             }
             $config->merge($data);
         }
         return $config;
     });
 }
Example #2
0
 /**
  * @param ContainerInterface $container
  */
 protected function register(ContainerInterface $container)
 {
     $core = $container->make('Kraken\\Core\\CoreInterface');
     $context = $container->make('Kraken\\Runtime\\RuntimeContextInterface');
     $env = $container->make('Kraken\\Environment\\EnvironmentInterface');
     $this->core = $core;
     $this->context = $context;
     $dir = $this->getDir($context->getName(), $context->getType());
     $name = $context->getName();
     $prefix = $core->getDataPath() . '/config';
     $paths = [$prefix . '/' . $dir . '/' . $name, $prefix . '/Runtime/' . $name, $prefix . '/' . $dir, $prefix . '/Runtime'];
     $path = '';
     $pathFound = false;
     foreach ($paths as $path) {
         if (is_dir($path)) {
             $path .= '/config\\.([a-zA-Z]*?)$';
             $pathFound = true;
             break;
         }
     }
     if (!$pathFound) {
         throw new ReadException('There is no valid configuration file.');
     }
     $data = $core->config();
     $data['imports'] = [];
     $data['imports'][] = ['resource' => $path, 'mode' => 'merge'];
     $config = new Config($data);
     $config->setOverwriteHandler(new OverwriteReverseMerger());
     $this->configure($config);
     $config->setOverwriteHandler(new OverwriteMerger());
     $vars = array_merge($config->exists('vars') ? $config->get('vars') : [], $this->getDefaultVariables());
     $records = ArraySupport::flatten($config->getAll());
     foreach ($records as $key => $value) {
         $new = $value;
         $new = preg_replace_callback('#%env(\\.([a-zA-Z0-9_-]*?))+%#si', function ($matches) use($env) {
             $key = strtoupper(str_replace(['%', 'env.'], ['', ''], $matches[0]));
             return $env->getEnv($key);
         }, $new);
         $new = preg_replace_callback('#%func\\.([a-zA-Z0-9_-]*?)%#si', function ($matches) use($context) {
             return call_user_func([$context, $matches[1]]);
         }, $new);
         $new = StringSupport::parametrize($new, $vars);
         if (is_string($value) && $new != $value) {
             if (ctype_digit($new)) {
                 $new = (int) $new;
             }
             $config->set($key, $new);
         }
     }
     $container->instance('Kraken\\Config\\ConfigInterface', $config);
 }