Namespaces

  • 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
  • PHP

Classes

  • Compiler
  • CompilerExtension
  • Configurator
  • Helpers
  • Loader

Interfaces

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