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
 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: 
39:     private function process(array $arr)
40:     {
41:         $res = array();
42:         foreach ($arr as $key => $val) {
43:             if (substr($key, -1) === self::PREVENT_MERGING) {
44:                 if (!is_array($val) && $val !== NULL) {
45:                     throw new InvalidStateException("Replacing operator is available only for arrays, item '$key' is not array.");
46:                 }
47:                 $key = substr($key, 0, -1);
48:                 $val[ConfigHelpers::EXTENDS_KEY] = ConfigHelpers::OVERWRITE;
49: 
50:             } elseif (preg_match('#^(\S+)\s+' . self::INHERITING_SEPARATOR . '\s+(\S+)$#', $key, $matches)) {
51:                 if (!is_array($val) && $val !== NULL) {
52:                     throw new InvalidStateException("Inheritance operator is available only for arrays, item '$key' is not array.");
53:                 }
54:                 list(, $key, $val[ConfigHelpers::EXTENDS_KEY]) = $matches;
55:                 if (isset($res[$key])) {
56:                     throw new InvalidStateException("Duplicated key '$key'.");
57:                 }
58:             }
59: 
60:             if (is_array($val)) {
61:                 $val = $this->process($val);
62:             } elseif ($val instanceof NeonEntity) {
63:                 $val = (object) array('value' => $val->value, 'attributes' => $this->process($val->attributes));
64:             }
65:             $res[$key] = $val;
66:         }
67:         return $res;
68:     }
69: 
70: 
71: 
72:     /**
73:      * Generates configuration in NEON format.
74:      * @param  array
75:      * @return string
76:      */
77:     public function dump(array $data)
78:     {
79:         $tmp = array();
80:         foreach ($data as $name => $secData) {
81:             if ($parent = ConfigHelpers::takeParent($secData)) {
82:                 $name .= ' ' . self::INHERITING_SEPARATOR . ' ' . $parent;
83:             }
84:             $tmp[$name] = $secData;
85:         }
86:         return "# generated by Nette\n\n" . Neon::encode($tmp, Neon::BLOCK);
87:     }
88: 
89: }
90: 
Nette Framework 2.0.1 (for PHP 5.2, un-prefixed) API API documentation generated by ApiGen 2.7.0