Packages

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • none

Classes

Interfaces

  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  * @package Nette\Config
  7:  */
  8: 
  9: 
 10: 
 11: /**
 12:  * Configuration file loader.
 13:  *
 14:  * @author     David Grudl
 15:  *
 16:  * @property-read array $dependencies
 17:  * @package Nette\Config
 18:  */
 19: class NConfigLoader extends NObject
 20: {
 21:     /** @internal */
 22:     const INCLUDES_KEY = 'includes';
 23: 
 24:     private $adapters = array(
 25:         'php' => 'NConfigPhpAdapter',
 26:         'ini' => 'NConfigIniAdapter',
 27:         'neon' => 'NConfigNeonAdapter',
 28:     );
 29: 
 30:     private $dependencies = array();
 31: 
 32: 
 33:     /**
 34:      * Reads configuration from file.
 35:      * @param  string  file name
 36:      * @param  string  optional section to load
 37:      * @return array
 38:      */
 39:     public function load($file, $section = NULL)
 40:     {
 41:         if (!is_file($file) || !is_readable($file)) {
 42:             throw new FileNotFoundException("File '$file' is missing or is not readable.");
 43:         }
 44:         $this->dependencies[] = $file = realpath($file);
 45:         $data = $this->getAdapter($file)->load($file);
 46: 
 47:         if ($section) {
 48:             if (isset($data[self::INCLUDES_KEY])) {
 49:                 throw new InvalidStateException("Section 'includes' must be placed under some top section in file '$file'.");
 50:             }
 51:             $data = $this->getSection($data, $section, $file);
 52:         }
 53: 
 54:         // include child files
 55:         $merged = array();
 56:         if (isset($data[self::INCLUDES_KEY])) {
 57:             NValidators::assert($data[self::INCLUDES_KEY], 'list', "section 'includes' in file '$file'");
 58:             foreach ($data[self::INCLUDES_KEY] as $include) {
 59:                 $merged = NConfigHelpers::merge($this->load(dirname($file) . '/' . $include), $merged);
 60:             }
 61:         }
 62:         unset($data[self::INCLUDES_KEY]);
 63: 
 64:         return NConfigHelpers::merge($data, $merged);
 65:     }
 66: 
 67: 
 68:     /**
 69:      * Save configuration to file.
 70:      * @param  array
 71:      * @param  string  file
 72:      * @return void
 73:      */
 74:     public function save($data, $file)
 75:     {
 76:         if (file_put_contents($file, $this->getAdapter($file)->dump($data)) === FALSE) {
 77:             throw new IOException("Cannot write file '$file'.");
 78:         }
 79:     }
 80: 
 81: 
 82:     /**
 83:      * Returns configuration files.
 84:      * @return array
 85:      */
 86:     public function getDependencies()
 87:     {
 88:         return array_unique($this->dependencies);
 89:     }
 90: 
 91: 
 92:     /**
 93:      * Registers adapter for given file extension.
 94:      * @param  string  file extension
 95:      * @param  string|IConfigAdapter
 96:      * @return self
 97:      */
 98:     public function addAdapter($extension, $adapter)
 99:     {
100:         $this->adapters[strtolower($extension)] = $adapter;
101:         return $this;
102:     }
103: 
104: 
105:     /** @return IConfigAdapter */
106:     private function getAdapter($file)
107:     {
108:         $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
109:         if (!isset($this->adapters[$extension])) {
110:             throw new InvalidArgumentException("Unknown file extension '$file'.");
111:         }
112:         return is_object($this->adapters[$extension]) ? $this->adapters[$extension] : new $this->adapters[$extension];
113:     }
114: 
115: 
116:     private function getSection(array $data, $key, $file)
117:     {
118:         NValidators::assertField($data, $key, 'array|null', "section '%' in file '$file'");
119:         $item = $data[$key];
120:         if ($parent = NConfigHelpers::takeParent($item)) {
121:             $item = NConfigHelpers::merge($item, $this->getSection($data, $parent, $file));
122:         }
123:         return $item;
124:     }
125: 
126: }
127: 
Nette Framework 2.0.18 (for PHP 5.2, prefixed) API documentation generated by ApiGen 2.8.0