Configuration.php 4.27 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
<?php
/**
 * Class file for App Configuration
 * @package core\Configuration
 */
namespace core\Configuration;

/**
 * Class that defines the configuration singleton object
 */
class Configuration
    extends Section
{
    /* Constants */

    /* Properties */
    /**
     * The object storage for the singleton instance
     * @var array
     * @static
     * @access protected
     */
    protected static $instance;

    /**
     * The set of variables defined on configurations
     * @var array
     */
    public $variables;

    /* Methods */
    /**
     * Get the configuration instance. It can be
     *  * from configuration files
     *  * from Instance of application if loaded
     *  * from the already instantiated instance during the same call to app
     * @return \core\Configuration\Configuration object
     */
    public static function getInstance()
    {
        if (!isset(self::$instance)) {
            if ($configuration = \laabs::getCache('configuration')) {
                self::$instance = $configuration;
            } else {
                self::$instance = new Configuration();
                self::$instance->registerVariable('laabsDirectory', dirname(getcwd()));
                
                $confFile = \laabs::getConfiguration();
                self::$instance->loadFile($confFile);
                
                \laabs::setCache('configuration', self::$instance);
            }
        }

        return self::$instance;
    }

    /**
     * Registers a variable on the global scope
     * @param string $name  The name of the variable
     * @param mixed  $value The value of the variable
     * 
     * @return void
     */
    public function registerVariable($name, $value)
    {
        $this->variables[$name] = $value;
    }

    /**
     * Retrieves a variable on the global scope
     * @param string $name the name of the variable
     * 
     * @return mixed $value The value of the variable
     */
    public function getVariable($name)
    {
        if (isset($this->variables[$name])) {
            return $this->variables[$name];
        }
    }

    /**
     * Parse and loads a configuration file into current section
     * @param string $conffile The path of a configuration file from conf root
     * 
     * @return void
     * 
     * @access protected
     */
    protected function loadFile($conffile)
    {   
        $parsedContent = $this->parseFile($conffile);

        if ($this->name) {
            $section = false;
        } else {
            $section = true;
        }

        $contents = parse_ini_string($parsedContent, $section);

        $this->load($contents);
    }

    protected function parseFile($conffile) 
    {
        if (!is_file($conffile)) {
            throw new \Exception("Unable to load configuration file '".$conffile."'");
        }

        $confdir = \laabs\dirname($conffile);

        // Load file or directory and process includes
        $confstring = "";
        $confhdl = fopen($conffile, 'r');
        while ($line = fgets($confhdl)) {
            if (strpos($line, "@include") === 0) {
                $includedFile = trim(str_replace("@include", "", $line));
                $includeDir = null;

                if (is_dir($includedFile)) {
                    $includeDir = $includedFile;
                } else if (is_dir($confdir . DIRECTORY_SEPARATOR . $includedFile)) {
                    $includeDir = $confdir . DIRECTORY_SEPARATOR . $includedFile;
                }
                else if (is_file($confdir . DIRECTORY_SEPARATOR . $includedFile)) {
                    $includedFile = $confdir . DIRECTORY_SEPARATOR . $includedFile;
                }

                if ($includeDir) {
                    foreach (glob($includeDir . DIRECTORY_SEPARATOR . "*.ini") as $filename) {
                        $includedFile = $filename;
                        $includedString = $this->parseFile($includedFile);
                        $confstring .= $includedString . PHP_EOL;
                    }
                } else {
                    $includedString = $this->parseFile($includedFile);
                    $confstring .= $includedString . PHP_EOL;
                }

            } else {
                $confstring .= str_replace('\"', '&quot;', $line);
            }
            
        }

        return $confstring;
    }


}