Completed
Push — master ( 0b2272...9516ca )
by Mikael
02:47
created

Configuration   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 0
loc 130
ccs 36
cts 36
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setBaseDirectories() 0 18 5
B load() 0 38 9
A loadFromDir() 0 9 2
1
<?php
2
3
namespace Anax\Configure;
4
5
/**
6
 * Load configuration for a specified item, look in several places for the
7
 * configuration files or directories. Return the configuration as the value
8
 * received from the configuration file.
9
 */
10
class Configuration
11
{
12
    /**
13
     * @var [] $dirs where to look for configuration items.
0 ignored issues
show
Documentation introduced by
The doc-type [] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
14
     */
15
    protected $dirs = [];
16
17
18
19
    /**
20
     * Set the directories where to look for configuration
21
     * items (files, directories) to load.
22
     *
23
     * @throws Exception when the path to any of the directories are incorrect.
24
     *
25
     * @param array $dirs with the path to the config directories to search in.
26
     *
27
     * @return self to allow chaining.
28
     */
29 6
    public function setBaseDirectories(array $dirs): object
30
    {
31 6
        if (empty($dirs)) {
32 1
            throw new Exception(t("The array for configuration directories can not be empty."));
33
        }
34
35 5
        foreach ($dirs as $dir) {
36 5
            if (!(is_readable($dir) && is_dir($dir))) {
37 1
                throw new Exception(t(
38 1
                    "The configuration dir '@dir' is not a valid path.",
39 5
                    ["@dir" => $dir]
40
                ));
41
            }
42
        }
43
44 4
        $this->dirs = $dirs;
45 4
        return $this;
46
    }
47
48
49
50
    /**
51
     * Read configuration from file or directory, if a file, look though all
52
     * base dirs and use the first configuration that is found. A configuration
53
     * item can be combined from a file and a directory, when available in the
54
     * same base directory.
55
     *
56
     * The resulting configuration is always an array, its structure contains
57
     * values from each individual configuration file, like this.
58
     *
59
     * $config = [
60
     *      "base" => configuration returned from file.php,
61
     *      "items" => [
62
     *          "file1.php" => configuration returned from dir/file1.php,
63
     *          "file2.php" => configuration returned from dir/file2.php,
64
     *      ]
65
     * ]
66
     *
67
     * The configuration files in the directory are loaded per alphabetical
68
     * order.
69
     *
70
     * @param string $item is a name representing the module and is used to
71
     *                     combine the path to search for.
72
     *
73
     * @return mixed with returned value from the loaded configuration.
74
     *
75
     * @throws Exception when configuration item can not be found.
76
     * @throws Exception when $dirs are empty.
77
     */
78 5
    public function load(string $item): array
79
    {
80 5
        if (empty($this->dirs)) {
81 1
            throw new Exception(t("The array for configuration directories can not be empty."));
82
        }
83
84 4
        $found = false;
85 4
        $config = [];
86 4
        foreach ($this->dirs as $dir) {
87 4
            $path = "$dir/$item";
88 4
            $file = "$path.php";
89
90
            // The configuration is found in a file
91 4
            if (is_readable($file) && is_file($file)) {
92 2
                $found = true;
93 2
                $config["base"] = require $file;
94
            }
95
96
            // The configuration is found in a directory
97 4
            if (is_readable($path) && is_dir($path)) {
98 2
                $found = true;
99 2
                $config["items"] = $this->loadFromDir($path);
100
            }
101
102 4
            if ($found) {
103 4
                break;
104
            }
105
        }
106
107 4
        if (!$found) {
108 1
            throw new Exception(t(
109 1
                "Configure item '@item' can not be found.",
110 1
                ["@item" => $item]
111
            ));
112
        }
113
114 3
        return $config;
115
    }
116
117
118
119
    /**
120
     * Read configuration a directory, loop through all files and add
121
     * them into the $config array as [
122
     *      "file1.php" => loaded configuration from dir/file1.php,
123
     *      "file2.php" => loaded configuration from dir/file2.php,
124
     * ].
125
     *
126
     * @param string $path is the path to the directory containing config files.
127
     *
128
     * @return array with configuration for each file.
129
     */
130 2
    public function loadFromDir(string $path): array
131
    {
132 2
        $config = [];
133 2
        foreach (glob("$path/*.php") as $file) {
134 2
            $config[basename($file)] = require $file;
135
        }
136
137 2
        return $config;
138
    }
139
}
140