1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Packagist hook setup |
5
|
|
|
* |
6
|
|
|
* @author Alireza Josheghani <[email protected]> |
7
|
|
|
* @since 17 Nov 2016 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Josh\Console\Commands; |
11
|
|
|
|
12
|
|
|
use GuzzleHttp\Client as Guzzle; |
13
|
|
|
use Josh\Console\ConsoleStyle as Style; |
14
|
|
|
use Symfony\Component\Console\Command\Command; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
class HookSetupCommand extends Command |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* configure command |
26
|
|
|
* |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
|
|
public function configure() |
30
|
|
|
{ |
31
|
|
|
$this->setName('hook:setup') |
32
|
|
|
->setDescription('Setup hook of your packagist package'); |
33
|
|
|
|
34
|
|
|
$this->addOption('clear' , 'c', InputOption::VALUE_NONE, 'Clear default details'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* execute command |
39
|
|
|
* |
40
|
|
|
* @param InputInterface $input |
41
|
|
|
* @param OutputInterface $output |
42
|
|
|
*/ |
43
|
|
|
protected function execute(InputInterface $input , OutputInterface $output) |
44
|
|
|
{ |
45
|
|
|
$authFile = $this->getHomeDir() . '/.Josh/auth.json'; |
46
|
|
|
|
47
|
|
|
if($input->getOption('clear')){ |
48
|
|
|
unlink($authFile); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$command = new Style($input, $output); |
52
|
|
|
|
53
|
|
|
if(! file_exists($authFile)){ |
54
|
|
|
|
55
|
|
|
$username = $command->addInput('Enter your packagist username'); |
56
|
|
|
|
57
|
|
|
if(empty($username)) { |
58
|
|
|
$command->error("Packagist username is required !"); |
59
|
|
|
exit; |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$api = $command->addInput('Enter your packagist API'); |
63
|
|
|
|
64
|
|
|
if(empty($api)) { |
65
|
|
|
$command->error("Packagist API is required !"); |
66
|
|
|
exit; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$helper = $this->getHelper('question'); |
70
|
|
|
|
71
|
|
|
$question = new ConfirmationQuestion( |
72
|
|
|
'Are you wanna to save this details of default ? [ Yes or No ] :', |
73
|
|
|
['yes' , 'no'] |
|
|
|
|
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$answer = $helper->ask($input, $output, $question); |
77
|
|
|
|
78
|
|
|
if($answer){ |
79
|
|
|
touch($authFile); |
80
|
|
|
chmod($authFile,777); |
81
|
|
|
file_put_contents($authFile,json_encode([ |
82
|
|
|
'api' => $api, |
83
|
|
|
'username' => $username |
84
|
|
|
])); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} else { |
88
|
|
|
|
89
|
|
|
$auth = file_get_contents($authFile); |
90
|
|
|
|
91
|
|
|
$auth = json_decode($auth, true); |
92
|
|
|
|
93
|
|
|
$command->line(""); |
94
|
|
|
|
95
|
|
|
$command->line("Packagist API : ". $auth['api']); |
96
|
|
|
|
97
|
|
|
$command->line('Packagist Username : '. $auth['username']); |
98
|
|
|
|
99
|
|
|
$command->note( |
100
|
|
|
'If you want change your details type `josh hook:setup --clear` ' |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$api = $auth['api']; |
104
|
|
|
|
105
|
|
|
$username = $auth['username']; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->sendRequest($command,$api,$username); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Send Request to packagist |
113
|
|
|
* |
114
|
|
|
* @param Style $command |
115
|
|
|
* @param $api |
116
|
|
|
* @param $username |
117
|
|
|
*/ |
118
|
|
|
public function sendRequest(Style $command, $api, $username) |
119
|
|
|
{ |
120
|
|
|
$package = $command->addInput('Enter your package-url'); |
121
|
|
|
|
122
|
|
|
if(empty(trim($package))) { |
123
|
|
|
$command->error("Package-url is required !"); |
124
|
|
|
exit; |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$command->info("Sending request ..."); |
128
|
|
|
|
129
|
|
|
$data = [ 'repository' => [ 'url' => $package ] ]; |
130
|
|
|
|
131
|
|
|
$client = new Guzzle([ |
132
|
|
|
'base_uri' => 'https://packagist.org/', |
133
|
|
|
'headers' => [ |
134
|
|
|
'Accept' => 'application/json' |
135
|
|
|
] |
136
|
|
|
]); |
137
|
|
|
|
138
|
|
|
$result = $client->post('/api/update-package?username=' . $username . |
139
|
|
|
'&apiToken=' . $api,[ 'json' => $data ]); |
140
|
|
|
|
141
|
|
|
$result = json_decode($result->getBody()->getContents(), true); |
142
|
|
|
|
143
|
|
|
if($result['status'] !== 'success') { |
144
|
|
|
$command->error("Message : ".$result['message']." | Failed "); |
145
|
|
|
exit; |
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$command->info("Success"); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getHomeDir() |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
if(empty($_SERVER['HOME'])){ |
154
|
|
|
return posix_getpwuid(posix_getuid()); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $_SERVER['HOME']; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
} |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.