1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CL\ComposerInit\Prompt; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use CL\ComposerInit\GitConfig; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
use Symfony\Component\Console\Helper\DialogHelper; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Ivan Kerin <[email protected]> |
12
|
|
|
* @copyright (c) 2014 Clippings Ltd. |
13
|
|
|
* @license http://spdx.org/licenses/BSD-3-Clause |
14
|
|
|
*/ |
15
|
|
|
class CopyrightPrompt implements PromptInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Github Client |
19
|
|
|
* @var Client |
20
|
|
|
*/ |
21
|
|
|
private $github; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var GitConfig |
25
|
|
|
*/ |
26
|
|
|
private $gitConfig; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param GitConfig $gitConfig |
30
|
|
|
* @param Client $github |
31
|
|
|
*/ |
32
|
1 |
|
public function __construct(GitConfig $gitConfig, Client $github) |
33
|
|
|
{ |
34
|
1 |
|
$this->github = $github; |
35
|
1 |
|
$this->gitConfig = $gitConfig; |
36
|
1 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return GitConfig |
40
|
|
|
*/ |
41
|
1 |
|
public function getGitConfig() |
42
|
|
|
{ |
43
|
1 |
|
return $this->gitConfig; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return Client |
48
|
|
|
*/ |
49
|
1 |
|
public function getGithub() |
50
|
|
|
{ |
51
|
1 |
|
return $this->github; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get default values from different sources |
56
|
|
|
* |
57
|
|
|
* - github organization |
58
|
|
|
* - github user |
59
|
|
|
* - git user |
60
|
|
|
* - file owner |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
3 |
|
public function getDefaults() |
65
|
|
|
{ |
66
|
3 |
|
$defaults = []; |
67
|
3 |
|
$origin = $this->gitConfig->getOrigin(); |
68
|
|
|
|
69
|
3 |
|
if (null !== $origin) { |
70
|
1 |
|
$response = $this->github->get("/repos/{$origin}"); |
71
|
1 |
|
$repo = json_decode($response->getBody(), true); |
72
|
|
|
|
73
|
1 |
|
if (isset($repo['organization'])) { |
74
|
1 |
|
$response = $this->github->get("/orgs/{$repo['organization']['login']}"); |
75
|
1 |
|
$organizaion = json_decode($response->getBody(), true); |
76
|
1 |
|
$defaults []= $organizaion['name']; |
77
|
1 |
|
} |
78
|
|
|
|
79
|
1 |
|
$response = $this->github->get("/users/{$repo['owner']['login']}"); |
80
|
|
|
|
81
|
1 |
|
$owner = json_decode($response->getBody(), true); |
82
|
1 |
|
$defaults []= $owner['name']; |
83
|
1 |
|
} |
84
|
|
|
|
85
|
3 |
|
$defaults []= $this->gitConfig->get('user.name'); |
86
|
3 |
|
$defaults []= get_current_user(); |
87
|
|
|
|
88
|
3 |
|
return array_values( |
89
|
3 |
|
array_unique( |
90
|
3 |
|
array_filter( |
91
|
|
|
$defaults |
92
|
3 |
|
) |
93
|
3 |
|
) |
94
|
3 |
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Prepend a string to all items in array |
99
|
|
|
* |
100
|
|
|
* @param string $prepend |
101
|
|
|
* @param array $array |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public function prependToArray($prepend, array $array) |
105
|
|
|
{ |
106
|
1 |
|
return array_map(function($item) use ($prepend) { |
|
|
|
|
107
|
1 |
|
return $prepend.$item; |
108
|
1 |
|
}, $array); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param OutputInterface $output |
113
|
|
|
* @param DialogHelper $dialog |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
1 |
|
public function getValues(OutputInterface $output, DialogHelper $dialog) |
117
|
|
|
{ |
118
|
1 |
|
$defaults = $this->prependToArray( |
119
|
1 |
|
date('Y').', ', |
120
|
1 |
|
$this->getDefaults() |
121
|
1 |
|
); |
122
|
|
|
|
123
|
1 |
|
$value = $dialog->ask( |
124
|
1 |
|
$output, |
125
|
1 |
|
"<info>Copyright</info> ({$defaults[0]}): ", |
126
|
1 |
|
$defaults[0], |
127
|
|
|
$defaults |
128
|
1 |
|
); |
129
|
|
|
|
130
|
|
|
return [ |
131
|
1 |
|
'copyright' => $value, |
132
|
1 |
|
'copyright_entity' => preg_replace('/^\d{4}, +/', '', $value), |
133
|
1 |
|
]; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|