|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Koriym\AppStateDiagram; |
|
6
|
|
|
|
|
7
|
|
|
use Koriym\DataFile\XmlConfigLoad; |
|
8
|
|
|
use SimpleXMLElement; |
|
9
|
|
|
|
|
10
|
|
|
use function assert; |
|
11
|
|
|
use function dirname; |
|
12
|
|
|
use function is_dir; |
|
13
|
|
|
use function is_file; |
|
14
|
|
|
use function property_exists; |
|
15
|
|
|
use function realpath; |
|
16
|
|
|
use function sprintf; |
|
17
|
|
|
|
|
18
|
|
|
final class ConfigFactory |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @param array<string> $argv |
|
22
|
|
|
* @param array<string, string|bool> $options |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function fromFile(string $configFile, int $argc = 1, array $argv = [''], array $options = []): Config |
|
25
|
|
|
{ |
|
26
|
|
|
$xml = (new XmlConfigLoad('asd.xml'))($configFile, dirname(__DIR__) . '/docs/asd.xsd'); |
|
27
|
|
|
assert(property_exists($xml, 'watch')); |
|
28
|
|
|
$dir = is_dir($configFile) ? $configFile : dirname($configFile); |
|
29
|
|
|
|
|
30
|
|
|
$maybePath = (string) realpath($argv[$argc - 1]); |
|
31
|
|
|
$profile = is_file($maybePath) && $configFile !== $maybePath ? $maybePath : sprintf('%s/%s', $dir, (string) $xml->alpsFile); |
|
32
|
|
|
/** @var ?SimpleXMLElement $filter */ |
|
33
|
|
|
$filter = property_exists($xml, 'filter') ? $xml->filter : null; |
|
34
|
|
|
$mode = property_exists($xml, 'mode') ? (string) $xml->mode : DumpDocs::MODE_HTML; |
|
35
|
|
|
$option = new Option($options, $filter); |
|
36
|
|
|
|
|
37
|
|
|
return new Config( |
|
38
|
|
|
$profile, |
|
39
|
|
|
$option->watch, |
|
40
|
|
|
new ConfigFilter($option->and, $option->or, $option->color), |
|
|
|
|
|
|
41
|
|
|
$mode |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param array<string> $argv |
|
47
|
|
|
* @param array<string, string|bool> $options |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function fromCommandLine(int $argc, array $argv, array $options): Config |
|
50
|
|
|
{ |
|
51
|
|
|
$option = new Option($options, null); |
|
52
|
|
|
|
|
53
|
|
|
return new Config( |
|
54
|
|
|
(string) realpath($argv[$argc - 1]), |
|
55
|
|
|
$option->watch, |
|
56
|
|
|
new ConfigFilter($option->and, $option->or, $option->color), |
|
|
|
|
|
|
57
|
|
|
$option->mode |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|