-
Notifications
You must be signed in to change notification settings - Fork 2
/
validate_m2_package.php
executable file
·926 lines (816 loc) · 27.8 KB
/
validate_m2_package.php
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
#!/usr/bin/php
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See https://github.com/magento/magento2/blob/develop/COPYING.txt for license details.
*/
/**
* validate_m2_package.php - a script that checks a given M2 zip package to ensure
* it is structured correctly and has all the required files.
*
*
*/
/**
* @global array List of files to check for magento2-module packages.
*
*/
$g_magentoModuleFiles = array(
'etc/module.xml'
);
/**
* @global array List of files to check for magento2-theme packages.
*
*/
$g_themeFiles = array(
'theme.xml'
);
/**
* @global array List of files to check for magento2-theme packages.
*
*/
$g_langFiles = array(
'language.xml'
);
/**
* @global array List of accepted package types to be validated in composer.json
* type field. The value here is not used.
*
*/
$g_moduleTypes = array(
'metapackage' => true,
'magento2-module' => true,
'magento2-theme' => true,
'magento2-language' => true
);
/**
* @global array List of Magento components or modules that an Extension developer must not depend on
* in the composer.json. The value here is not used.
*
*/
$g_invalidDependencies = array(
'magento/magento2-base' => true,
'magento/magento2-ee-base' => true,
'magento/product-community-edition' => true,
'magento/product-enterprise-edition' => true
);
main($argc, $argv);
/**
* Main entry point: process arguments and invokes validateM2Zip()
*
* It calls exit() with the following integer codes:
*
* 0 - Success; zip file was scanned and it passed all the checks.
* 1 - No zip file name provided.
* 2 - Some exception with stack trace.
*
* Other codes - @see validateM2Zip() below.
*
* @see usage()
*
* @see validateM2Zip()
*
* @SuppressWarnings(PHPMD.ExitExpression)
*
*/
function main($argc, $argv)
{
$opts = getopt('hd');
if( isset($opts['h']) ) {
usage();
exit(0);
}
if( $argc < 2 ) {
usage();
exit(1);
}
$debug = isset($opts['d']);
$zipFiles = getZipFiles($argv);
if( count($zipFiles) == 0 ) {
fwrite(STDERR, "ERROR: No zip files were detected. Please refer to the usage.\n");
usage();
exit(1);
}
// Exit code is non-zero if any of the supplied zip files
// return a non-zero code.
$rc = 0;
foreach( $zipFiles as $zip ) {
$rc2 = validateM2Zip($zip, $debug);
if($rc2 != 0)
{
$rc = $rc2;
}
}
exit($rc);
}
/**
* Displays usage.
*
* @return void
*
*/
function usage()
{
echo <<<EOF
Usage: validate_m2_package [OPTIONS] <M2 zip file> [<M2 zip file> ...]
-h help
Prints this usage.
-d debug
Optional - prints additional debug messages.
EOF;
}
/**
* Parses the zip files given as arguments
*
* @param array $argv Command Line arguments
*
* @return array $zipFiles Names of the zip files.
*
*/
function getZipFiles($argv)
{
$zipFiles = [];
// Getting rid of the script name
array_shift($argv);
foreach( $argv as $arg ) {
if( $arg == '-d' ) {
continue;
}
if( preg_match("/.*\.zip$/", $arg) ) {
$zipFiles[] = $arg;
}
else {
print "ERROR: \"$arg\" was skipped because it is not of the correct file format (.zip).\n";
}
}
return $zipFiles;
}
/**
* Validates the supplied M2 package zip file.
*
* The core logic starts here - the zip file name is opened and inspected for
* for various checks as described below.
*
* The required files and module directory structure can be at the top-level,
* or one level down from the top level directory. In addition, the required files
* can be in an arbitrary folder (i.e. src/) given that they are mapped correctly in the
* "composer.json", which should reside outside of this folder. If there is a top-level directory,
* it is usually expected to to be the same as the package name with the ".zip" extension.
* So for example, for a package named MyExtension.zip, the required file "composer.json"
* is expected at:
*
* unzip -l MyExtension.zip
* .
* etc/
* composer.json
* .
* .
*
* OR
*
* unzip -l MyExtension.zip
*
* MyExtension/....
* MyExtension/etc/
* MyExtension/composer.json
* MyExtension/....
* MyExtension/....
*
* OR
*
* unzip -l MyExtension.zip
*
* src/
* composer.json
* .
* .
*
* OR
*
* unzip -l MyExtension.zip
*
* MyExtension/src/
* MyExtension/composer.json
* MyExtension/...
* MyExtension/...
*
* The top-level directory name need not match the package name (minus the '.zip' extension)- it
* is noted in such cases.
*
* So whether at top-level, or one level down as illustrated above, it performs the
* following checks:
*
* 1) The supplied file must be a zip file archive.
*
* 2) It should contain a valid composer.json file.
*
* 3) The commposer.json file is inspected for certain fields - @see validateComposerJson()
* below.
*
* 4) Based on the package type, additional files are checked to see if it is present - see
* the list of globals above for each type which lists the files it checks for existence for
* its respective type.
*
* a) $g_magentoModules (magento2-module)
*
* b) $g_themeFiles (magento2-theme)
*
* c) $g_langFiles (magento2-language)
*
* 5) For non-metapackages, it checks to see if registration.php is also present.
*
* Wherever possible, the check continues on and outputs any errors or warnings to the stderr.
*
* @param string $fname The path to the zip file.
* @param boolean $debug Debug flag, which if enabled, adds DEBUG lines to the output.
*
* @return integer The return code to indicate the validation status:
*
* 0 - All checks passed (no errors detected).
*
* 101 - Supplied file not in zip format.
*
* 102 - Some zip archive error; the erorr message contains the archive error code.
*
* 200 - Errors detected during the checks. Specific errors are displayed in stderr.
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*
*/
function validateM2Zip($fname, $debug)
{
$pkgName = basename($fname);
$zip = new ZipArchive;
$res = $zip->open($fname);
if($res !== true)
return processZipErrors($pkgName, $res);
if($debug) {
displayZipArchive($pkgName, $zip);
}
$err = false;
// Check to see if there is a top-level directory.
list($topDir, $numDirs) = getTopDir($zip);
$topDirBasename = basename($topDir);
if($numDirs > 1) {
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": More than one top-level directory detected, " .
"number of directories = $numDirs. Top level directory expected to be the module directory.\n");
return 200;
}
if($debug) {
print "DEBUG - \"" . $pkgName . "\": Top level directory - <$topDirBasename>.\n";
}
$pkgName2 = basename($fname, '.zip');
if( ($numDirs ==1) && ($topDirBasename != $pkgName2) ) {
fwrite(STDERR, "NOTE - \"" . $pkgName ."\": Top-level directory does not match " .
"package name - \"$topDirBasename\" != \"$pkgName2\"\n");
}
// First check to see if composer.json file is present
// in JSON format.
$composerJsonStr = getComposerJson($zip, $topDir);
$composerJson = '';
if($composerJsonStr === false) {
// Is composer.json present anywhere?
$composerFname = locateFile($zip, 'composer.json');
if($composerFname === false) {
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": \"composer.json\" missing. " .
"Please consult the \"Name your component\" section of the PHP Developer Guide for more information.\n");
$err = true;
}
else {
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": \"composer.json\" found in unexpected " .
"place. Zip archive layout not to standard as described in the " .
"\"Component File Structure\" section of the PHP Developer Guide.\n");
$err = true;
}
}
else if($composerJsonStr == '') {
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": Empty \"composer.json\" file detected. " .
"Please consult the \"Name your component\" section of the PHP Developer Guide for more information.\n");
$err = true;
}
else {
$composerJson = json_decode($composerJsonStr, true);
if(is_null($composerJson)) {
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": Bad \"composer.json\" file detected. " .
"Please consult the \"Name your component\" section of the PHP Developer Guide for more information.\n");
$err = true;
}
}
if($debug) {
print "DEBUG - \"" . $pkgName . "\": composer.json\n" . rtrim( str_replace("\n", "\n\t", "\t" . $composerJsonStr), "\t" ) . "\n";
}
$type = '';
// Attempt to find the source folder if it exists
$srcDir = findSourceFolder($composerJson);
if($srcDir !== "") {
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": Alternate folder structure is currently not supported\n");
$err = true;
}
if( is_array($composerJson) ) {
// Ensure all the needed fields are present in the
// composer.json. Based on the type field, ensure
// its respective files are also present.
$type = (string) @$composerJson['type'];
if( !validateComposerJson($pkgName, $composerJson) ) {
$err = true;
}
if( !validateFiles($type, $pkgName, $zip, $topDir, $srcDir) ) {
$err = true;
}
}
// Check for registration.php - skip it for metapackages.
if( ($type != '') && ($type != 'metapackage') ) {
$regPhp = findRegistrationLoad($composerJson);
$hasRegPhp = false;
// If we cannot locate "registration.php" based on the composer.json,
// then we take our best guess as to where it should be.
if($regPhp === false) {
$hasRegPhp = registrationPhpExists($zip, $topDir . $srcDir . "registration.php");
}
else {
$hasRegPhp = registrationPhpExists($zip, $topDir . $regPhp);
}
if( $hasRegPhp === false ) {
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": \"registration.php\" is missing. " .
"Please consult the \"Component Registration\" section of the PHP Developer Guide for more information.\n");
$err = true;
}
elseif( $hasRegPhp <= 0 ) {
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": \"registration.php\" is empty. " .
"Please consult the \"Component Registration\" section of the PHP Developer Guide for more information.\n");
$err = true;
}
}
if($err)
return 200;
if($debug) {
print "DEBUG - \"" . $pkgName . "\": Success, passed all the validation checks.\n";
}
return 0;
}
/**
* Handle zip archive error codes.
*
* Helper function to report zip archive errors.
*
* @param string $pkgName Name of the zip file.
* @param integer $res ZipArchive::open() return code.
*
* @return integer See codes below.
*
*/
function processZipErrors($pkgName, $res)
{
switch($res) {
case ZipArchive::ER_NOZIP:
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": Supplied file not in zip format. " .
"Please consult the \"Package a component\" section of the PHP Developer Guide for more information.\n");
return 101;
default:
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": Zip file open failure with code $res.\n");
return 102;
}
}
/**
* Extracts composer.json from the zip archive file.
*
* It looks for the composer.json at the top level or under the
* top level directory if set.
*
* @param object $zip ZipArchive object
* @param string $topDir Top level directory if present.
*
* @return string composer.json contents
*
*/
function getComposerJson($zip, $topDir)
{
$fname = $topDir . 'composer.json';
return $zip->getFromName($fname);
}
/**
* Checks to see if the extension uses an alternate file structure
*
* @param array $composerJson
*
* @return string filepath of the "source" folder, or empty string if none was found.
*
*/
function findSourceFolder($composerJson)
{
$srcFolder = @current(@$composerJson['autoload']['psr-4']);
if(is_null($srcFolder)) {
$pathParts = pathinfo(@current(@$composerJson['autoload']['files']));
$dirname = @$pathParts['dirname'];
if(is_null($dirname) || ($dirname === '.')) {
return '';
}
return (string) $pathParts['dirname'] . '/';
}
return (string) @current(@$composerJson['autoload']['psr-4']);
}
/**
* Checks to see if registration.php exists in the autoload files section
*
* @param array $composerJson
* @param boolean $isAutoloadSection
*
* @return boolean/string Location of registration.php, false otherwise
*
*/
function findRegistrationLoad($composerJson, $isAutoloadSection = false)
{
if($isAutoloadSection) {
$files = @$composerJson['files'];
}
else {
$files = @$composerJson['autoload']['files'];
}
if(!is_null($files)) {
$registrationFile = 'registration.php';
$registrationFileStringLength = strlen($registrationFile);
foreach ($files as $file) {
if (substr($file, -1*$registrationFileStringLength) === $registrationFile) {
return $file;
}
}
}
return false;
}
/**
* Checks to see if registration.php exists in the supplied zip file.
*
* It looks for registration.php at the top-level or under the top level if
* set. It also takes into account the source folder, if it exists.
* It should be a non-empty file.
*
* @param object $zip ZipArchive object
* @param string $topDir Top level directory if present.
*
* @return boolean/integer Size of registration.php, false otherwise.
*
*/
function registrationPhpExists($zip, $regPhp)
{
//TODO: Do 'php -l registration.php' to detect syntax errors.
$stat = $zip->statName($regPhp);
if($stat === false)
return false;
return $stat['size'];
}
/**
* Validates the composer.json required field and values.
*
* It inspects and validates the following required fields:
*
* name
* type
* version
* autoload - only for non-metapackages
* require
*
* See comments below for the expected format of the values. Any
* errors detected here are reported to the stdout.
*
* @param string $pkgName Name of the supplied zip file.
* @param array $composerJson Json decoded composer.json contents.
* @return boolean True if all validations succeeded, false otherwise.
*
* @see validateComposerAutoload()
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*
*/
function validateComposerJson($pkgName, $composerJson)
{
global $g_moduleTypes;
$name = (string) @$composerJson['name'];
$type = (string) @$composerJson['type'];
$version = (string) @$composerJson['version'];
$autoload = @$composerJson['autoload'];
$require = @$composerJson['require'];
$extra = @$composerJson['extra'];
$res = true;
$knownType = true;
// name - must be of the format '<vendor>/<package name>'
if( !preg_match("/^([a-z0-9_-])+\/([a-z0-9_-])+$/i", $name) ) {
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": \"composer.json\" has invalid name - " .
"\"$name\". It should be of the format '<vendor>/<package name>'.\n");
$res = false;
}
// type - must be in the known types list.
if($type == '') {
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": The 'type' field in \"composer.json\" is " .
"missing or empty. The 'type' field is required and can only be one of the following: " .
"magento2-theme, magento2-language, or magento2-module.\n");
$res = false;
}
else if( !isset($g_moduleTypes[$type]) ) {
// unknown type
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": Unknown 'type' detected - '$type'. " .
"The 'type' field is required and can only be one of the following: " .
"magento2-theme, magento2-language, or magento2-module.\n");
$res = false;
$knownType = false;
}
// version
// Expected format as per https://getcomposer.org/doc/04-schema.md#version
if( !preg_match("/^(|v)([0-9])+\.([0-9])+\.([0-9])+" .
"(-(patch|p|dev|a|alpha|b|beta|rc)([0-9])*)?$/i", $version)
) {
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": The 'version' field in \"composer.json\" " .
"is missing, empty or not in expected format. The 'version' field is required and needs " .
"to be of the following form as described here: " .
"https://getcomposer.org/doc/04-schema.md#version .\n");
$res = false;
}
if( $knownType && ($type != 'metapackage') ) {
// autoload check - not applicable to metapackage
if(!validateComposerAutoload($type, $pkgName, $autoload)) {
$res = false;
}
}
if(!validateComposerDependencies($type, $pkgName, $require)) {
$res = false;
}
if( isset($extra) ) {
// extra['map'] is deprecated and should not be present anymore.
if( isset($extra['map']) ) {
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": The \"extra['map']\" field is deprecated; " .
"it should not be present anymore.\n");
$res = false;
}
// extra['magento-root-dir'] is deprecated and should not be present anymore.
if( isset($extra['magento-root-dir']) ) {
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": The \"extra['magento-root-dir'] field is deprecated; " .
"it should not be present anymore.\n");
$res = false;
}
}
return $res;
}
/**
* Helper function to validate composer.json autoload field value.
*
* It ensures that the following fields are set:
*
* files - the list here must contain registration.php
* psr-4 - ensures that it is a non-empty list with namespace
* properly set.
*
* Any errors detected here are reported to the stdout.
*
* @param string $type The module type from the composer.json 'type' field.
* @param string $pkgName Name of the supplied zip file.
* @param array $autoload Map contents of the autoload field from composer.json.
*
* @return boolean True if all validations succeeded, false otherwise.
*
*/
function validateComposerAutoload($type, $pkgName, $autoload)
{
$res = true;
if(is_array($autoload)) {
$files = @$autoload['files'];
$psr4 = @$autoload['psr-4'];
if( is_array($files) && (count($files) > 0) ) {
if(!findRegistrationLoad($autoload, true)) {
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": \"registration.php\" not found in " .
"'files' field of the 'autoload' directive. Please consult the \"Component registration\" section of the " .
"PHP Developer Guide for more information.\n");
$res = false;
}
}
else {
// the 'files' field is what's being referenced.
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": The 'files' field of the 'autoload' " .
"directive is missing or not set up correctly. Please consult the \"Component registration\" section of the " .
"PHP Developer Guide for more information.\n");
$res = false;
}
// Currently psr-4 check is only valid for 'magento2-module'.
//
//TODO: Can the namespace setting here be actually verified?
if( ($type == 'magento2-module') && (!is_array($psr4) || (count($psr4) <= 0)) ) {
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": The 'psr4' field of the 'autoload' " .
"directive is missing or not set up correctly. Please consult the \"Component registration\" section of the " .
"PHP Developer Guide for more information.\n");
$res = false;
}
}
else {
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": The 'autoload' directive is missing or not " .
"set up correctly. Please consult the \"Component registration\" section of the PHP Developer Guide for more " .
"information.\n");
$res = false;
}
return $res;
}
/**
* Helper function to validate composer.json require field value.
*
* It ensures that invalid packages and/or dependencies are not specified.
*
* Any errors detected here are reported to the stdout.
*
* @param string $type The module type from the composer.json 'type' field.
* @param string $pkgName Name of the supplied zip file.
* @param array $require List of dependencies specified in require field from composer.json.
*
* @return boolean True if all validations succeeded, false otherwise.
*
*/
function validateComposerDependencies($type, $pkgName, $require)
{
global $g_invalidDependencies;
$res = true;
if(is_array($require) && (count($require) > 0)) {
foreach($require as $package => $version) {
if(isset($g_invalidDependencies[$package])) {
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": The '$package' package must not be specified as a dependency.\n");
$res = false;
}
else if(preg_match("/^magento\//", $package) && ($version === '*')) {
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": The '$package' must have specific version(s) as a dependency.\n");
$res = false;
}
else if($package === 'php') {
fwrite(STDERR, "WARNING - \"" . $pkgName . "\": It is not recommended to specify supported php versions in dependencies. " .
"If specified, it must at least match the php requirements of the supported Magento versions.\n");
}
}
}
else if($type == 'metapackage') {
// metapackages should have non-empty require directive
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": The 'require' directive in " .
"\"composer.json\" is missing, empty, or incorrect. Please consult the \"Package a component\" section " .
"of the PHP Developer Guide for more information.\n");
$res = false;
}
return $res;
}
/**
* Validate that certain needed files exist for a given package type.
*
* It ensures that for a given package type (non-metapackage), its corresponding
* needed files are present and non-empty. This is a driver function to
* map the needed files for a given package type. The actual check is done
* at validateFilesCore()
*
* @param string $type The type field from composer.json
* @param string $pkgName Name of the supplied zip file.
* @param object $zip The ZipArchive object
* @param string $topDir The top level directory if present in the zip.
*
* @return boolean True if all validations succeeded, false otherwise.
*
* @see validateFilesCore()
*
*/
function validateFiles($type, $pkgName, $zip, $topDir, $srcDir = '')
{
global $g_magentoModuleFiles;
global $g_themeFiles;
global $g_langFiles;
//TODO: for the various file checks below, right now it does
// only existence check (size > 0). It will be nice to
// also do additional validation - e.g. etc/module.xml can
// be validated to ensure it is correct for magento2-module.
$res = true;
switch($type) {
case 'magento2-module':
$res = validateFilesCore($type, $pkgName, $g_magentoModuleFiles, $zip, $topDir, $srcDir);
break;
case 'magento2-theme':
$res = validateFilesCore($type, $pkgName, $g_themeFiles, $zip, $topDir);
break;
case 'magento2-language':
$res = validateFilesCore($type, $pkgName, $g_langFiles, $zip, $topDir);
break;
case 'metapackage':
default: // unknown types are handled earlier in composer validation.
break;
}
return $res;
}
/**
* Helper function to check existence of non-empty files.
*
* The list of files for a given package type is supplied, and
* for each of them, it checks to see if the file exists and
* it is not empty in the given zip file.
*
* @param string $type The type of package
* @param string $pkgName Name of the supplied zip file.
* @param array $files The list of files to check.
* @param ZipArchive $zip The zip file package to inspect.
* @param string $topDir The top level directory if present in the zip.
* @param string $srcDir The source folder if it is present
*
* @return boolean True if all the files in the list is present, false otherwise.
*
*/
function validateFilesCore($type, $pkgName, $files, $zip, $topDir, $srcDir = '')
{
$res = true;
foreach($files as $f) {
$f = $topDir . $srcDir . $f;
$stat = $zip->statName($f);
if($stat === false) {
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": \"$f\" is missing. " .
"\"$f\" is a required file for the '$type' type.\n");
$res = false;
}
else if( $stat['size'] <= 0 ) {
fwrite(STDERR, "ERROR - \"" . $pkgName . "\": \"$f\" is empty. " .
"\"$f\" is a required file for the '$type' type.\n");
$res = false;
}
}
return $res;
}
/**
* Method to extract the top level directory if present in the zip archive.
*
* A supplied package can either be archived from the 'main directory'
* where composer.json, and registration.php (if applicable) exists, or
* these mandatory files can be one level down, i.e. the zip file has
* a top level directory. For an example, see comments in validateM2Zip()
* function above.
*
* @param ZipArchive $zip The zip package to inspect.
*
* @return array The following values (tuple) are returned in the given offsets:
* 0 - string Top level directory is present; can be an empty string.
* 1 - integer Number of top-level directories detected.
*
*
*/
function getTopDir($zip)
{
$topDirs = array();
for($i = 0; $i < $zip->numFiles; ++$i) {
$fname = $zip->getNameIndex($i);
$pos = strpos($fname, '/');
if($pos === false) {
// Any regular files on the top-level implies that there is either no top level directory
// (i.e. composer.json etc.. is at the top level), or there are spurious files at the top level
// which is also not expected.
return array('', 0);
}
$topDir = substr($fname, 0, $pos + 1);
$topDirs[$topDir] = $topDir;
}
// Now if there are more that one top level directory, it is not in the expected format.
$numDirs = count($topDirs);
if($numDirs != 1) {
return array('', $numDirs);
}
return array( array_shift($topDirs), 1);
}
/**
* Debug routine to dump the zip archive.
*
* Displays the file names and its respective sizes
* for debugging purposes.
*
* @param string $pkgName Name of the supplied zip file.
* @param ZipArchive $zip The zip file to dump.
*
* @return void
*
*/
function displayZipArchive($pkgName, $zip)
{
print "DEBUG - \"" . $pkgName . "\": Zip file contents (file and size).\n";
for($i = 0; $i < $zip->numFiles; ++$i) {
$fname = $zip->getNameIndex($i);
$stat = $zip->statName($fname);
print "\t$fname - " . $stat['size'] . "\n";
}
}
/**
* Locate a file in the zip archive.
*
* The supplied file name is checked against all
* the file path locations in the zip archive at
* any depth level.
*
* @param ZipArchive $zip The zip file to inspect.
* @param string $fname The file name to check.
*
* @return mix Returns full path to the file if found, or false otherwise.
*
*/
function locateFile($zip, $fname)
{
for($i = 0; $i < $zip->numFiles; ++$i) {
$fname2 = $zip->getNameIndex($i);
if($fname == basename($fname2)) {
return $fname2;
}
}
return false;
}