|
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 = $_SERVER['HOME'] . '/.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
|
|
|
file_put_contents($authFile,json_encode([ |
|
80
|
|
|
'api' => $api, |
|
81
|
|
|
'username' => $username |
|
82
|
|
|
])); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
} else { |
|
86
|
|
|
|
|
87
|
|
|
$auth = file_get_contents($authFile); |
|
88
|
|
|
|
|
89
|
|
|
$auth = json_decode($auth, true); |
|
90
|
|
|
|
|
91
|
|
|
$command->line(""); |
|
92
|
|
|
|
|
93
|
|
|
$command->line("Packagist API : ". $auth['api']); |
|
94
|
|
|
|
|
95
|
|
|
$command->line('Packagist Username : '. $auth['username']); |
|
96
|
|
|
|
|
97
|
|
|
$command->note( |
|
98
|
|
|
'If you want change your details type `josh hook:setup --clear` ' |
|
99
|
|
|
); |
|
100
|
|
|
|
|
101
|
|
|
$api = $auth['api']; |
|
102
|
|
|
|
|
103
|
|
|
$username = $auth['username']; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$this->sendRequest($command,$api,$username); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Send Request to packagist |
|
111
|
|
|
* |
|
112
|
|
|
* @param Style $command |
|
113
|
|
|
* @param $api |
|
114
|
|
|
* @param $username |
|
115
|
|
|
*/ |
|
116
|
|
|
public function sendRequest(Style $command, $api, $username) |
|
117
|
|
|
{ |
|
118
|
|
|
$package = $command->addInput('Enter your package-url'); |
|
119
|
|
|
|
|
120
|
|
|
if(empty(trim($package))) { |
|
121
|
|
|
$command->error("Package-url is required !"); |
|
122
|
|
|
exit; |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$command->info("Sending request ..."); |
|
126
|
|
|
|
|
127
|
|
|
$data = [ 'repository' => [ 'url' => $package ] ]; |
|
128
|
|
|
|
|
129
|
|
|
$client = new Guzzle([ |
|
130
|
|
|
'base_uri' => 'https://packagist.org/', |
|
131
|
|
|
'headers' => [ |
|
132
|
|
|
'Accept' => 'application/json' |
|
133
|
|
|
] |
|
134
|
|
|
]); |
|
135
|
|
|
|
|
136
|
|
|
$result = $client->post('/api/update-package?username=' . $username . |
|
137
|
|
|
'&apiToken=' . $api,[ 'json' => $data ]); |
|
138
|
|
|
|
|
139
|
|
|
$result = json_decode($result->getBody()->getContents(), true); |
|
140
|
|
|
|
|
141
|
|
|
if($result['status'] !== 'success') { |
|
142
|
|
|
$command->error("Message : ".$result['message']." | Failed "); |
|
143
|
|
|
exit; |
|
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$command->info("Success"); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: