Completed
Push — master ( 076f7c...8b4f44 )
by Yo!
03:06
created

ConfigFileLoader::setCurrentDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Yosymfony Config-loader.
5
 *
6
 * (c) YoSymfony <http://github.com/yosymfony>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Yosymfony\ConfigLoader;
13
14
use Yosymfony\ConfigLoader\Exception;
15
16
/**
17
 * Abstract class used by built-in loaders.
18
 *
19
 * @author Victor Puertas <[email protected]>
20
 */
21
abstract class ConfigFileLoader implements LoaderInterface
22
{
23
    private const DIST_EXTENSION = "dist";
24
25
    /** @var LoaderResolverInterface */
26
    private $loaderResolver;
27
    /** @var FileLocatorInterface */
28
    private $locator;
29
    /** @var string */
30
    private $currentDir;
31
32
    public function __construct(FileLocatorInterface $locator)
33
    {
34
        $this->locator = $locator;
35
    }
36
37
    /**
38
     * Resolve the location of a file following the hierarchy:
39
     *    1. filename.ext
40
     *    2. filename.ext.dist (if filename.ext does not exist).
41
     *
42
     *    or
43
     *
44
     *    filename.ext.dist if the .dist is included in the resource.
45
     *
46
     * @param string $resource Filename path
47
     *
48
     * @return string The filename location
49
     *
50
     * @throws \InvalidArgumentException When the file is not found
51
     */
52
    public function getLocation(string $resource) : string
53
    {
54
        if (!$this->isDistExtension($resource)) {
55
            try {
56
                return $this->getLocator()->locateFirst($resource, $this->currentDir);
57
            } catch (\InvalidArgumentException $ex) {
58
                $resource = $resource.'.'.self::DIST_EXTENSION;
59
            }
60
        }
61
62
        return  $this->getLocator()->locateFirst($resource, null);
63
    }
64
65
    /**
66
     * Sets the current directory.
67
     *
68
     * @param string $dir
69
     */
70
    public function setCurrentDir($dir)
71
    {
72
        $this->currentDir = $dir;
73
    }
74
75
    /**
76
     * Returns the file locator used by this loader.
77
     *
78
     * @return FileLocatorInterface
79
     */
80
    public function getLocator() : FileLocatorInterface
81
    {
82
        return $this->locator;
83
    }
84
85
    public function setLoaderResolver(LoaderResolverInterface $loaderResolver) : void
86
    {
87
        $this->loaderResolver = $loaderResolver;
88
    }
89
90
    /**
91
     * Returns the Loader Resolver implementation
92
     *
93
     * @return LoaderResolverInterface
94
     */
95
    protected function getLoaderResolver() : LoaderResolverInterface
96
    {
97
        return $this->loaderResolver;
98
    }
99
100
    /**
101
     * Has the file resource a ".dist" extension?
102
     *
103
     * @param string $resource The filename
104
     *
105
     * @return bool
106
     */
107
    protected function isDistExtension(string $resource) : bool
108
    {
109
        return pathinfo($resource, PATHINFO_EXTENSION) === self::DIST_EXTENSION;
110
    }
111
112
    /**
113
     * Parses the repositories in "imports" key
114
     *
115
     * @param Repository $repository
116
     * @param string     $file
117
     *
118
     * @return RepositoryInterface
119
     *
120
     * @throws InvalidArgumentException If error with "imports" key
121
     */
122
    protected function parseImports(RepositoryInterface $repository, string $file) : RepositoryInterface
123
    {
124
        if (!isset($repository['imports'])) {
125
            return $repository;
126
        }
127
128
        if (!is_array($repository['imports'])) {
129
            throw new \InvalidArgumentException(sprintf('The "imports" key should contain an array in %s. Check your YAML syntax.', $file));
130
        }
131
132
        foreach ($repository['imports'] as $import) {
133
            if (!is_array($import)) {
134
                $import = ['resource' => $import];
135
            }
136
            
137
            $ignoreErrors = isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false;
138
139
            $importedResource = $this->import($import['resource'], null, $ignoreErrors, $file);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $importedResource is correct as $this->import($import['r..., $ignoreErrors, $file) (which targets Yosymfony\ConfigLoader\ConfigFileLoader::import()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
140
141
            if ($importedResource) {
142
                $repository = $repository->union($importedResource);
143
            }
144
        }
145
146
        return $repository;
147
    }
148
149
    /**
150
     * Checks if the file has the extension passed as argument. This method
151
     * is aware about "dist" files
152
     *
153
     * @param string $file
154
     * @param string $extension Extension to check without dot. e.g: "json"
155
     *
156
     * @return bool
157
     */
158
    protected function hasResourceExtension(string $file, string $extension) : bool
159
    {
160
        return preg_match("#\.{$extension}(\.dist)?$#", $file) === 1;
161
    }
162
163
    /**
164
     * Reads a file
165
     *
166
     * @param string $file The name of the file
167
     *
168
     * @return string The file's content
169
     *
170
     * @throws BadFileException If the file is not a file or it is not readable
171
     */
172
    protected function readFile(string $file) : string
173
    {
174
        if (is_file($file) === false) {
175
            throw new BadFileException("The file \"{$file}\" is not a file.", $file);
176
        }
177
178
        if (is_readable($file) === false) {
179
            throw new BadFileException("Unable to open \"{$file}\" as the file is not readable.", $file);
180
        }
181
182
        return file_get_contents($file);
183
    }
184
185
    private function import(string $resource, string $type = null, bool $ignoreErrors = false, string $sourceResource = null) : ?RepositoryInterface
0 ignored issues
show
Unused Code introduced by
The parameter $sourceResource is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
186
    {
187
        $loader = $this->getLoaderResolver()->resolveLoader($resource, $type);
188
189
        if ($loader === null) {
190
            if (!$ignoreErrors) {
191
                throw new LoaderLoadException($resource, $type);
192
            }
193
194
            return null;
195
        }
196
197
        return $loader->load($resource, $type);
198
    }
199
}
200