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

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