|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ArtisanBuild\FluxThemes\Pipeline; |
| 4 | + |
| 5 | +use ArtisanBuild\FluxThemes\Actions\DetectPackagesWithTemplates; |
| 6 | +use ArtisanBuild\FluxThemes\Theme; |
| 7 | +use Closure; |
| 8 | + |
| 9 | +class EnsureRequiredSourcePathsExist |
| 10 | +{ |
| 11 | + public function __construct(private readonly DetectPackagesWithTemplates $packages_with_templates) {} |
| 12 | + |
| 13 | + public function __invoke(Theme $theme, Closure $next): Theme |
| 14 | + { |
| 15 | + $laravel = [ |
| 16 | + "@source '../../vendor/laravel/framework/src/Illuminate/Pagination/resources/views/*.blade.php';", |
| 17 | + "@source '../../storage/framework/views/*.php';", |
| 18 | + "@source '../../resources/**/*.blade.php';", |
| 19 | + "@source '.../../resources/**/*.js';", |
| 20 | + "@source '../../resources/**/*.vue';", |
| 21 | + ]; |
| 22 | + $flux = [ |
| 23 | + "@source '../../vendor/livewire/flux/stubs/**/*.blade.php';", |
| 24 | + "@source '../../vendor/livewire/flux-pro/stubs/**/*.blade.php';", |
| 25 | + ]; |
| 26 | + $packages = ($this->packages_with_templates)() |
| 27 | + ->map(fn ($package) => "@source '{$package}/resources/views/*.blade.php';") |
| 28 | + ->toArray(); |
| 29 | + |
| 30 | + $required = array_merge($laravel, $flux, $packages); |
| 31 | + |
| 32 | + // Filter out paths that already exist in the CSS |
| 33 | + $missing_paths = collect($required)->reject(fn ($path): bool => str_contains($theme->css, (string) $path)); |
| 34 | + |
| 35 | + if ($missing_paths->isNotEmpty()) { |
| 36 | + // Convert CSS content to collection of lines |
| 37 | + $lines = collect(explode("\n", $theme->css)); |
| 38 | + |
| 39 | + // Get the index of the last @import by checking all lines |
| 40 | + $last_import_index = $lines |
| 41 | + ->reduce(fn ($carry, $line, $index) => str_contains((string) $line, '@import') ? $index : $carry, -1); |
| 42 | + |
| 43 | + if ($last_import_index !== -1) { |
| 44 | + // Get all lines up to and including the last @import |
| 45 | + $before = $lines->take($last_import_index + 1); |
| 46 | + // Get all remaining lines |
| 47 | + $after = $lines->skip($last_import_index + 1); |
| 48 | + |
| 49 | + // Ensure we add a newline after imports if there isn't one |
| 50 | + if ($missing_paths->isNotEmpty() && trim((string) $before->last()) !== '') { |
| 51 | + $missing_paths->prepend(''); |
| 52 | + } |
| 53 | + |
| 54 | + // Rebuild the CSS content |
| 55 | + $theme->css = $before |
| 56 | + ->merge($missing_paths) |
| 57 | + ->when($after->isNotEmpty(), fn ($collection) => $collection->merge([''])->merge($after)) |
| 58 | + ->filter() |
| 59 | + ->implode("\n"); |
| 60 | + } else { |
| 61 | + // If no @import found, add at the beginning |
| 62 | + $theme->css = $missing_paths |
| 63 | + ->merge(['']) // Add a blank line between imports and content |
| 64 | + ->merge($lines) |
| 65 | + ->filter() |
| 66 | + ->implode("\n"); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return $next($theme); |
| 71 | + } |
| 72 | +} |
0 commit comments