summaryrefslogtreecommitdiff
path: root/src/Manager/Config/Loader.php
blob: 97df62e1daa2c0dd2fd155e3cfb0af3ec7fe56c4 (plain)
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
<?php

namespace App\Config;

/**
 * A class to load a config into an array
 * Throws an exception if it fails to laod
 *
 * @author Phil Burton <phil@pgburton.com>
 */
class Loader
{
    /**
     * A loaded config array
     *
     * @var mixed[]
     */
    protected $config;

    /**
     * Try and load a config
     *
     * @author Phil Burton <phil@pgburton.com>
     */
    public function __construct()
    {
        foreach ($this->getConfigPaths() as $path) {
            if ($config = $this->loadConfig($path)) {
                $this->validate();
                $this->clean();
                return;
            }
        }

        // No config found
        throw new \Exception('Could not find config file.');
    }

    /**
     * Try and load a config file
     * If we can't return false
     *
     * @author Phil Burton <phil@pgburton.com>
     * @return mixed[]|false
     */
    public function loadConfig(string $path)
    {
        $filename = $path . '/' . $this->getConfigFilename();

        if (!$this->config) {
            if (!file_exists($filename)) {
                return false;
            }

            if (!is_readable($filename)) {
                return false;
            }

            if (!$config = include($filename)) {
                return false;
            }

            $this->config = $config;
        }

        return $this->config;
    }

    /**
     * Return the nsam of the config file to load
     *
     * @author Phil Burton <phil@pgburton.com>
     * @return string;
     */
    public function getConfigFilename()
    {
        return 'dipper.php';
    }

    /**
     * Make sure the config is up to scratch
     *
     * @author Phil Burton <phil@pgburton.com>
     */
    public function validate()
    {
        $config = $this->getConfig();

        foreach ($this->getRequired() as $required) {
            if (!isset($config[$required])) {
                throw new \Exception("Could not find " . $required . " in config file.");
            }
        }
    }

    /**
     * Clean up the config ready for use
     *
     * @author Phil Burton <phil@pgburton.com>
     */
    public function clean()
    {
        $config = $this->getConfig();
        foreach ($config as &$item) {
            // Remove trailing slashes
            $item = rtrim($item, "/");
        }

        $this->config = $config;
    }

    /**
     * Return the array of config paths
     *
     * @author Phil Burton <phil@pgburton.com>
     * @return string[]
     */
    public function getConfigPaths()
    {
        return [
            realpath(dirname(__FILE__) . "/../../"), // Root of dipper
            realpath(dirname(__FILE__) . "/../../config") // Config directory in dipper
        ];
    }

    /**
     * Return the loaded config
     *
     * @author Phil Burton <phil@pgburton.com>
     * @return mixed[]
     */
    public function getConfig()
    {
        return $this->config;
    }

    /**
     * Return the required config keys
     *
     * @author Phil Burton <phil@pgburton.com>
     * @return string[]
     */
    public function getRequired()
    {
        return [
            "SITES_ROOT",
            "CONFIG_ROOT"
        ];
    }
}