1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Tools\Installer; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Composer\Factory; |
7
|
|
|
use Composer\IO\ConsoleIO; |
8
|
|
|
use Composer\IO\IOInterface; |
9
|
|
|
use Composer\Script\Event; |
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
use PackageVersions\Versions; |
12
|
|
|
use Symfony\Component\Console\Application; |
13
|
|
|
use Symfony\Component\Console\Input\ArgvInput; |
14
|
|
|
use Symfony\Component\Console\Output\Output; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
use Symfony\Component\Yaml\Yaml; |
17
|
|
|
use Throwable; |
18
|
|
|
|
19
|
|
|
final class Installer |
20
|
|
|
{ |
21
|
|
|
const TITLE = 'PHP API Clients skeleton installer'; |
22
|
|
|
|
23
|
|
|
public static function postCreateProject(Event $composerEvent) |
24
|
|
|
{ |
25
|
|
|
require_once str_replace( |
26
|
|
|
'composer.json', |
27
|
|
|
'vendor/autoload.php', |
28
|
|
|
Factory::getComposerFile() |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
try { |
32
|
|
|
$path = str_replace( |
33
|
|
|
'composer.json', |
34
|
|
|
'installer.yml', |
35
|
|
|
Factory::getComposerFile() |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
if (!file_exists($path)) { |
39
|
|
|
throw new InvalidArgumentException('Missing installer configuration file'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
static::install($path, $composerEvent->getIO()); |
|
|
|
|
43
|
|
|
} catch (Throwable $throwable) { |
44
|
|
|
echo get_class($throwable), ' thrown with message: ', $throwable->getMessage(), PHP_EOL; |
45
|
|
|
echo $throwable->getTraceAsString(), PHP_EOL; |
46
|
|
|
exit(1); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private static function install(string $fileName, IOInterface $io) |
51
|
|
|
{ |
52
|
|
|
$yaml = Yaml::parse( |
53
|
|
|
file_get_contents( |
54
|
|
|
$fileName |
55
|
|
|
) |
56
|
|
|
); |
57
|
|
|
$app = new Application( |
58
|
|
|
self::TITLE, |
59
|
|
|
Versions::getVersion('api-clients/installer') |
60
|
|
|
); |
61
|
|
|
$app->add((new Install(Install::COMMAND))->setYaml($yaml)); |
62
|
|
|
$app->find(Install::COMMAND)->run(new ArgvInput([]), self::getOutput($io)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private static function getOutput(IOInterface $io): OutputInterface |
66
|
|
|
{ |
67
|
|
|
if ($io instanceof ConsoleIO) { |
68
|
|
|
return Closure::bind(function (ConsoleIO $consoleIO): OutputInterface { |
69
|
|
|
return $consoleIO->ouput; |
|
|
|
|
70
|
|
|
}, null, ConsoleIO::class)($io); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return new class($io) extends Output { |
74
|
|
|
/** |
75
|
|
|
* @var IOInterface |
76
|
|
|
*/ |
77
|
|
|
private $io; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* constructor. |
81
|
|
|
* @param IOInterface $io |
82
|
|
|
*/ |
83
|
|
|
public function __construct(IOInterface $io) |
84
|
|
|
{ |
85
|
|
|
parent::__construct(); |
86
|
|
|
$this->io = $io; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function doWrite($message, $newline) |
90
|
|
|
{ |
91
|
|
|
$this->io->write($message, $newline); |
92
|
|
|
} |
93
|
|
|
}; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Late static binding only has effect in subclasses. A
final
class cannot be extended anymore so late static binding cannot occurr. Consider replacingstatic::
withself::
.To learn more about late static binding, please refer to the PHP core documentation.