Packages

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

Classes

  • NConfig
  • NIniAdapter
  • NNeonAdapter

Interfaces

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