FileLoader   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 159
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A load() 0 18 3
A exists() 0 20 3
A getPath() 0 8 3
A addNamespace() 0 4 1
A getNamespaces() 0 4 1
A getRequire() 0 4 1
A getFilesystem() 0 4 1
1
<?php
2
3
namespace Magister\Services\Config;
4
5
use Magister\Services\Filesystem\Filesystem;
6
7
/**
8
 * Class FileLoader.
9
 */
10
class FileLoader implements LoaderInterface
11
{
12
    /**
13
     * The filesystem instance.
14
     *
15
     * @var \Magister\Services\Filesystem\Filesystem
16
     */
17
    protected $files;
18
19
    /**
20
     * The default configuration path.
21
     *
22
     * @var string
23
     */
24
    protected $defaultPath;
25
26
    /**
27
     * All of the named path hints.
28
     *
29
     * @var array
30
     */
31
    protected $hints = [];
32
33
    /**
34
     * A cache of whether namespaces and groups exists.
35
     *
36
     * @var array
37
     */
38
    protected $exists = [];
39
40
    /**
41
     * Create a new file configuration loader.
42
     *
43
     * @param \Magister\Services\Filesystem\Filesystem $files
44
     * @param string                                   $defaultPath
45
     */
46
    public function __construct(Filesystem $files, $defaultPath)
47
    {
48
        $this->files = $files;
49
        $this->defaultPath = $defaultPath;
50
    }
51
52
    /**
53
     * Load the given configuration group.
54
     *
55
     * @param string $group
56
     * @param string $namespace
57
     *
58
     * @return array
59
     */
60
    public function load($group, $namespace = null)
61
    {
62
        $items = [];
63
64
        $path = $this->getPath($namespace);
65
66
        if (is_null($path)) {
67
            return $items;
68
        }
69
70
        $file = "{$path}/{$group}.php";
71
72
        if ($this->files->exists($file)) {
73
            $items = $this->files->getRequire($file);
74
        }
75
76
        return $items;
77
    }
78
79
    /**
80
     * Determine if the given group exists.
81
     *
82
     * @param string $group
83
     * @param string $namespace
84
     *
85
     * @return bool
86
     */
87
    public function exists($group, $namespace = null)
88
    {
89
        $key = $group.$namespace;
90
91
        if (isset($this->exists[$key])) {
92
            return $this->exists[$key];
93
        }
94
95
        $path = $this->getPath($namespace);
96
97
        if (is_null($path)) {
98
            return $this->exists[$key] = false;
99
        }
100
101
        $file = "{$path}/{$group}.php";
102
103
        $exists = $this->files->exists($file);
104
105
        return $this->exists[$key] = $exists;
106
    }
107
108
    /**
109
     * Get the configuration path for a namespace.
110
     *
111
     * @param string $namespace
112
     *
113
     * @return string
114
     */
115
    protected function getPath($namespace)
116
    {
117
        if (is_null($namespace)) {
118
            return $this->defaultPath;
119
        } elseif (isset($this->hints[$namespace])) {
120
            return $this->hints[$namespace];
121
        }
122
    }
123
124
    /**
125
     * Add a new namespace to the loader.
126
     *
127
     * @param string $namespace
128
     * @param string $hint
129
     *
130
     * @return void
131
     */
132
    public function addNamespace($namespace, $hint)
133
    {
134
        $this->hints[$namespace] = $hint;
135
    }
136
137
    /**
138
     * Returns all registered namespaces with the config loader.
139
     *
140
     * @return array
141
     */
142
    public function getNamespaces()
143
    {
144
        return $this->hints;
145
    }
146
147
    /**
148
     * Get a file's contents by requiring it.
149
     *
150
     * @param string $path
151
     *
152
     * @return mixed
153
     */
154
    protected function getRequire($path)
155
    {
156
        return $this->files->getRequire($path);
157
    }
158
159
    /**
160
     * Get the filesystem instance.
161
     *
162
     * @return \Magister\Services\Filesystem\Filesystem
163
     */
164
    public function getFilesystem()
165
    {
166
        return $this->files;
167
    }
168
}
169