FileLoader::getRequire()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
namespace Jumilla\Addomnipot\Laravel\Repository;
4
5
use Illuminate\Filesystem\Filesystem;
6
7
class FileLoader implements LoaderInterface
8
{
9
    /**
10
     * The filesystem instance.
11
     *
12
     * @var \Illuminate\Filesystem\Filesystem
13
     */
14
    protected $files;
15
16
    /**
17
     * The default configuration path.
18
     *
19
     * @var string
20
     */
21
    protected $defaultPath;
22
23
    /**
24
     * All of the named path hints.
25
     *
26
     * @var array
27
     */
28
    protected $hints = [];
29
30
    /**
31
     * A cache of whether namespaces and groups exists.
32
     *
33
     * @var array
34
     */
35
    protected $exists = [];
36
37
    /**
38
     * Create a new file configuration loader.
39
     *
40
     * @param \Illuminate\Filesystem\Filesystem $files
41
     * @param string                            $defaultPath
42
     */
43 6
    public function __construct(Filesystem $files, $defaultPath)
44
    {
45 6
        $this->files = $files;
46 6
        $this->defaultPath = $defaultPath;
47 6
    }
48
49
    /**
50
     * Load the given configuration group.
51
     *
52
     * @param string $group
53
     * @param string $namespace
54
     *
55
     * @return array
56
     */
57 2
    public function load($group, $namespace = null)
58
    {
59 2
        $items = [];
60
61 2
        $path = $this->getPath($namespace);
62
63 2
        if (is_null($path)) {
64 1
            return $items;
65
        }
66
67 1
        $file = "{$path}/{$group}.php";
68
69 1
        if ($this->files->exists($file)) {
70 1
            $items = $this->getRequire($file);
71
        }
72
73 1
        return $items;
74
    }
75
76
    /**
77
     * Determine if the given group exists.
78
     *
79
     * @param string $group
80
     * @param string $namespace
81
     *
82
     * @return bool
83
     */
84 4
    public function exists($group, $namespace = null)
85
    {
86 4
        $key = $group.$namespace;
87
88
        // We'll first check to see if we have determined if this namespace and
89
        // group combination have been checked before. If they have, we will
90
        // just return the cached result so we don't have to hit the disk.
91 4
        if (isset($this->exists[$key])) {
92
            return $this->exists[$key];
93
        }
94
95 4
        $path = $this->getPath($namespace);
96
97
        // To check if a group exists, we will simply get the path based on the
98
        // namespace, and then check to see if this files exists within that
99
        // namespace. False is returned if no path exists for a namespace.
100 4
        if (is_null($path)) {
101 1
            return $this->exists[$key] = false;
102
        }
103
104 3
        $file = "{$path}/{$group}.php";
105
106
        // Finally, we can simply check if this file exists. We will also cache
107
        // the value in an array so we don't have to go through this process
108
        // again on subsequent checks for the existing of the config file.
109 3
        $exists = $this->files->exists($file);
110
111 3
        return $this->exists[$key] = $exists;
112
    }
113
114
    /**
115
     * Get the configuration path for a namespace.
116
     *
117
     * @param string $namespace
118
     *
119
     * @return string
120
     */
121 6
    protected function getPath($namespace)
122
    {
123 6
        if (is_null($namespace)) {
124 2
            return $this->defaultPath;
125 4
        } elseif (isset($this->hints[$namespace])) {
126 2
            return $this->hints[$namespace];
127
        }
128 2
    }
129
130
    /**
131
     * Add a new namespace to the loader.
132
     *
133
     * @param string $namespace
134
     * @param string $hint
135
     */
136 2
    public function addNamespace($namespace, $hint)
137
    {
138 2
        $this->hints[$namespace] = $hint;
139 2
    }
140
141
    /**
142
     * Returns all registered namespaces with the config
143
     * loader.
144
     *
145
     * @return array
146
     */
147
    public function getNamespaces()
148
    {
149
        return $this->hints;
150
    }
151
152
    /**
153
     * Get a file's contents by requiring it.
154
     *
155
     * @param string $path
156
     *
157
     * @return mixed
158
     */
159 1
    protected function getRequire($path)
160
    {
161 1
        return $this->files->getRequire($path);
162
    }
163
164
    /**
165
     * Get the Filesystem instance.
166
     *
167
     * @return \Illuminate\Filesystem\Filesystem
168
     */
169 4
    public function getFilesystem()
170
    {
171 4
        return $this->files;
172
    }
173
}
174