1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Magento Version Identification |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* @author Steve Robbins <[email protected]> |
8
|
|
|
* @copyright 2015 Steve Robbins |
9
|
|
|
* @license http://creativecommons.org/licenses/by/4.0/ CC BY 4.0 |
10
|
|
|
* @link https://github.com/steverobbins/magento-version-identification-php |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Mvi\Command; |
14
|
|
|
|
15
|
|
|
use Mvi\Command\MviCommand; |
16
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Generates useful md5 hashes of files in release/ |
22
|
|
|
*/ |
23
|
|
|
class GenerateCommand extends MviCommand |
24
|
|
|
{ |
25
|
|
|
const DIR_RELEASE = 'release'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Folders that have files whos has is of use |
29
|
|
|
* |
30
|
|
|
* @var string[] |
31
|
|
|
*/ |
32
|
|
|
protected $hashFolders = [ |
33
|
|
|
'js', |
34
|
|
|
'media', |
35
|
|
|
'pub/static', |
36
|
|
|
'skin', |
37
|
|
|
'static' |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Patterns of file that are of no use here |
42
|
|
|
* |
43
|
|
|
* @var string[] |
44
|
|
|
*/ |
45
|
|
|
protected $fileIgnorePatterns = [ |
46
|
|
|
"/\.(htaccess|php)$/", |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Configure generate command |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
protected function configure() |
55
|
|
|
{ |
56
|
|
|
$this |
57
|
|
|
->setName('generate') |
58
|
|
|
->setDescription('Generate MD5 hashes from locally stored releases'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Run generate command |
63
|
|
|
* |
64
|
|
|
* @param InputInterface $input |
65
|
|
|
* @param OutputInterface $output |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
70
|
|
|
{ |
71
|
|
|
$releases = $this->getReleases(); |
72
|
|
|
$releaseCount = count($releases); |
73
|
|
|
if ($releaseCount === 0) { |
74
|
|
|
$output->writeln('<error>No releases were found</error>'); |
75
|
|
|
} |
76
|
|
|
$output->writeln(sprintf('Found <info>%d</info> release(s)', $releaseCount)); |
77
|
|
|
$progressBar = new ProgressBar($output, $releaseCount); |
78
|
|
|
$progressBar->setFormat('[%bar%] %percent%% %message%'); |
79
|
|
|
$progressBar->setMessage('Processing...'); |
80
|
|
|
$progressBar->start(); |
81
|
|
|
foreach ($releases as $release) { |
82
|
|
|
$progressBar->setMessage($release); |
83
|
|
|
if (false === $this->generate($release)) { |
84
|
|
|
$output->writeln('<error>Failed to generate hashes</error>'); |
85
|
|
|
} |
86
|
|
|
$progressBar->advance(); |
87
|
|
|
} |
88
|
|
|
$progressBar->setMessage('Done'); |
89
|
|
|
$progressBar->finish(); |
90
|
|
|
$output->writeln(''); |
91
|
|
|
$output->writeln('<info>Generation complete</info>'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get list of release folders |
96
|
|
|
* |
97
|
|
|
* @return string[] |
98
|
|
|
*/ |
99
|
|
|
protected function getReleases() |
100
|
|
|
{ |
101
|
|
|
$names = []; |
102
|
|
|
foreach (glob($this->baseDir . DS . self::DIR_RELEASE . DS . '*') as $name) { |
103
|
|
|
$name = explode(DS, $name); |
104
|
|
|
$names[] = $name[count($name) - 1]; |
105
|
|
|
} |
106
|
|
|
return $names; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Generate the the list of hashes for this release |
111
|
|
|
* |
112
|
|
|
* @param string $release |
113
|
|
|
* |
114
|
|
|
* @return integer|boolean |
115
|
|
|
*/ |
116
|
|
|
protected function generate($release) |
117
|
|
|
{ |
118
|
|
|
$hashes = []; |
119
|
|
|
foreach ($this->hashFolders as $folder) { |
120
|
|
|
$files = $this->getFiles($release, $folder); |
121
|
|
|
foreach ($files as $file) { |
122
|
|
|
foreach ($this->fileIgnorePatterns as $pattern) { |
123
|
|
|
if (preg_match($pattern, $file)) { |
124
|
|
|
continue 2; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
$hashes[] = $this->getHash($release, $file); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
return file_put_contents( |
131
|
|
|
$this->baseDir . DS . self::DIR_MD5 . DS . $release, |
132
|
|
|
implode("\n", array_merge($hashes, array(''))) |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Gets all files in a specified folder of a release |
138
|
|
|
* |
139
|
|
|
* @param string $release |
140
|
|
|
* @param string $folder |
141
|
|
|
* |
142
|
|
|
* @return string[] |
143
|
|
|
*/ |
144
|
|
|
protected function getFiles($release, $folder) |
145
|
|
|
{ |
146
|
|
|
$path = $this->baseDir . DS . self::DIR_RELEASE . DS . $release . DS; |
147
|
|
|
$matches = []; |
148
|
|
|
if (is_dir($path . $folder)) { |
149
|
|
|
$directory = new \RecursiveDirectoryIterator($path . $folder . DS); |
150
|
|
|
$iterator = new \RecursiveIteratorIterator($directory); |
151
|
|
|
foreach ($iterator as $file) { |
152
|
|
|
if (in_array($file->getFilename(), array('.', '..'))) { |
153
|
|
|
continue; |
154
|
|
|
} |
155
|
|
|
$matches[] = str_replace($path, '', (string) $file); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
return $matches; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Gets the md5 has of a file in the release |
163
|
|
|
* |
164
|
|
|
* @param string $release |
165
|
|
|
* @param string $file |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
protected function getHash($release, $file) |
170
|
|
|
{ |
171
|
|
|
return md5(file_get_contents($this->baseDir . DS . self::DIR_RELEASE . DS . $release . DS . $file)) |
172
|
|
|
. ' ' . $file; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|