Completed
Push — master ( 4d43de...d186d6 )
by Joram van den
04:11
created

Ajde_Config_Repository::readConfigDir()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
rs 8.9197
cc 4
eloc 12
nc 4
nop 1
1
<?php
2
3
class Ajde_Config_Repository extends Ajde_Object_Standard
4
{
5
    /**
6
     * TODO
7
     *
8
     * @param string $directory
9
     */
10
    public function __construct($directory)
11
    {
12
        $this->readConfigDir($directory);
13
        $this->defaults();
14
    }
15
16
    /**
17
     * TODO
18
     *
19
     * @param string $directory
20
     * @throws Exception
21
     */
22
    public function readConfigDir($directory)
23
    {
24
        $environment = Ajde_Environment::current();
25
26
        $searchDirs = [
27
            CORE_DIR . $directory, '*.json',
28
            CORE_DIR . $directory . $environment . DS, '*.json',
29
            APP_DIR . $directory, '*.json',
30
            APP_DIR . $directory . $environment . DS, '*.json'
31
        ];
32
33
        foreach($searchDirs as $searchDir) {
34
            foreach(Ajde_Fs_Find::findFiles($searchDir, '*.json') as $configFile)
35
            {
36
                if (!$configData = json_decode(file_get_contents($configFile), true)) {
37
                    throw new Exception('Config file ' . $configFile . ' contains invalid JSON');
38
                }
39
40
                $this->merge(pathinfo($configFile, PATHINFO_FILENAME), $configData);
41
            }
42
        }
43
    }
44
45
    public function defaults()
46
    {
47
        // URI fragments
48
        $this->set("app.protocol", (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS'])) ? 'https://' : 'http://');
49
        $this->set("app.domain", $_SERVER['SERVER_NAME']);
50
        $this->set("app.path", str_replace('index.php', '', $_SERVER['PHP_SELF']));
51
        $this->set("app.root", $this->get("app.protocol") . $this->site_domain . $this->site_path);
0 ignored issues
show
Bug introduced by
The property site_domain does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property site_path does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
52
        $this->set("i18n.root", $this->get("app.root"));
53
54
        // Set default timezone now
55
        date_default_timezone_set($this->timezone);
0 ignored issues
show
Bug introduced by
The property timezone does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
56
    }
57
}
58