-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathComposerModuleResolver.php
179 lines (162 loc) · 5.74 KB
/
ComposerModuleResolver.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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Util;
use Magento\FunctionalTestingFramework\Composer\ComposerInstall;
use Magento\FunctionalTestingFramework\Composer\ComposerPackage;
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
/**
* Composer Based Module Resolver
*/
class ComposerModuleResolver
{
/**
* Code path array from composer json search
*
* @var array
*/
private $searchedTestModules = null;
/**
* Code path array from composer installed test packages
*
* @var array
*/
private $installedTestModules = null;
/**
* ComposerModuleResolver constructor
*/
public function __construct()
{
}
/**
* Get code paths for installed test modules
*
* @param string $rootComposerFile
* @return array
* @throws TestFrameworkException
*/
public function getComposerInstalledTestModules($rootComposerFile)
{
if (null !== $this->installedTestModules) {
return $this->installedTestModules;
}
if (!file_exists($rootComposerFile) || basename($rootComposerFile, '.json') != 'composer') {
throw new TestFrameworkException("Invalid root composer json file: {$rootComposerFile}");
}
$this->installedTestModules = [];
$composer = new ComposerInstall($rootComposerFile);
foreach ($composer->getInstalledTestPackages() as $packageName => $packageData) {
$suggestedModuleNames = $packageData[ComposerInstall::PACKAGE_SUGGESTED_MAGENTO_MODULES];
$path = $packageData[ComposerInstall::PACKAGE_INSTALLEDPATH];
$this->installedTestModules[$path] = $suggestedModuleNames;
}
return $this->installedTestModules;
}
/**
* Get code paths by searching test module composer json file from input directories
*
* @param array $directories
* @return array
* @throws TestFrameworkException
*/
public function getTestModulesFromPaths($directories)
{
if (null !== $this->searchedTestModules) {
return $this->searchedTestModules;
}
$this->searchedTestModules = [];
foreach ($directories as $directory) {
$this->searchedTestModules = array_merge_recursive(
$this->searchedTestModules,
$this->getTestModules($directory)
);
}
return $this->searchedTestModules;
}
/**
* Get code paths by searching test module composer json file from input directory
*
* @param string $directory
* @return array
* @throws TestFrameworkException
*/
private function getTestModules($directory)
{
$normalizedDir = realpath($directory);
if (!is_dir($normalizedDir)) {
throw new TestFrameworkException("Invalid directory: {$directory}");
}
// Find all composer json files under directory
$modules = [];
$fileList = $this->findComposerJsonFilesAtDepth($normalizedDir, 2);
foreach ($fileList as $file) {
// Parse composer json for test module name and path information
$composerInfo = new ComposerPackage($file);
if ($composerInfo->isMftfTestPackage()) {
$modulePath = str_replace(
DIRECTORY_SEPARATOR . 'composer.json',
'',
$file
);
$suggestedMagentoModuleNames = $composerInfo->getSuggestedMagentoModules();
if (array_key_exists($modulePath, $modules)) {
$modules[$modulePath] = array_merge($modules[$modulePath], $suggestedMagentoModuleNames);
} else {
$modules[$modulePath] = $suggestedMagentoModuleNames;
}
}
}
return $modules;
}
/**
* Find absolute paths of all composer json files in a given directory
*
* @param string $directory
* @return array
*/
private function findAllComposerJsonFiles($directory)
{
$directory = realpath($directory);
$jsonPattern = DIRECTORY_SEPARATOR . "composer.json";
$subDirectoryPattern = DIRECTORY_SEPARATOR . "*";
$jsonFileList = [];
foreach (glob($directory . $subDirectoryPattern, GLOB_ONLYDIR) as $dir) {
$jsonFileList = array_merge_recursive($jsonFileList, self::findAllComposerJsonFiles($dir));
}
$curJsonFiles = glob($directory . $jsonPattern);
if ($curJsonFiles !== false && !empty($curJsonFiles)) {
$jsonFileList = array_merge_recursive($jsonFileList, $curJsonFiles);
}
return $jsonFileList;
}
/**
* Find absolute paths of all composer json files in a given directory at certain depths
*
* @param string $directory
* @param integer $depth
* @return array
*/
private function findComposerJsonFilesAtDepth($directory, $depth)
{
$directory = realpath($directory);
$jsonPattern = DIRECTORY_SEPARATOR . "composer.json";
$subDirectoryPattern = DIRECTORY_SEPARATOR . "*";
$jsonFileList = [];
if ($depth > 0) {
foreach (glob($directory . $subDirectoryPattern, GLOB_ONLYDIR) as $dir) {
$jsonFileList = array_merge_recursive(
$jsonFileList,
self::findComposerJsonFilesAtDepth($dir, $depth-1)
);
}
} elseif ($depth == 0) {
$jsonFileList = glob($directory . $jsonPattern);
if ($jsonFileList === false) {
$jsonFileList = [];
}
}
return $jsonFileList;
}
}