private function loadConfig(LoaderInterface $loader)
 {
     $files = $this->configManager->getFiles();
     $config = [];
     // Load configs for all definitions.
     foreach ($this->configManager->getDefinitions() as $definition) {
         $type = $definition->getType();
         $rawConfig = $this->loadFilesByType($loader, $files, $type);
         $config[$type] = [];
         if (false === empty($rawConfig)) {
             try {
                 $config[$type] = $this->processor->processConfiguration($definition, $rawConfig);
             } catch (InvalidConfigurationException $ex) {
                 throw ConfigException::validationError($ex->getMessage());
             }
         }
     }
     // Check for configs without definitions.
     foreach ($this->configManager->getFiles() as $file) {
         if (false === $file->isLoaded()) {
             $name = $file->getType();
             // Merge the array if it exists already.
             if (true === isset($config[$name])) {
                 $config[$name] = array_merge_recursive($config[$name], $this->loadFile($loader, $file));
             } else {
                 $config[$name] = $this->loadFile($loader, $file);
             }
         }
     }
     return $config;
 }
 private function setupConfig(\Silex\Application $app, $configName)
 {
     $app[$configName . '.manager'] = $app->share(function ($app) {
         return new ConfigManager();
     });
     $app[$configName] = $app->share(function ($app) use($configName) {
         // Get the config paths and the file locator.
         $paths = $app[$configName . '.paths'];
         if (null === $paths || count($paths) === 0) {
             throw ConfigException::pathsNotSpecified();
         }
         $locator = new FileLocator($paths);
         // Dispatch the event so that other providers can add their
         // definitions.
         $this->dispatchInitEvent($app);
         // Get some app definitions.
         $cacheType = $app[$this->configName . '.cache.type'];
         $manager = $app[$configName . '.manager'];
         $loaderFactory = new LoaderFactory($locator, $manager->getLoaders());
         $cacheFactory = new CacheProducer($manager->getCacheFactories(), $app, $cacheType);
         $processor = new Processor();
         $compiler = new ConfigCompiler($manager, $loaderFactory, $processor, $cacheFactory);
         // Run the configuration compiler.
         $config = new Config($compiler->compile());
         $this->dispatchLoadedEvent($app, $config);
         return $config;
     });
 }
示例#3
0
 /**
  * {@inheritdoc}
  */
 public function offsetGet($offset)
 {
     if (false === $this->offsetExists($offset)) {
         throw ConfigException::configNotFound($offset);
     } else {
         return $this->config[$offset];
     }
 }
示例#4
0
 /**
  * Loads the given PHP file.
  * 
  * @param string $resource
  * @param string $type
  */
 public function load($resource, $type = null)
 {
     $path = $this->locator->locate($resource);
     if (!stream_is_local($path)) {
         throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $path));
     }
     if (!file_exists($path)) {
         throw new \InvalidArgumentException(sprintf('File "%s" not found.', $path));
     }
     $returnArray = (include $path);
     if (false === is_array($returnArray)) {
         throw ConfigException::configNotArray($path);
     }
     $this->loaded[$resource] = $path;
     return $returnArray;
 }
 /**
  * Produces a CacheInterface object.
  * 
  * @return \Affiniti\Config\Cache\CacheInterface
  * 
  * @throws \InvalidArgumentException
  * @throws \Affiniti\Config\Exception\CacheFactoryNotFound
  */
 public function produce()
 {
     $cacheFactory = null;
     foreach ($this->cacheFactories as $factory) {
         if (false === $factory instanceof CacheFactoryInterface) {
             throw new \InvalidArgumentException("Array of Cache Factories must contain valid CacheFactory objects.");
         }
         if ($this->type == $factory->getType()) {
             $cacheFactory = $factory;
             break;
         }
     }
     if (null === $cacheFactory) {
         throw ConfigException::cacheFactoryNotFound($this->type);
     }
     $cache = $cacheFactory->newInstance($this->app);
     if (false === $cache instanceof CacheInterface) {
         throw new \InvalidArgumentException("Cache factory must produce a valid CacheInterface object.");
     }
     return $cache;
 }