forked from hechoendrupal/drupal-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleDownloadCommand.php
More file actions
125 lines (101 loc) · 4.46 KB
/
Copy pathModuleDownloadCommand.php
File metadata and controls
125 lines (101 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* @file
* Contains \Drupal\AppConsole\Command\ModuleDownloadCommand.
*/
namespace Drupal\AppConsole\Command;
use Drupal\Core\Archiver\ArchiveTar;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Output\OutputInterface;
class ModuleDownloadCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('module:download')
->setDescription($this->trans('commands.module.install.description'))
->addArgument('module', InputArgument::REQUIRED, $this->trans('commands.module.install.options.module'));
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$client = $this->getHttpClient();
$module = $input->getArgument('module');
// Getting Module page header and parse to get module Node
$output->writeln('[+] <info>' . sprintf($this->trans('commands.module.download.messages.getting-releases'),
implode(',', array($module))) . '</info>');
$response = $client->head('https://www.drupal.org/project/' . $module);
$header_link = explode(";", $response->getHeader('link'));
$project_node = str_replace('<', '', str_replace('>', '', $header_link[0]));
$project_release_d8 = $project_node . '/release?api_version%5B%5D=7234';
// Parse release module page to get Drupal 8 releases
try {
$response = $client->get($project_release_d8);
$html = $response->getBody()->__tostring();
} catch (\Exception $e) {
$output->writeln('[+] <error>' . $e->getMessage() . '</error>');
return;
}
$crawler = new Crawler($html);
$releases = [];
foreach ($crawler->filter('span.file a') as $element) {
if (strpos($element->nodeValue, ".tar.gz") > 0) {
$release_name = str_replace(
'.tar.gz', '',
str_replace(
$module . '-', '', $element->nodeValue
)
);
$releases[$release_name] = $element->nodeValue;
}
}
if (empty($releases)) {
$output->writeln('[+] <error>' . sprintf($this->trans('commands.module.download.messages.no-releases'),
implode(',', array($module))) . '</error>');
return;
}
// List module releases to enable user to select his favorite release
$questionHelper = $this->getQuestionHelper();
$question = new ChoiceQuestion(
'Please select your favorite release',
array_keys($releases),
0
);
$release_selected = $questionHelper->ask($input, $output, $question);
// Start the process to download the zip file of release and copy in contrib folter
$output->writeln(
'[+] <info>' .
sprintf(
$this->trans('commands.module.download.messages.downloading'),
$module,
$release_selected
) .
'</info>'
);
$release_file_path = 'http://ftp.drupal.org/files/projects/' . $module . '-' . $release_selected . '.tar.gz';
// Destination file to download the release
$destination = tempnam(sys_get_temp_dir(), 'console.') . "tar.gz";
try {
$client->get($release_file_path, ['save_to' => $destination]);
// Determine destination folder for contrib modules
$drupalAutoLoad = $this->getHelperSet()->get('drupal-autoload');
$module_contrib_path = $drupalAutoLoad->getDrupalRoot() . "/modules/contrib";
// Create directory if does not exist
if (file_exists(dirname($module_contrib_path))) {
mkdir($module_contrib_path, 0777, true);
}
// Preper release to unzip and untar
$archiver = new ArchiveTar($destination, 'gz');
$archiver->extract($module_contrib_path . '/');
fclose($destination . ".tar.gz");
$output->writeln('[+] <info>' . sprintf($this->trans('commands.module.download.messages.installed'),
$module, $release_selected) . '</info>');
} catch (\Exception $e) {
$output->writeln('[+] <error>' . $e->getMessage() . '</error>');
return;
}
return true;
}
}