1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Vasoft\VersionIncrement; |
6
|
|
|
|
7
|
|
|
use Vasoft\VersionIncrement\Core\Help; |
8
|
|
|
use Vasoft\VersionIncrement\Core\Legend; |
9
|
|
|
use Vasoft\VersionIncrement\Core\UpdateRunner; |
10
|
|
|
use Vasoft\VersionIncrement\Events\Event; |
11
|
|
|
use Vasoft\VersionIncrement\Events\EventType; |
12
|
|
|
use Vasoft\VersionIncrement\Exceptions\ApplicationException; |
13
|
|
|
use Vasoft\VersionIncrement\Exceptions\InvalidConfigFileException; |
14
|
|
|
|
15
|
|
|
class Application |
16
|
|
|
{ |
17
|
10 |
|
public function run(array $argv): int |
18
|
|
|
{ |
19
|
10 |
|
array_shift($argv); |
20
|
10 |
|
$config = null; |
21
|
|
|
|
22
|
|
|
try { |
23
|
10 |
|
$composerJsonPath = getenv('COMPOSER') ?: getcwd(); |
24
|
9 |
|
$configFile = $composerJsonPath . '/.vs-version-increment.php'; |
25
|
9 |
|
$config = $this->loadConfig($configFile); |
26
|
8 |
|
$helper = new Help(); |
27
|
8 |
|
$handlers = [ |
28
|
8 |
|
$helper, |
29
|
8 |
|
new Legend($config), |
30
|
8 |
|
new UpdateRunner($composerJsonPath, $config), |
31
|
8 |
|
]; |
32
|
8 |
|
$helper->registerHandlersFromArray($handlers); |
33
|
8 |
|
foreach ($handlers as $handler) { |
34
|
8 |
|
if (($exitCode = $handler->handle($argv)) !== null) { |
35
|
5 |
|
return $exitCode; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
3 |
|
return 0; |
40
|
2 |
|
} catch (ApplicationException $e) { |
41
|
1 |
|
fwrite(STDERR, 'Error: ' . $e->getMessage() . PHP_EOL); |
42
|
1 |
|
$config?->getEventBus()->dispatch(new Event(EventType::ON_ERROR)); |
43
|
|
|
|
44
|
1 |
|
return $e->getCode(); |
45
|
1 |
|
} catch (\Throwable $e) { |
46
|
1 |
|
fwrite(STDERR, 'Error: ' . $e->getMessage() . PHP_EOL); |
47
|
1 |
|
$config?->getEventBus()->dispatch(new Event(EventType::ON_ERROR)); |
48
|
|
|
|
49
|
1 |
|
return ApplicationException::DEFAULT_CODE; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @throws InvalidConfigFileException |
55
|
|
|
*/ |
56
|
9 |
|
private function loadConfig(string $configFile): Config |
57
|
|
|
{ |
58
|
9 |
|
if (file_exists($configFile)) { |
59
|
7 |
|
$config = include $configFile; |
60
|
7 |
|
if (!$config instanceof Config) { |
61
|
7 |
|
throw new InvalidConfigFileException(); |
62
|
|
|
} |
63
|
|
|
} else { |
64
|
2 |
|
$config = new Config(); |
65
|
|
|
} |
66
|
|
|
|
67
|
8 |
|
return $config; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|