Section.php 5.26 KB
Newer Older
Prosper De Laure's avatar
Prosper De Laure committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
 * Class file for App Configuration Section
 * @package core\Configuration
 */
namespace core\Configuration;
/**
 * Class file for App Configuration Section
 * 
 * @package Core
 */
class Section
    extends \ArrayObject
{
    /* Constants */

    /* Properties */
    /**
     * The name of the section or null if config root
     * @var string
     */
    protected $name;


    /* Methods */
    /**
     * Constructor for a configuration section
     * @param string $name     The name of the configuration section
     * @param array  $contents The associative array of configuration sections or directives to load on construction or null
     * 
     * @return void
     */
    public function __construct($name = null, $contents = null)
    {
        $this->name = $name;

        if ($contents) {
            $this->load($contents);
        }
    }

    
    /**
     * Load an array of configuration sections or directives
     * @param array $contents The associative array of configuration sections or directives
     */
    public function load($contents)
    {
        foreach ($contents as $name => $content) {
            if (is_array($content)) {
                if (!isset($this[$name])) {
                    $this[$name] = new Section($name);
                }
                $this[$name]->load($content);
            } else {
                if (isset($this[$name])) {
                    continue;
                }

                // Try to find and replace constants
                if (defined($content)) {
                    $content = constant($content);
                }

                $content = trim(str_replace('&quot;', '\"', $content));
                $content = str_replace('\"', '"', $content);

                // Check name, if variable, content will be stored for later use
                if (preg_match("/^%[A-Za-z_]%$/", $name)) {
                    $varname = substr($name, 1, -1);
                    Configuration::getInstance()->registerVariable($varname, $content);
                    continue;
                }
                if (preg_match("/^\@var\.(\w+)$/", $name, $match)) {
                    $varname = $match[1];
                    Configuration::getInstance()->registerVariable($varname, $content);
                    continue;
                }
                
                // Find and replace variables
                if (preg_match_all("#%[A-Za-z_]+%#", $content, $variables)) {
                    foreach ($variables[0] as $variable) {
                        //$value = $this->getVariable($variable);
                        $varname = substr($variable, 1, -1);
                        $value = Configuration::getInstance()->getVariable($varname);
                        if (is_null($value)) {
                            throw new \Exception("Error retrieving variable '$varname' for configuration directive '$name'");
                        }
                        $content = str_replace($variable, $value, $content);
                    }
                }

                // Parse array
                if (preg_match("/^\[.+\]$/s", $content) || preg_match("/^\{.+\}$/s", $content)) {
                    $content = \laabs::decodeIni($content);
                }


                //preg_replace('#\\\\"#', '"', $value);

                // Finally set directive value
                $this->offsetSet($name, $content);
            }
        }
    }


    /**
     * Imports a configuration section
     * @param object $contents The core\Configuration\Section object to import
     */
    public function import(Section $contents)
    {
        foreach ($contents as $name => $content) {
            if ($content instanceof Section) {
                if (!isset($this[$name])) {
                    $this[$name] = new Section($name);
                }
                $this[$name]->import($content);
            } else {
                $this->offsetSet($name, $content);
            }
        }
    }

    /**
     * Exports a configuration section
     * @param string $namespace The namespace of the directives or section to export
     * 
     * @return configuration\Section
     */
    public function export($namespace = false)
    {
        $namespace = str_replace(LAABS_URI_SEPARATOR, ".", $namespace);

        $section = new Section($namespace);

        foreach ($this as $name => $content) {
            // Extract at root level
            if (!($content instanceof Section)) {
                
                /* directive defined in requested namespace : remove namespace and set local name */
                // ns.name && ns is requested => import as name
                // ns.name && not the requested ns => import as ns.name
                // name && ns not requested 
                if ($namespace) {
                    if (strpos($name, $namespace . ".") === 0) {
                        $section->offsetSet(str_replace($namespace . ".", "", $name), $content);
                    } elseif (strpos($name, ".") !== false) {
                        $section->offsetSet($name, $content);
                    }
                } else {
                    $section->offsetSet($name, $content);  
                }

            } elseif ($name == $namespace) {
                $section->import($content);
            }

        }

        return $section;
    }
}