|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of CaptainHook. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace CaptainHook\App\Composer; |
|
11
|
|
|
|
|
12
|
|
|
use CaptainHook\App\CH; |
|
13
|
|
|
use Composer\IO\IOInterface; |
|
14
|
|
|
use Composer\Script\Event; |
|
15
|
|
|
use CaptainHook\App\Console\Command\Configuration; |
|
16
|
|
|
use CaptainHook\App\Console\Command\Install; |
|
17
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Cmd |
|
21
|
|
|
* |
|
22
|
|
|
* @package CaptainHook |
|
23
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
24
|
|
|
* @link https://github.com/captainhookphp/captainhook |
|
25
|
|
|
* @since Class available since Release 0.9.0 |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract class Cmd |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Gets called by composer after a successful package installation |
|
31
|
|
|
* |
|
32
|
|
|
* @param \Composer\Script\Event $event |
|
33
|
|
|
* @return void |
|
34
|
|
|
* @throws \Exception |
|
35
|
|
|
*/ |
|
36
|
2 |
|
public static function setup(Event $event) : void |
|
37
|
|
|
{ |
|
38
|
2 |
|
$extra = self::getExtraConfig($event); |
|
39
|
2 |
|
$config = self::extract(CH::COMPOSER_CONFIG, $extra, CH::CONFIG); |
|
40
|
2 |
|
$repo = self::extract(CH::COMPOSER_GIT_DIR, $extra, '.git'); |
|
41
|
2 |
|
$app = self::createApplication($event->getIO(), $config); |
|
42
|
|
|
|
|
43
|
2 |
|
self::configure($app, $config); |
|
44
|
2 |
|
self::install($app, $config, $repo); |
|
45
|
2 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param \CaptainHook\App\Composer\Application $app |
|
49
|
|
|
* @param string $config |
|
50
|
|
|
* @return void |
|
51
|
|
|
* @throws \Exception |
|
52
|
|
|
*/ |
|
53
|
2 |
|
private static function configure(Application $app, string $config) |
|
54
|
|
|
{ |
|
55
|
2 |
|
if (file_exists($config)) { |
|
56
|
1 |
|
$app->getIO()->write((' <info>Using CaptainHook config: ' . $config . '</info>')); |
|
57
|
1 |
|
return; |
|
58
|
|
|
} |
|
59
|
|
|
$options = [ |
|
60
|
1 |
|
'command' => 'configure', |
|
61
|
1 |
|
'--configuration' => $config |
|
62
|
|
|
]; |
|
63
|
1 |
|
$app->run(new ArrayInput($options)); |
|
64
|
1 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Installs the hooks to your local repository |
|
68
|
|
|
* |
|
69
|
|
|
* @param \CaptainHook\App\Composer\Application $app |
|
70
|
|
|
* @param string $config |
|
71
|
|
|
* @param string $repo |
|
72
|
|
|
* @return void |
|
73
|
|
|
* @throws \Exception |
|
74
|
|
|
*/ |
|
75
|
2 |
|
private static function install(Application $app, string $config, string $repo) : void |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
$options = [ |
|
78
|
2 |
|
'command' => 'install', |
|
79
|
2 |
|
'--configuration' => $config, |
|
80
|
2 |
|
'--git-directory' => '.git', |
|
81
|
2 |
|
'-f' => '-f' |
|
82
|
|
|
]; |
|
83
|
2 |
|
$app->run(new ArrayInput($options)); |
|
84
|
2 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Return Composer extra config, make sure it is an array |
|
88
|
|
|
* |
|
89
|
|
|
* @param \Composer\Script\Event $event |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
2 |
|
private static function getExtraConfig(Event $event) : array |
|
93
|
|
|
{ |
|
94
|
2 |
|
$extra = $event->getComposer()->getPackage()->getExtra(); |
|
95
|
2 |
|
return is_array($extra) ? $extra : []; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Create a CaptainHook Composer application |
|
100
|
|
|
* |
|
101
|
|
|
* @param \Composer\IO\IOInterface $io |
|
102
|
|
|
* @param string $config |
|
103
|
|
|
* @return \CaptainHook\App\Composer\Application |
|
104
|
|
|
*/ |
|
105
|
2 |
|
private static function createApplication(IOInterface $io, string $config) : Application |
|
106
|
|
|
{ |
|
107
|
2 |
|
$app = new Application(); |
|
108
|
2 |
|
$app->setAutoExit(false); |
|
109
|
2 |
|
$app->setConfigFile($config); |
|
110
|
2 |
|
$app->setProxyIO($io); |
|
111
|
|
|
|
|
112
|
2 |
|
$install = new Install(); |
|
113
|
2 |
|
$install->setIO($app->getIO()); |
|
114
|
2 |
|
$app->add($install); |
|
115
|
|
|
|
|
116
|
2 |
|
$configuration = new Configuration(); |
|
117
|
2 |
|
$configuration->setIO($app->getIO()); |
|
118
|
2 |
|
$app->add($configuration); |
|
119
|
2 |
|
return $app; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Extract a value from an array if not set it returns the given default |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $key |
|
126
|
|
|
* @param array $array |
|
127
|
|
|
* @param string $default |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
2 |
|
private static function extract(string $key, array $array, string $default = '') : string |
|
131
|
|
|
{ |
|
132
|
2 |
|
return isset($array[$key]) ? $array[$key] : $default; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.