These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * slince config library |
||
4 | * @author Tao <[email protected]> |
||
5 | */ |
||
6 | namespace Slince\Config; |
||
7 | |||
8 | use Slince\Config\Exception\UnsupportedFormatException; |
||
9 | use Slince\Config\Exception\InvalidFileException; |
||
10 | use Slince\Config\Parser\IniParser; |
||
11 | use Slince\Config\Parser\JsonParser; |
||
12 | use Slince\Config\Parser\ParserInterface; |
||
13 | use Slince\Config\Parser\PHPParser; |
||
14 | |||
15 | class Config extends Collection implements ConfigInterface |
||
16 | { |
||
17 | /** |
||
18 | * Array of parser instances |
||
19 | * @var array |
||
20 | */ |
||
21 | protected static $parsers = []; |
||
22 | |||
23 | /** |
||
24 | * Array of supported file format parsers |
||
25 | * @var array |
||
26 | */ |
||
27 | protected static $supportedFileParsers = [ |
||
28 | PHPParser::class, |
||
29 | IniParser::class, |
||
30 | JsonParser::class |
||
31 | ]; |
||
32 | |||
33 | public function __construct($path = null) |
||
34 | { |
||
35 | parent::__construct([]); |
||
36 | is_null($path) || $this->load($path); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | public function load($path) |
||
43 | { |
||
44 | $paths = is_array($path) ? $path : [$path]; |
||
45 | foreach ($paths as $path) { |
||
46 | $this->merge($this->parseFile($path)); |
||
47 | } |
||
48 | return $this; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | public function dump($file) |
||
55 | { |
||
56 | $extension = pathinfo($file, PATHINFO_EXTENSION); |
||
57 | return static::getParser($extension)->dump($file, $this->toArray()); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Parse a configuration file or directory to an array |
||
62 | * @param string|array $path |
||
63 | * @return array |
||
64 | * @deprecated |
||
65 | */ |
||
66 | public function parse($path) |
||
67 | { |
||
68 | return $this->parseFile($path); |
||
0 ignored issues
–
show
|
|||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Parses a configuration file or directory |
||
73 | * @param string $path |
||
74 | * @return array |
||
75 | */ |
||
76 | protected function parseFile($path) |
||
77 | { |
||
78 | $data = []; |
||
79 | $files = $this->findConfigurationFiles($path); |
||
80 | foreach ($files as $file) { |
||
81 | $extension = pathinfo($file, PATHINFO_EXTENSION); |
||
82 | $data = array_merge($data, static::getParser($extension)->parse($file)); |
||
83 | } |
||
84 | return $data; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * finds all supported configuration files at the directory |
||
89 | * @param string $path |
||
90 | * @throws InvalidFileException |
||
91 | * @return array |
||
92 | */ |
||
93 | protected function findConfigurationFiles($path) |
||
94 | { |
||
95 | if (is_dir($path)) { |
||
96 | $files = glob($path . '/*.*'); |
||
97 | if (empty($files)) { |
||
98 | throw new InvalidFileException(sprintf('There are no supported configuration files in the directory "%s"', $path)); |
||
99 | } |
||
100 | } else { |
||
101 | if (!file_exists($path)) { |
||
102 | throw new InvalidFileException(sprintf('File "%s" does not exists', $path)); |
||
103 | } |
||
104 | $files = [$path]; |
||
105 | } |
||
106 | return $files; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Gets a file parser by its extension |
||
111 | * @param string $extension |
||
112 | * @throws UnsupportedFormatException |
||
113 | * @return ParserInterface |
||
114 | */ |
||
115 | protected static function getParser($extension) |
||
116 | { |
||
117 | if (isset(static::$parsers[$extension])) { |
||
118 | return static::$parsers[$extension]; |
||
119 | } |
||
120 | foreach (static::$supportedFileParsers as $parser) { |
||
121 | if (in_array($extension, call_user_func([$parser, 'getSupportedExtensions']))) { |
||
122 | static::$parsers[$extension] = new $parser(); |
||
123 | return static::$parsers[$extension]; |
||
124 | } |
||
125 | } |
||
126 | throw new UnsupportedFormatException($extension); |
||
127 | } |
||
128 | } |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.