Skip to content

Commit 8bb95b8

Browse files
committed
feat: Enhance composer dependency checks with improved path resolution and error handling 🛠️
1 parent b43334c commit 8bb95b8

1 file changed

Lines changed: 107 additions & 37 deletions

File tree

src/Service/ThemeChecker/MagentoStandard/Checker.php

Lines changed: 107 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,65 +40,135 @@ public function checkComposerDependencies(string $themePath): array
4040
return ['error' => 'Composer not found on the system.'];
4141
}
4242

43+
// Find the correct composer path to use
44+
$composerConfig = $this->findComposerPath($themePath);
45+
if (isset($composerConfig['warning'])) {
46+
return $composerConfig;
47+
}
48+
49+
$composerPath = $composerConfig['path'];
50+
$usingProjectRoot = $composerConfig['using_project_root'];
51+
52+
// Get dependencies using JSON format first
53+
$result = $this->getComposerDependenciesJson($composerPath);
54+
55+
// If JSON format failed, try table format
56+
if (empty($result)) {
57+
$result = $this->getComposerDependenciesTable($composerPath);
58+
}
59+
60+
// If we have results, add metadata
61+
if (!empty($result) && !isset($result['error'])) {
62+
$result = $this->addComposerMetadata($result, $usingProjectRoot, $composerPath);
63+
return $result;
64+
}
65+
66+
return $result ?: ['error' => 'Error parsing composer outdated output.'];
67+
}
68+
69+
/**
70+
* Find the appropriate path for composer operations
71+
*
72+
* @param string $themePath The theme path to check
73+
* @return array Configuration with path and flag
74+
*/
75+
protected function findComposerPath(string $themePath): array
76+
{
4377
// Check if the theme has a vendor directory
4478
$hasVendorDir = $this->fileSystem->isDir($themePath . '/vendor');
4579

46-
// If there is no vendor directory in the theme, try to use the project root vendor
47-
if (!$hasVendorDir) {
48-
$projectRoot = $this->findProjectRoot();
49-
if (empty($projectRoot) || !$this->fileSystem->isDir($projectRoot . '/vendor')) {
50-
return ['warning' => 'No vendor directory found in theme or project root.'];
51-
}
52-
$usingProjectRoot = true;
53-
$composerPath = $projectRoot;
54-
} else {
55-
$usingProjectRoot = false;
56-
$composerPath = $themePath;
80+
if ($hasVendorDir) {
81+
return [
82+
'path' => $themePath,
83+
'using_project_root' => false
84+
];
5785
}
5886

59-
// Run composer outdated
87+
// If no vendor directory in theme, try project root
88+
$projectRoot = $this->findProjectRoot();
89+
if (!empty($projectRoot) && $this->fileSystem->isDir($projectRoot . '/vendor')) {
90+
return [
91+
'path' => $projectRoot,
92+
'using_project_root' => true
93+
];
94+
}
95+
96+
// No valid vendor directory found
97+
return ['warning' => 'No vendor directory found in theme or project root.'];
98+
}
99+
100+
/**
101+
* Get composer dependencies using JSON format
102+
*
103+
* @param string $composerPath Path to run composer in
104+
* @return array Results or empty array if failed
105+
*/
106+
protected function getComposerDependenciesJson(string $composerPath): array
107+
{
60108
$cwd = $this->fileSystem->getCurrentDir();
61109
$this->fileSystem->changeDir($composerPath);
110+
62111
$output = [];
63112
$exitCode = 0;
64113
$this->safeExec('composer outdated --direct --format=json 2>/dev/null', $output, $exitCode);
114+
65115
$this->fileSystem->changeDir($cwd);
66116

67-
// Parse JSON output if available
68-
if (!empty($output)) {
69-
$jsonOutput = implode('', $output);
70-
$outdated = json_decode($jsonOutput, true);
117+
if (empty($output)) {
118+
return [];
119+
}
71120

72-
if (json_last_error() === JSON_ERROR_NONE && isset($outdated['installed'])) {
73-
$result = $outdated['installed'];
74-
if ($usingProjectRoot) {
75-
$result['_meta'] = [
76-
'using_project_root' => true,
77-
'project_root' => $composerPath
78-
];
79-
}
80-
return $result;
81-
}
121+
$jsonOutput = implode('', $output);
122+
$outdated = json_decode($jsonOutput, true);
123+
124+
if (json_last_error() === JSON_ERROR_NONE && isset($outdated['installed'])) {
125+
return $outdated['installed'];
82126
}
83127

84-
// If JSON parsing failed or no structured output, try the table format
85-
$output = [];
128+
return [];
129+
}
130+
131+
/**
132+
* Get composer dependencies using table format
133+
*
134+
* @param string $composerPath Path to run composer in
135+
* @return array Results or empty array if failed
136+
*/
137+
protected function getComposerDependenciesTable(string $composerPath): array
138+
{
139+
$cwd = $this->fileSystem->getCurrentDir();
86140
$this->fileSystem->changeDir($composerPath);
141+
142+
$output = [];
87143
$this->safeExec('composer outdated --direct 2>/dev/null', $output);
144+
88145
$this->fileSystem->changeDir($cwd);
89146

90147
if (!empty($output)) {
91-
$result = $this->parseComposerOutdatedOutput($output);
92-
if ($usingProjectRoot) {
93-
$result['_meta'] = [
94-
'using_project_root' => true,
95-
'project_root' => $composerPath
96-
];
97-
}
98-
return $result;
148+
return $this->parseComposerOutdatedOutput($output);
99149
}
100150

101-
return ['error' => 'Error parsing composer outdated output.'];
151+
return [];
152+
}
153+
154+
/**
155+
* Add metadata to composer results
156+
*
157+
* @param array $result The dependency results
158+
* @param bool $usingProjectRoot Whether using project root
159+
* @param string $composerPath The path used
160+
* @return array Results with metadata
161+
*/
162+
protected function addComposerMetadata(array $result, bool $usingProjectRoot, string $composerPath): array
163+
{
164+
if ($usingProjectRoot) {
165+
$result['_meta'] = [
166+
'using_project_root' => true,
167+
'project_root' => $composerPath
168+
];
169+
}
170+
171+
return $result;
102172
} /**
103173
* {@inheritdoc}
104174
*/

0 commit comments

Comments
 (0)