Skip to content

Commit 759ec08

Browse files
Copilotdermatz
andcommitted
#feature-request - Refactor CleanCommand for better efficiency
Co-authored-by: dermatz <6103201+dermatz@users.noreply.github.com>
1 parent b619baf commit 759ec08

1 file changed

Lines changed: 30 additions & 60 deletions

File tree

src/Console/Command/Static/CleanCommand.php

Lines changed: 30 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -183,38 +183,14 @@ private function cleanViewPreprocessed(string $themeCode): bool
183183
$viewPreprocessedPath = $varDir->getAbsolutePath('view_preprocessed');
184184

185185
if (!is_dir($viewPreprocessedPath)) {
186-
$this->io->info('var/view_preprocessed directory does not exist.');
186+
$this->io->writeln('var/view_preprocessed directory does not exist');
187187
return true;
188188
}
189189

190-
// Convert Vendor/theme to frontend/Vendor/theme pattern
191-
$themePattern = $this->getThemePattern($themeCode);
192-
$cleaned = 0;
193-
194-
// Scan for matching theme directories
195-
$iterator = new \RecursiveIteratorIterator(
196-
new \RecursiveDirectoryIterator($viewPreprocessedPath, \RecursiveDirectoryIterator::SKIP_DOTS),
197-
\RecursiveIteratorIterator::SELF_FIRST
198-
);
199-
200-
$dirsToDelete = [];
201-
foreach ($iterator as $file) {
202-
$path = $file->getPathname();
203-
if ($file->isDir() && strpos($path, $themePattern) !== false) {
204-
$dirsToDelete[] = $path;
205-
}
206-
}
207-
208-
// Delete directories from deepest to shallowest
209-
rsort($dirsToDelete);
210-
foreach ($dirsToDelete as $dir) {
211-
if ($this->removeDirectory($dir)) {
212-
$cleaned++;
213-
}
214-
}
190+
$cleaned = $this->cleanThemeDirectories($viewPreprocessedPath, $themeCode);
215191

216192
if ($cleaned > 0) {
217-
$this->io->writeln(sprintf(' ✓ Cleaned %d directories from var/view_preprocessed', $cleaned));
193+
$this->io->writeln(sprintf(' ✓ Cleaned %d item(s) from var/view_preprocessed', $cleaned));
218194
} else {
219195
$this->io->writeln(' ℹ No files found in var/view_preprocessed');
220196
}
@@ -239,38 +215,14 @@ private function cleanPubStatic(string $themeCode): bool
239215
$staticPath = $staticDir->getAbsolutePath();
240216

241217
if (!is_dir($staticPath)) {
242-
$this->io->info('pub/static directory does not exist.');
218+
$this->io->writeln('pub/static directory does not exist');
243219
return true;
244220
}
245221

246-
// Convert Vendor/theme to frontend/Vendor/theme pattern
247-
$themePattern = $this->getThemePattern($themeCode);
248-
$cleaned = 0;
249-
250-
// Scan for matching theme directories
251-
$iterator = new \RecursiveIteratorIterator(
252-
new \RecursiveDirectoryIterator($staticPath, \RecursiveDirectoryIterator::SKIP_DOTS),
253-
\RecursiveIteratorIterator::SELF_FIRST
254-
);
255-
256-
$dirsToDelete = [];
257-
foreach ($iterator as $file) {
258-
$path = $file->getPathname();
259-
if ($file->isDir() && strpos($path, $themePattern) !== false) {
260-
$dirsToDelete[] = $path;
261-
}
262-
}
263-
264-
// Delete directories from deepest to shallowest
265-
rsort($dirsToDelete);
266-
foreach ($dirsToDelete as $dir) {
267-
if ($this->removeDirectory($dir)) {
268-
$cleaned++;
269-
}
270-
}
222+
$cleaned = $this->cleanThemeDirectories($staticPath, $themeCode);
271223

272224
if ($cleaned > 0) {
273-
$this->io->writeln(sprintf(' ✓ Cleaned %d directories from pub/static', $cleaned));
225+
$this->io->writeln(sprintf(' ✓ Cleaned %d item(s) from pub/static', $cleaned));
274226
} else {
275227
$this->io->writeln(' ℹ No files found in pub/static');
276228
}
@@ -283,16 +235,34 @@ private function cleanPubStatic(string $themeCode): bool
283235
}
284236

285237
/**
286-
* Get theme pattern for matching directories
238+
* Clean theme directories from a base path
287239
*
240+
* @param string $basePath
288241
* @param string $themeCode
289-
* @return string
242+
* @return int Number of items cleaned
290243
*/
291-
private function getThemePattern(string $themeCode): string
244+
private function cleanThemeDirectories(string $basePath, string $themeCode): int
292245
{
293-
// Theme code format: Vendor/theme
294-
// Pattern in directories: frontend/Vendor/theme
295-
return 'frontend/' . $themeCode;
246+
$cleaned = 0;
247+
248+
// Scan for areas (frontend, adminhtml, etc.)
249+
$areas = ['frontend'];
250+
foreach ($areas as $area) {
251+
$areaPath = $basePath . DIRECTORY_SEPARATOR . $area;
252+
if (!is_dir($areaPath)) {
253+
continue;
254+
}
255+
256+
// Look for the specific theme directory
257+
$themePath = $areaPath . DIRECTORY_SEPARATOR . $themeCode;
258+
if (is_dir($themePath)) {
259+
if ($this->removeDirectory($themePath)) {
260+
$cleaned++;
261+
}
262+
}
263+
}
264+
265+
return $cleaned;
296266
}
297267

298268
/**

0 commit comments

Comments
 (0)