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
|
|
|
$port = property_exists($xml, 'port') ? (int) $xml->port : null; |
36
|
|
|
$option = new Option($options, $filter, $port); |
37
|
|
|
|
38
|
|
|
return new Config( |
39
|
|
|
$profile, |
40
|
|
|
$option->watch, |
41
|
|
|
new ConfigFilter($option->and, $option->or, $option->color), |
|
|
|
|
42
|
|
|
$mode, |
43
|
|
|
$option->port, |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array<string> $argv |
49
|
|
|
* @param array<string, string|bool> $options |
50
|
|
|
*/ |
51
|
|
|
public static function fromCommandLine(int $argc, array $argv, array $options): Config |
52
|
|
|
{ |
53
|
|
|
$option = new Option($options, null, null); |
54
|
|
|
|
55
|
|
|
return new Config( |
56
|
|
|
(string) realpath($argv[$argc - 1]), |
57
|
|
|
$option->watch, |
58
|
|
|
new ConfigFilter($option->and, $option->or, $option->color), |
|
|
|
|
59
|
|
|
$option->mode, |
60
|
|
|
$option->port, |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|