/
Files.php
1573 lines (1480 loc) · 51.8 KB
/
Files.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
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\App\Utility;
use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Component\DirSearch;
use Magento\Framework\View\Design\Theme\ThemePackageList;
use Magento\Framework\Filesystem\Glob;
/**
* A helper to gather specific kind of files in Magento application
*
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
class Files
{
/**@#+
* File types offset flags
*/
const INCLUDE_APP_CODE = 1;
const INCLUDE_TESTS = 2;
const INCLUDE_DEV_TOOLS = 4;
const INCLUDE_TEMPLATES = 8;
const INCLUDE_LIBS = 16;
const INCLUDE_PUB_CODE = 32;
const INCLUDE_NON_CLASSES = 64;
/**#@-*/
/**
* Return as DataSet offset flag
*/
const AS_DATA_SET = 1024;
/**
* Component registrar
*
* @var ComponentRegistrar
*/
protected $componentRegistrar;
/**
* @var \Magento\Framework\App\Utility\Files
*/
protected static $_instance = null;
/**
* In-memory cache for the data sets
*
* @var array
*/
protected static $_cache = [];
/**
* Dir search for registered components
*
* @var DirSearch
*/
private $dirSearch;
/**
* Theme list for registered themes
*
* @var ThemePackageList
*/
private $themePackageList;
/**
* Setter for an instance of self
*
* Also can unset the current instance, if no arguments are specified
*
* @param Files|null $instance
* @return void
*/
public static function setInstance(Files $instance = null)
{
self::$_instance = $instance;
}
/**
* Getter for an instance of self
*
* @return \Magento\Framework\App\Utility\Files
* @throws \Exception when there is no instance set
*/
public static function init()
{
if (!self::$_instance) {
throw new \Exception('Instance is not set yet.');
}
return self::$_instance;
}
/**
* Compose PHPUnit's data sets that contain each file as the first argument
*
* @param array $files
* @return array
*/
public static function composeDataSets(array $files)
{
$result = [];
foreach ($files as $file) {
$result[$file] = [$file];
}
return $result;
}
/**
* Set path to source code
*
* @param ComponentRegistrar $componentRegistrar
* @param DirSearch $dirSearch
* @param ThemePackageList $themePackageList
*/
public function __construct(
ComponentRegistrar $componentRegistrar,
DirSearch $dirSearch,
ThemePackageList $themePackageList
) {
$this->componentRegistrar = $componentRegistrar;
$this->dirSearch = $dirSearch;
$this->themePackageList = $themePackageList;
}
/**
* Get list of regular expressions for matching test directories in modules
*
* @return array
*/
private function getModuleTestDirsRegex()
{
$moduleTestDirs = [];
foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleDir) {
$moduleTestDirs[] = str_replace('\\', '/', '#' . $moduleDir . '/Test#');
}
return $moduleTestDirs;
}
/**
* Get base path
*
* @return string
*/
public function getPathToSource()
{
return BP;
}
/**
* Returns list of files, where expected to have class declarations
*
* @param int $flags
* @return array
*/
public function getPhpFiles($flags = 0)
{
// Sets default value
if ($flags === 0) {
$flags = self::INCLUDE_APP_CODE
| self::INCLUDE_TESTS
| self::INCLUDE_DEV_TOOLS
| self::INCLUDE_LIBS
| self::AS_DATA_SET;
}
$key = __METHOD__ . BP . $flags;
if (!isset(self::$_cache[$key])) {
$files = [];
$files = array_merge($files, $this->getAppCodeFiles($flags));
$files = array_merge($files, $this->getTestFiles($flags));
$files = array_merge($files, $this->getDevToolsFiles($flags));
$files = array_merge($files, $this->getTemplateFiles($flags));
$files = array_merge($files, $this->getLibraryFiles($flags));
$files = array_merge($files, $this->getPubFiles($flags));
self::$_cache[$key] = $files;
}
if ($flags & self::AS_DATA_SET) {
return self::composeDataSets(self::$_cache[$key]);
}
return self::$_cache[$key];
}
/**
* Return array with all template files
*
* @param int $flags
* @return array
*/
private function getTemplateFiles($flags)
{
if ($flags & self::INCLUDE_TEMPLATES) {
return $this->getPhtmlFiles(false, false);
}
return [];
}
/**
* Return array with all php files related to library
*
* @param int $flags
* @return array
*/
private function getLibraryFiles($flags)
{
if ($flags & self::INCLUDE_LIBS) {
$libraryExcludeDirs = [];
foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::LIBRARY) as $libraryDir) {
$libraryExcludeDirs[] = str_replace('\\', '/', '#' . $libraryDir . '/Test#');
$libraryExcludeDirs[] = str_replace('\\', '/', '#' . $libraryDir) . '/[\\w]+/Test#';
if (!($flags & self::INCLUDE_NON_CLASSES)) {
$libraryExcludeDirs[] = str_replace('\\', '/', '#' . $libraryDir . '/registration#');
}
}
return $this->getFilesSubset(
$this->componentRegistrar->getPaths(ComponentRegistrar::LIBRARY),
'*.php',
$libraryExcludeDirs
);
}
return [];
}
/**
* Return array with all php files related to pub
*
* @param int $flags
* @return array
*/
private function getPubFiles($flags)
{
if ($flags & self::INCLUDE_PUB_CODE) {
return array_merge(
Glob::glob(BP . '/*.php', Glob::GLOB_NOSORT),
Glob::glob(BP . '/pub/*.php', Glob::GLOB_NOSORT)
);
}
return [];
}
/**
* Return array with all php files related to dev tools
*
* @param int $flags
* @return array
*/
private function getDevToolsFiles($flags)
{
if ($flags & self::INCLUDE_DEV_TOOLS) {
return $this->getFilesSubset([BP . '/dev/tools/Magento'], '*.php', []);
}
return [];
}
/**
* Return array with all php files related to modules
*
* @param int $flags
* @return array
*/
private function getAppCodeFiles($flags)
{
if ($flags & self::INCLUDE_APP_CODE) {
$excludePaths = [];
$paths = $this->componentRegistrar->getPaths(ComponentRegistrar::MODULE);
if ($flags & self::INCLUDE_NON_CLASSES) {
$paths[] = BP . '/app';
} else {
foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleDir) {
$excludePaths[] = str_replace('\\', '/', '#' . $moduleDir . '/registration.php#');
$excludePaths[] = str_replace('\\', '/', '#' . $moduleDir . '/cli_commands.php#');
}
}
return $this->getFilesSubset(
$paths,
'*.php',
array_merge($this->getModuleTestDirsRegex(), $excludePaths)
);
}
return [];
}
/**
* Return array with all test files
*
* @param int $flags
* @return array
*/
private function getTestFiles($flags)
{
if ($flags & self::INCLUDE_TESTS) {
$testDirs = [
BP . '/dev/tests',
BP . '/setup/src/Magento/Setup/Test',
];
$moduleTestDir = [];
foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleDir) {
$moduleTestDir[] = $moduleDir . '/Test';
}
$libraryTestDirs = [];
foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::LIBRARY) as $libraryDir) {
$libraryTestDirs[] = $libraryDir . '/Test';
$libraryTestDirs[] = $libraryDir . '/*/Test';
}
$testDirs = array_merge($testDirs, $moduleTestDir, $libraryTestDirs);
return self::getFiles($testDirs, '*.php');
}
return [];
}
/**
* Returns list of xml files, used by Magento application
*
* @return array
*/
public function getXmlFiles()
{
return array_merge(
$this->getMainConfigFiles(),
$this->getLayoutFiles(),
$this->getPageLayoutFiles(),
$this->getConfigFiles(),
$this->getDiConfigs(true),
$this->getLayoutConfigFiles(),
$this->getPageTypeFiles()
);
}
/**
* Retrieve all config files, that participate (or have a chance to participate) in composing main config
*
* @param bool $asDataSet
* @return array
*/
public function getMainConfigFiles($asDataSet = true)
{
$cacheKey = __METHOD__ . '|' . serialize(func_get_args());
if (!isset(self::$_cache[$cacheKey])) {
$configXmlPaths = [];
foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleDir) {
$configXmlPaths[] = $moduleDir . '/etc/config.xml';
// Module DB-specific configs, e.g. config.mysql4.xml
$configXmlPaths[] = $moduleDir . '/etc/config.*.xml';
}
$globPaths = [BP . '/app/etc/config.xml', BP . '/app/etc/*/config.xml'];
$configXmlPaths = array_merge($globPaths, $configXmlPaths);
$files = [];
foreach ($configXmlPaths as $xmlPath) {
$files = array_merge($files, glob($xmlPath));
}
self::$_cache[$cacheKey] = $files;
}
if ($asDataSet) {
return self::composeDataSets(self::$_cache[$cacheKey]);
}
return self::$_cache[$cacheKey];
}
/**
* Returns list of configuration files, used by Magento application
*
* @param string $fileNamePattern
* @param array $excludedFileNames
* @param bool $asDataSet
* @return array
* @codingStandardsIgnoreStart
*/
public function getConfigFiles(
$fileNamePattern = '*.xml',
$excludedFileNames = ['wsdl.xml', 'wsdl2.xml', 'wsi.xml'],
$asDataSet = true
) {
$cacheKey = __METHOD__ . '|' . serialize(func_get_args());
if (!isset(self::$_cache[$cacheKey])) {
$files = $this->dirSearch->collectFiles(ComponentRegistrar::MODULE, "/etc/{$fileNamePattern}");
$files = array_filter(
$files,
function ($file) use ($excludedFileNames) {
return !in_array(basename($file), $excludedFileNames);
}
);
self::$_cache[$cacheKey] = $files;
}
if ($asDataSet) {
return self::composeDataSets(self::$_cache[$cacheKey]);
}
return self::$_cache[$cacheKey];
}
// @codingStandardsIgnoreEnd
/**
* Returns list of XML related files, used by Magento application
*
* @param string $fileNamePattern
* @param array $excludedFileNames
* @param bool $asDataSet
* @return array
*/
public function getXmlCatalogFiles(
$fileNamePattern = '*.xsd',
$excludedFileNames = [],
$asDataSet = true
) {
$cacheKey = __METHOD__ . '|' . serialize(func_get_args());
if (!isset(self::$_cache[$cacheKey])) {
$files = $this->getFilesSubset(
$this->componentRegistrar->getPaths(ComponentRegistrar::MODULE),
$fileNamePattern,
[]
);
$libraryExcludeDirs = [];
foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::LIBRARY) as $libraryDir) {
$libraryExcludeDirs[] = str_replace('\\', '/', '#' . $libraryDir . '/Test#');
$libraryExcludeDirs[] = str_replace('\\', '/', '#' . $libraryDir) . '/[\\w]+/Test#';
}
$files = array_merge(
$files,
$this->getFilesSubset(
$this->componentRegistrar->getPaths(ComponentRegistrar::LIBRARY),
$fileNamePattern,
$libraryExcludeDirs
)
);
$files = array_merge(
$files,
$this->getFilesSubset(
$this->componentRegistrar->getPaths(ComponentRegistrar::THEME),
$fileNamePattern,
[]
)
);
$files = array_filter(
$files,
function ($file) use ($excludedFileNames) {
return !in_array(basename($file), $excludedFileNames);
}
);
self::$_cache[$cacheKey] = $files;
}
if ($asDataSet) {
return self::composeDataSets(self::$_cache[$cacheKey]);
}
return self::$_cache[$cacheKey];
}
/**
* Returns a list of configuration files found under theme directories.
*
* @param string $fileNamePattern
* @param bool $asDataSet
* @return array
*/
public function getLayoutConfigFiles($fileNamePattern = '*.xml', $asDataSet = true)
{
$cacheKey = __METHOD__ . '|' . serialize(func_get_args());
if (!isset(self::$_cache[$cacheKey])) {
self::$_cache[$cacheKey] = $this->dirSearch->collectFiles(
ComponentRegistrar::THEME,
"/etc/{$fileNamePattern}"
);
}
if ($asDataSet) {
return self::composeDataSets(self::$_cache[$cacheKey]);
}
return self::$_cache[$cacheKey];
}
/**
* Returns list of page configuration and generic layout files, used by Magento application modules
*
* An incoming array can contain the following items
* array (
* 'namespace' => 'namespace_name',
* 'module' => 'module_name',
* 'area' => 'area_name',
* 'theme' => 'theme_name',
* 'include_code' => true|false,
* 'include_design' => true|false,
* 'with_metainfo' => true|false,
* )
*
* @param array $incomingParams
* @param bool $asDataSet
* @return array
*/
public function getLayoutFiles($incomingParams = [], $asDataSet = true)
{
return $this->getLayoutXmlFiles('layout', $incomingParams, $asDataSet);
}
/**
* Returns list of page layout files, used by Magento application modules
*
* An incoming array can contain the following items
* array (
* 'namespace' => 'namespace_name',
* 'module' => 'module_name',
* 'area' => 'area_name',
* 'theme' => 'theme_name',
* 'include_code' => true|false,
* 'include_design' => true|false,
* 'with_metainfo' => true|false,
* )
*
* @param array $incomingParams
* @param bool $asDataSet
* @return array
*/
public function getPageLayoutFiles($incomingParams = [], $asDataSet = true)
{
return $this->getLayoutXmlFiles('page_layout', $incomingParams, $asDataSet);
}
/**
* @param string $location
* @param array $incomingParams
* @param bool $asDataSet
* @return array
*/
protected function getLayoutXmlFiles($location, $incomingParams = [], $asDataSet = true)
{
$params = [
'namespace' => '*',
'module' => '*',
'area' => '*',
'theme_path' => '*/*',
'include_code' => true,
'include_design' => true,
'with_metainfo' => false
];
foreach (array_keys($params) as $key) {
if (isset($incomingParams[$key])) {
$params[$key] = $incomingParams[$key];
}
}
$cacheKey = md5($location . '|' . implode('|', $params));
if (!isset(self::$_cache[__METHOD__][$cacheKey])) {
$files = [];
if ($params['include_code']) {
$files = array_merge($files, $this->collectModuleLayoutFiles($params, $location));
}
if ($params['include_design']) {
$files = array_merge($files, $this->collectThemeLayoutFiles($params, $location));
}
self::$_cache[__METHOD__][$cacheKey] = $files;
}
if ($asDataSet) {
return self::composeDataSets(self::$_cache[__METHOD__][$cacheKey]);
}
return self::$_cache[__METHOD__][$cacheKey];
}
/**
* Collect layout files from modules
*
* @param array $params
* @param string $location
* @return array
*/
private function collectModuleLayoutFiles(array $params, $location)
{
$files = [];
$area = $params['area'];
$requiredModuleName = $params['namespace'] . '_' . $params['module'];
foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleName => $moduleDir) {
if ($requiredModuleName == '*_*' || $moduleName == $requiredModuleName) {
$moduleFiles = [];
$this->_accumulateFilesByPatterns(
[$moduleDir . "/view/{$area}/{$location}"],
'*.xml',
$moduleFiles
);
if ($params['with_metainfo']) {
foreach ($moduleFiles as $moduleFile) {
$modulePath = str_replace(DIRECTORY_SEPARATOR, '/', preg_quote($moduleDir, '#'));
$regex = '#^' . $modulePath . '/view/(?P<area>[a-z]+)/layout/(?P<path>.+)$#i';
if (preg_match($regex, $moduleFile, $matches)) {
$files[] = [
$matches['area'],
'',
$moduleName,
$matches['path'],
$moduleFile,
];
} else {
throw new \UnexpectedValueException("Could not parse modular layout file '$moduleFile'");
}
}
} else {
$files = array_merge($files, $moduleFiles);
}
}
}
return $files;
}
/**
* Collect layout files from themes
*
* @param array $params
* @param string $location
* @return array
*/
private function collectThemeLayoutFiles(array $params, $location)
{
$files = [];
$area = $params['area'];
$requiredModuleName = $params['namespace'] . '_' . $params['module'];
$themePath = $params['theme_path'];
foreach ($this->themePackageList->getThemes() as $theme) {
$currentThemePath = str_replace(DIRECTORY_SEPARATOR, '/', $theme->getPath());
$currentThemeCode = $theme->getVendor() . '/' . $theme->getName();
if (($area == '*' || $theme->getArea() === $area)
&& ($themePath == '*' || $themePath == '*/*' || $themePath == $currentThemeCode)
) {
$themeFiles = [];
$this->_accumulateFilesByPatterns(
[$currentThemePath . "/{$requiredModuleName}/{$location}"],
'*.xml',
$themeFiles
);
if ($params['with_metainfo']) {
$files = array_merge($this->parseThemeFiles($themeFiles, $currentThemePath, $theme));
} else {
$files = array_merge($files, $themeFiles);
}
}
}
return $files;
}
/**
* @param array $themeFiles
* @param string $currentThemePath
* @param ThemePackage $theme
* @return array
*/
private function parseThemeFiles($themeFiles, $currentThemePath, $theme)
{
$files = [];
$regex = '#^' . $currentThemePath
. '/(?P<module>[a-z\d]+_[a-z\d]+)/layout/(override/((base/)|(theme/[a-z\d_]+/[a-z\d_]+/)))?'
. '(?P<path>.+)$#i';
foreach ($themeFiles as $themeFile) {
if (preg_match($regex, $themeFile, $matches)) {
$files[] = [
$theme->getArea(),
$theme->getVendor() . '/' . $theme->getName(),
$matches['module'],
$matches['path'],
$themeFile,
];
} else {
throw new \UnexpectedValueException("Could not parse theme layout file '$themeFile'");
}
}
return $files;
}
/**
* Returns list of page_type files, used by Magento application modules
*
* An incoming array can contain the following items
* array (
* 'namespace' => 'namespace_name',
* 'module' => 'module_name',
* 'area' => 'area_name',
* 'theme' => 'theme_name',
* )
*
* @param array $incomingParams
* @param bool $asDataSet
* @return array
*/
public function getPageTypeFiles($incomingParams = [], $asDataSet = true)
{
$params = ['namespace' => '*', 'module' => '*', 'area' => '*'];
foreach (array_keys($params) as $key) {
if (isset($incomingParams[$key])) {
$params[$key] = $incomingParams[$key];
}
}
$cacheKey = md5(implode('|', $params));
if (!isset(self::$_cache[__METHOD__][$cacheKey])) {
self::$_cache[__METHOD__][$cacheKey] = self::getFiles(
$this->getEtcAreaPaths($params['namespace'], $params['module'], $params['area']),
'page_types.xml'
);
}
if ($asDataSet) {
return self::composeDataSets(self::$_cache[__METHOD__][$cacheKey]);
}
return self::$_cache[__METHOD__][$cacheKey];
}
/**
* Get module etc paths for specified area
*
* @param string $namespace
* @param string $module
* @param string $area
* @return array
*/
private function getEtcAreaPaths($namespace, $module, $area)
{
$etcAreaPaths = [];
foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleName => $moduleDir) {
$keyInfo = explode('_', $moduleName);
if ($keyInfo[0] == $namespace || $namespace == '*') {
if ($keyInfo[1] == $module || $module == '*') {
$etcAreaPaths[] = $moduleDir . "/etc/{$area}";
}
}
}
return $etcAreaPaths;
}
/**
* Returns list of Javascript files in Magento
*
* @param string $area
* @param string $themePath
* @param string $namespace
* @param string $module
* @return array
*/
public function getJsFiles($area = '*', $themePath = '*/*', $namespace = '*', $module = '*')
{
$key = $area . $themePath . $namespace . $module . __METHOD__ . BP;
if (isset(self::$_cache[$key])) {
return self::$_cache[$key];
}
$moduleWebPaths = [];
foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleName => $moduleDir) {
$keyInfo = explode('_', $moduleName);
if ($keyInfo[0] == $namespace || $namespace == '*') {
if ($keyInfo[1] == $module || $module == '*') {
$moduleWebPaths[] = $moduleDir . "/view/{$area}/web";
}
}
}
$themePaths = $this->getThemePaths($area, $namespace . '_' . $module, '/web');
$files = self::getFiles(
array_merge(
[
BP . "/lib/web/{mage,varien}"
],
$themePaths,
$moduleWebPaths
),
'*.js'
);
$result = self::composeDataSets($files);
self::$_cache[$key] = $result;
return $result;
}
/**
* @param string $area
* @param string $module
* @param string $subFolder
* @return array
*/
private function getThemePaths($area, $module, $subFolder)
{
$themePaths = [];
foreach ($this->themePackageList->getThemes() as $theme) {
if ($area == '*' || $theme->getArea() === $area) {
$themePaths[] = $theme->getPath() . $subFolder;
$themePaths[] = $theme->getPath() . "/{$module}" . $subFolder;
}
}
return $themePaths;
}
/**
* Returns list of Static HTML files in Magento
*
* @param string $area
* @param string $themePath
* @param string $namespace
* @param string $module
* @return array
*/
public function getStaticHtmlFiles($area = '*', $themePath = '*/*', $namespace = '*', $module = '*')
{
$key = $area . $themePath . $namespace . $module . __METHOD__;
if (isset(self::$_cache[$key])) {
return self::$_cache[$key];
}
$moduleTemplatePaths = [];
foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleName => $moduleDir) {
$keyInfo = explode('_', $moduleName);
if ($keyInfo[0] == $namespace || $namespace == '*') {
if ($keyInfo[1] == $module || $module == '*') {
$moduleTemplatePaths[] = $moduleDir . "/view/{$area}/web/template";
$moduleTemplatePaths[] = $moduleDir . "/view/{$area}/web/templates";
}
}
}
$themePaths = $this->getThemePaths($area, $namespace . '_' . $module, '/web/template');
$files = self::getFiles(
array_merge(
$themePaths,
$moduleTemplatePaths
),
'*.html'
);
$result = self::composeDataSets($files);
self::$_cache[$key] = $result;
return $result;
}
/**
* Get list of static view files that are subject of Magento static view files pre-processing system
*
* @param string $filePattern
* @return array
*/
public function getStaticPreProcessingFiles($filePattern = '*')
{
$key = __METHOD__ . '|' . $filePattern;
if (isset(self::$_cache[$key])) {
return self::$_cache[$key];
}
$area = '*';
$locale = '*';
$result = [];
$moduleWebPath = [];
$moduleLocalePath = [];
foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleDir) {
$moduleWebPath[] = $moduleDir . "/view/{$area}/web";
$moduleLocalePath[] = $moduleDir . "/view/{$area}/web/i18n/{$locale}";
}
$this->_accumulateFilesByPatterns($moduleWebPath, $filePattern, $result, '_parseModuleStatic');
$this->_accumulateFilesByPatterns($moduleLocalePath, $filePattern, $result, '_parseModuleLocaleStatic');
$this->accumulateThemeStaticFiles($area, $locale, $filePattern, $result);
self::$_cache[$key] = $result;
return $result;
}
/**
* Accumulate files from themes
*
* @param string $area
* @param string $locale
* @param string $filePattern
* @param array $result
* @return void
*/
private function accumulateThemeStaticFiles($area, $locale, $filePattern, &$result)
{
foreach ($this->themePackageList->getThemes() as $themePackage) {
$themeArea = $themePackage->getArea();
if ($area == '*' || $area == $themeArea) {
$files = [];
$themePath = str_replace(DIRECTORY_SEPARATOR, '/', $themePackage->getPath());
$paths = [
$themePath . "/web",
$themePath . "/*_*/web",
$themePath . "/web/i18n/{$locale}",
$themePath . "/*_*/web/i18n/{$locale}"
];
$this->_accumulateFilesByPatterns($paths, $filePattern, $files);
$regex = '#^' . $themePath .
'/((?P<module>[a-z\d]+_[a-z\d]+)/)?web/(i18n/(?P<locale>[a-z_]+)/)?(?P<path>.+)$#i';
foreach ($files as $file) {
if (preg_match($regex, $file, $matches)) {
$result[] = [
$themeArea,
$themePackage->getVendor() . '/' . $themePackage->getName(),
$matches['locale'],
$matches['module'],
$matches['path'],
$file,
];
} else {
throw new \UnexpectedValueException("Could not parse theme static file '$file'");
}
}
}
}
}
/**
* Get all files from static library directory
*
* @return array
*/
public function getStaticLibraryFiles()
{
$result = [];
$this->_accumulateFilesByPatterns([BP . "/lib/web"], '*', $result, '_parseLibStatic');
return $result;
}
/**
* Parse file path from the absolute path of static library
*
* @param string $file
* @param string $path
* @return string
*/
protected function _parseLibStatic($file, $path)
{
preg_match('/^' . preg_quote("{$path}/lib/web/", '/') . '(.+)$/i', $file, $matches);
return $matches[1];
}
/**
* Search files by the specified patterns and accumulate them, applying a callback to each found row
*
* @param array $patterns
* @param string $filePattern
* @param array $result
* @param bool $subroutine
* @return void
*/
protected function _accumulateFilesByPatterns(array $patterns, $filePattern, array &$result, $subroutine = false)
{
$path = str_replace(DIRECTORY_SEPARATOR, '/', BP);
foreach (self::getFiles($patterns, $filePattern) as $file) {
$file = str_replace(DIRECTORY_SEPARATOR, '/', $file);
if ($subroutine) {
$result[] = $this->$subroutine($file, $path);
} else {
$result[] = $file;
}
}
}
/**
* Parse meta-info of a static file in module
*
* @param string $file
* @return array
*/
protected function _parseModuleStatic($file)
{
foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleName => $modulePath) {
if (preg_match(
'/^' . preg_quote("{$modulePath}/", '/') . 'view\/([a-z]+)\/web\/(.+)$/i',
$file,
$matches
) === 1
) {
list(, $area, $filePath) = $matches;
return [$area, '', '', $moduleName, $filePath, $file];
}
}
return [];
}
/**
* Parse meta-info of a localized (translated) static file in module
*
* @param string $file
* @return array
*/
protected function _parseModuleLocaleStatic($file)
{
foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleName => $modulePath) {
$appCode = preg_quote("{$modulePath}/", '/');
if (preg_match('/^' . $appCode . 'view\/([a-z]+)\/web\/i18n\/([a-z_]+)\/(.+)$/i', $file, $matches) === 1) {
list(, $area, $locale, $filePath) = $matches;
return [$area, '', $locale, $moduleName, $filePath, $file];
}
}
return [];
}
/**
* Returns list of Javascript files in Magento by certain area
*
* @param string $area
* @return array
*/
public function getJsFilesForArea($area)
{
$key = __METHOD__ . BP . $area;
if (isset(self::$_cache[$key])) {
return self::$_cache[$key];
}
$viewAreaPaths = [];
foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleDir) {
$viewAreaPaths[] = $moduleDir . "/view/{$area}";
}
$themePaths = [];