Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

Classes

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