From 21727154b5e3b563163a6390b7ba243be04713f1 Mon Sep 17 00:00:00 2001 From: Antonio Date: Fri, 17 Apr 2026 13:58:41 +0200 Subject: [PATCH] =?UTF-8?q?A=C3=B1ade=20soporte=20para=20.zipignore=20para?= =?UTF-8?q?=20excluir=20archivos=20del=20archivo=20ZIP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ZipGenerator.php | 80 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/src/ZipGenerator.php b/src/ZipGenerator.php index 27dbc93..8394b0e 100644 --- a/src/ZipGenerator.php +++ b/src/ZipGenerator.php @@ -11,6 +11,79 @@ class ZipGenerator { + private static array $ignorePatterns = []; + + private static function loadZipignore(): void + { + self::$ignorePatterns = []; + + if (!file_exists('.zipignore')) { + return; + } + + $lines = file('.zipignore', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); + foreach ($lines as $line) { + $line = trim($line); + if (empty($line) || $line[0] === '#') { + continue; + } + self::$ignorePatterns[] = $line; + } + } + + private static function shouldIgnore(string $path): bool + { + $relativePath = ltrim($path, './\\'); + + foreach (self::$ignorePatterns as $pattern) { + $negated = $pattern[0] === '!'; + if ($negated) { + $pattern = substr($pattern, 1); + } + + $pattern = trim($pattern); + if (empty($pattern)) { + continue; + } + + // Directorio: termina en / + $isDir = substr($pattern, -1) === '/'; + if ($isDir) { + $pattern = rtrim($pattern, '/'); + } + + // Normalizar el patrón ./ -> vacío + $pattern = ltrim($pattern, './'); + + // Convertir glob a regex + $regex = self::globToRegex($pattern); + + if ($isDir) { + // Para directorios, verificar si la ruta comienza con el patrón + $patternDir = ltrim($pattern, './'); + if (strpos($relativePath, $patternDir . '/') === 0) { + return !$negated; + } + } else { + $match = preg_match($regex, $relativePath); + if ($match) { + return !$negated; + } + } + } + + return false; + } + + private static function globToRegex(string $glob): string + { + $regex = preg_quote($glob, '/'); + $regex = str_replace('\*\*', '.*', $regex); + $regex = str_replace('\*', '[^/]*', $regex); + $regex = str_replace('\?', '[^/]', $regex); + return '/^' . $regex . '$/'; + } + public static function generate(): void { if (false === Utils::isPluginFolder()) { @@ -25,6 +98,8 @@ public static function generate(): void return; } + self::loadZipignore(); + $zipName = $pluginName . '.zip'; if (file_exists($zipName)) { @@ -83,6 +158,11 @@ public static function generate(): void continue; } + // aplicar reglas de .zipignore + if (self::shouldIgnore($name)) { + continue; + } + $path = str_replace('./', $pluginName . '/', $name); $zip->addFile($name, $path); }