Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/ZipGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -25,6 +98,8 @@ public static function generate(): void
return;
}

self::loadZipignore();

$zipName = $pluginName . '.zip';

if (file_exists($zipName)) {
Expand Down Expand Up @@ -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);
}
Expand Down