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

Classes

  • ConfigIniAdapter
  • ConfigNeonAdapter
  • ConfigPhpAdapter
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
 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:  * @package Nette\Config\Adapters
11:  */
12: 
13: 
14: 
15: /**
16:  * Reading and generating NEON files.
17:  *
18:  * @author     David Grudl
19:  * @package Nette\Config\Adapters
20:  */
21: class ConfigNeonAdapter extends Object implements IConfigAdapter
22: {
23:     /** @internal */
24:     const INHERITING_SEPARATOR = '<', // child < parent
25:         PREVENT_MERGING = '!';
26: 
27:     /**
28:      * Reads configuration from NEON file.
29:      * @param  string  file name
30:      * @return array
31:      */
32:     public function load($file)
33:     {
34:         return $this->process((array) Neon::decode(file_get_contents($file)));
35:     }
36: 
37: 
38:     private function process(array $arr)
39:     {
40:         $res = array();
41:         foreach ($arr as $key => $val) {
42:             if (substr($key, -1) === self::PREVENT_MERGING) {
43:                 if (!is_array($val) && $val !== NULL) {
44:                     throw new InvalidStateException("Replacing operator is available only for arrays, item '$key' is not array.");
45:                 }
46:                 $key = substr($key, 0, -1);
47:                 $val[ConfigHelpers::EXTENDS_KEY] = ConfigHelpers::OVERWRITE;
48: 
49:             } elseif (preg_match('#^(\S+)\s+' . self::INHERITING_SEPARATOR . '\s+(\S+)\z#', $key, $matches)) {
50:                 if (!is_array($val) && $val !== NULL) {
51:                     throw new InvalidStateException("Inheritance operator is available only for arrays, item '$key' is not array.");
52:                 }
53:                 list(, $key, $val[ConfigHelpers::EXTENDS_KEY]) = $matches;
54:                 if (isset($res[$key])) {
55:                     throw new InvalidStateException("Duplicated key '$key'.");
56:                 }
57:             }
58: 
59:             if (is_array($val)) {
60:                 $val = $this->process($val);
61:             } elseif ($val instanceof NeonEntity) {
62:                 $val = (object) array('value' => $val->value, 'attributes' => $this->process($val->attributes));
63:             }
64:             $res[$key] = $val;
65:         }
66:         return $res;
67:     }
68: 
69: 
70:     /**
71:      * Generates configuration in NEON format.
72:      * @return string
73:      */
74:     public function dump(array $data)
75:     {
76:         $tmp = array();
77:         foreach ($data as $name => $secData) {
78:             if ($parent = ConfigHelpers::takeParent($secData)) {
79:                 $name .= ' ' . self::INHERITING_SEPARATOR . ' ' . $parent;
80:             }
81:             $tmp[$name] = $secData;
82:         }
83:         return "# generated by Nette\n\n" . Neon::encode($tmp, Neon::BLOCK);
84:     }
85: 
86: }
87: 
Nette Framework 2.0.11 (for PHP 5.2, un-prefixed) API API documentation generated by ApiGen 2.8.0