1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpUnitGen\Configuration; |
4
|
|
|
|
5
|
|
|
use PhpUnitGen\Configuration\ConfigurationInterface\ConsoleConfigFactoryInterface; |
6
|
|
|
use PhpUnitGen\Configuration\ConfigurationInterface\ConsoleConfigInterface; |
7
|
|
|
use PhpUnitGen\Exception\InvalidConfigException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AbstractConsoleConfigFactory. |
11
|
|
|
* |
12
|
|
|
* @author Paul Thébaud <[email protected]>. |
13
|
|
|
* @copyright 2017-2018 Paul Thébaud <[email protected]>. |
14
|
|
|
* @license https://opensource.org/licenses/MIT The MIT license. |
15
|
|
|
* @link https://github.com/paul-thebaud/phpunit-generator |
16
|
|
|
* @since Class available since Release 2.0.0. |
17
|
|
|
*/ |
18
|
|
|
abstract class AbstractConsoleConfigFactory implements ConsoleConfigFactoryInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
public function invoke(string $configPath): ConsoleConfigInterface |
24
|
|
|
{ |
25
|
|
|
return new ConsoleConfig($this->decode($configPath)); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function invokeOneFile(string $configPath, string $sourceFile, string $targetFile): ConsoleConfigInterface |
32
|
|
|
{ |
33
|
|
|
if (! file_exists($sourceFile)) { |
34
|
|
|
throw new InvalidConfigException(sprintf('The source file "%s" does not exists.', $sourceFile)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$configArray = $this->decode($configPath); |
38
|
|
|
$configArray['dirs'] = []; |
39
|
|
|
$configArray['files'] = [ |
40
|
|
|
$sourceFile => $targetFile |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
return new ConsoleConfig($configArray); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Decode a configuration file to get a configuration array. |
48
|
|
|
* |
49
|
|
|
* @param string $configPath The configuration file path. |
50
|
|
|
* |
51
|
|
|
* @return array The decoded configuration array. |
52
|
|
|
* |
53
|
|
|
* @throws InvalidConfigException If the configuration is invalid. |
54
|
|
|
*/ |
55
|
|
|
abstract protected function decode(string $configPath): array; |
56
|
|
|
} |
57
|
|
|
|