1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Shopware\Psh\Config; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Resolve the path to the config file |
8
|
|
|
*/ |
9
|
|
|
class ConfigFileFinder |
10
|
|
|
{ |
11
|
|
|
const VALID_FILE_NAME_GLOB = '.psh.*'; |
12
|
|
|
|
13
|
|
|
public function discoverFiles(string $fromDirectory): array |
14
|
|
|
{ |
15
|
|
|
$files = $this->findFirstDirectoryWithConfigFile($fromDirectory); |
16
|
|
|
|
17
|
|
|
return $this->determineResultInDirectory($files); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $fromDirectory |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
public function findFirstDirectoryWithConfigFile(string $fromDirectory): array |
25
|
|
|
{ |
26
|
|
|
$currentDirectory = $fromDirectory; |
27
|
|
|
|
28
|
|
|
do { |
29
|
|
|
$globResult = glob($currentDirectory . '/' . self::VALID_FILE_NAME_GLOB); |
30
|
|
|
|
31
|
|
|
if ($globResult) { |
|
|
|
|
32
|
|
|
return $globResult; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$currentDirectory = dirname($currentDirectory); |
36
|
|
|
} while ($currentDirectory !== '/'); |
37
|
|
|
|
38
|
|
|
throw new \RuntimeException('No config file found, make sure you have created a .psh file'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param array $globResult |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function determineResultInDirectory(array $globResult): array |
46
|
|
|
{ |
47
|
|
|
if (count($globResult) === 1) { |
48
|
|
|
return $globResult; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$overrideFile = array_filter($globResult, function (string $file) { |
52
|
|
|
$extension = pathinfo($file, PATHINFO_EXTENSION); |
53
|
|
|
|
54
|
|
|
return $extension === 'override'; |
55
|
|
|
}); |
56
|
|
|
|
57
|
|
|
$configFile = array_filter($globResult, function (string $file) { |
58
|
|
|
$extension = pathinfo($file, PATHINFO_EXTENSION); |
59
|
|
|
|
60
|
|
|
return $extension !== 'override'; |
61
|
|
|
}); |
62
|
|
|
|
63
|
|
|
return array_merge([$configFile[0]], $overrideFile); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.