Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • 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
    • Bridges
      • Nette

Classes

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