Namespaces

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

Classes

  • Config
  • IniAdapter
  • NeonAdapter

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, 2011 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\Neon;
16: 
17: 
18: 
19: /**
20:  * Reading and writing INI files.
21:  *
22:  * @author     Ondrej Hubsch
23:  */
24: final class NeonAdapter implements IAdapter
25: {
26:     /** @var string  section inheriting separator (section < parent) */
27:     public static $sectionSeparator = ' < ';
28: 
29: 
30:     /**
31:      * Static class - cannot be instantiated.
32:      */
33:     final public function __construct()
34:     {
35:         throw new Nette\StaticClassException;
36:     }
37: 
38: 
39: 
40:     /**
41:      * Reads configuration from NEON file.
42:      * @param  string  file name
43:      * @return array
44:      * @throws Nette\InvalidStateException
45:      */
46:     public static function load($file)
47:     {
48:         if (!is_file($file) || !is_readable($file)) {
49:             throw new Nette\FileNotFoundException("File '$file' is missing or is not readable.");
50:         }
51: 
52:         $neon = Neon::decode(file_get_contents($file));
53: 
54:         $separator = trim(self::$sectionSeparator);
55:         $data = array();
56:         foreach ($neon as $secName => $secData) {
57:             if ($secData === NULL) { // empty section
58:                 $secData = array();
59:             }
60: 
61:             if (is_array($secData)) {
62:                 // process extends sections like [staging < production]
63:                 $parts = $separator ? explode($separator, $secName) : array($secName);
64:                 if (count($parts) > 1) {
65:                     $parent = trim($parts[1]);
66:                     if (!isset($data[$parent]) || !is_array($data[$parent])) {
67:                         throw new Nette\InvalidStateException("Missing parent section '$parent' in file '$file'.");
68:                     }
69:                     $secData = array_reverse(Nette\Utils\Arrays::mergeTree(array_reverse($secData, TRUE), array_reverse($data[$parent], TRUE)), TRUE);
70:                     $secName = trim($parts[0]);
71:                     if ($secName === '') {
72:                         throw new Nette\InvalidStateException("Invalid empty section name in file '$file'.");
73:                     }
74:                 }
75:             }
76: 
77:             $data[$secName] = $secData;
78:         }
79: 
80:         return $data;
81:     }
82: 
83: 
84: 
85:     /**
86:      * Write NEON file.
87:      * @param  mixed
88:      * @param  string  file
89:      * @return void
90:      */
91:     public static function save($config, $file)
92:     {
93:         if (!file_put_contents($file, "# generated by Nette\n\n" . Neon::encode($config, Neon::BLOCK))) {
94:             throw new Nette\IOException("Cannot write file '$file'.");
95:         }
96:     }
97: 
98: }
99: 
Nette Framework 2.0beta1 API API documentation generated by ApiGen 2.3.0