|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ProtoneMedia\Splade; |
| 4 | + |
| 5 | +use Illuminate\Support\Str; |
| 6 | +use Illuminate\View\Compilers\BladeCompiler; |
| 7 | +use ProtoneMedia\Splade\Components\SpladeComponent; |
| 8 | +use ProtoneMedia\Splade\Http\PrepareTableCells; |
| 9 | + |
| 10 | +class CustomBladeCompiler extends BladeCompiler |
| 11 | +{ |
| 12 | + /** |
| 13 | + * This method is overridden to prepare some Splade components before they are compiled. |
| 14 | + * |
| 15 | + * @param string $view |
| 16 | + * @return string |
| 17 | + */ |
| 18 | + protected function compileComponentTags($view) |
| 19 | + { |
| 20 | + $this->prepareLazyComponents($view); |
| 21 | + $this->prepareRehydrateComponents($view); |
| 22 | + $this->replaceCellComponentWithCellDirective($view); |
| 23 | + |
| 24 | + return parent::compileComponentTags($view); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Returns a regex pattern to match an HTML tag and its contents. |
| 29 | + */ |
| 30 | + public static function regexForTag(string $tag): string |
| 31 | + { |
| 32 | + return '/(<\s*' . $tag . '[^>]*>)(.|\n)*?(<\/' . $tag . '>)/'; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Replaces the <x-splace-cell> component with the @cell directive. |
| 37 | + */ |
| 38 | + protected function replaceCellComponentWithCellDirective(&$view) |
| 39 | + { |
| 40 | + $view = preg_replace_callback(static::regexForTag(SpladeComponent::tag('table')), function ($table) { |
| 41 | + $tableHtml = $table[0]; |
| 42 | + $tableOpening = $table[1]; |
| 43 | + |
| 44 | + preg_match_all(PrepareTableCells::HTML_ATTRIBUTES_REGEX, $tableOpening, $matches, PREG_SET_ORDER); |
| 45 | + |
| 46 | + $arguments = collect($matches)->mapWithKeys(function ($match) use (&$tableOpening) { |
| 47 | + $attribute = $match['attribute']; |
| 48 | + $value = $match['value'] ?? null; |
| 49 | + |
| 50 | + if ($value && in_array($attribute, ['as', 'key', 'use'])) { |
| 51 | + // Remove these attributes from the table tag, so Vue won't be mad. |
| 52 | + $tableOpening = str_replace($match[0], '', $tableOpening); |
| 53 | + |
| 54 | + return [$attribute => PrepareTableCells::stripQuotes($value)]; |
| 55 | + } |
| 56 | + |
| 57 | + return []; |
| 58 | + }); |
| 59 | + |
| 60 | + // Replace the original tag opening with the modified one, without the attributes. |
| 61 | + $tableHtml = str_replace($table[1], $tableOpening, $tableHtml); |
| 62 | + |
| 63 | + // Replace the custom cells. |
| 64 | + return PrepareTableCells::replaceCellComponentWithCellDirective( |
| 65 | + $tableHtml, |
| 66 | + $arguments->get('as', '$item'), |
| 67 | + $arguments->get('key', '$key'), |
| 68 | + $arguments->get('use', ''), |
| 69 | + ); |
| 70 | + }, $view); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * It adds an additional unless-statement around the placeholder, so it's only rendered |
| 75 | + * when the component is not rehydrated. |
| 76 | + */ |
| 77 | + protected function prepareRehydrateComponents(&$view) |
| 78 | + { |
| 79 | + // Find the rehydrate components within the view |
| 80 | + preg_match_all(static::regexForTag(SpladeComponent::tag('rehydrate')), $view, $matches); |
| 81 | + |
| 82 | + // Extract the (optional) placeholder |
| 83 | + collect($matches[0] ?? [])->each(function (string $rehydrateComponent) use (&$view) { |
| 84 | + preg_match_all(static::regexForTag('x-slot:placeholder'), $rehydrateComponent, $placeholderMatches); |
| 85 | + |
| 86 | + $placeholder = $placeholderMatches[0][0] ?? ''; |
| 87 | + |
| 88 | + if (!$placeholder) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + $vuePlaceholder = str_replace($placeholderMatches[1][0], '<template v-slot:placeholder>', $placeholder); |
| 93 | + $vuePlaceholder = str_replace($placeholderMatches[3][0], '</template>', $vuePlaceholder); |
| 94 | + |
| 95 | + $vuePlaceholder = implode(PHP_EOL, [ |
| 96 | + '@unless(\ProtoneMedia\Splade\Facades\Splade::isRehydrateRequest())', |
| 97 | + $vuePlaceholder, |
| 98 | + '@endunless', |
| 99 | + ]); |
| 100 | + |
| 101 | + $view = str_replace($placeholder, $vuePlaceholder, $view); |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * It adds an additional if-statement around the slot, so it's only rendered |
| 107 | + * when the component is lazy-loaded. |
| 108 | + */ |
| 109 | + protected function prepareLazyComponents(&$view) |
| 110 | + { |
| 111 | + // Find the lazy components within the view |
| 112 | + preg_match_all(static::regexForTag(SpladeComponent::tag('lazy')), $view, $matches); |
| 113 | + |
| 114 | + // Replace all lazy components with just the placeholder |
| 115 | + collect($matches[0] ?? [])->each(function (string $lazyComponent, $key) use ($matches, &$view) { |
| 116 | + preg_match_all(static::regexForTag('x-slot:placeholder'), $lazyComponent, $placeholderMatches); |
| 117 | + |
| 118 | + $innerLazy = Str::between($lazyComponent, $matches[1][$key], $matches[3][$key]); |
| 119 | + |
| 120 | + $placeholder = $placeholderMatches[0][0] ?? ''; |
| 121 | + |
| 122 | + $innerLazyWithoutPlaceholder = $placeholder ? str_replace($placeholder, '', $innerLazy) : $innerLazy; |
| 123 | + |
| 124 | + $newLazyComponent = implode(PHP_EOL, array_filter([ |
| 125 | + $matches[1][$key], |
| 126 | + $placeholder, |
| 127 | + '@if(\ProtoneMedia\Splade\Facades\Splade::isLazyRequest())', |
| 128 | + $innerLazyWithoutPlaceholder, |
| 129 | + '@endif', |
| 130 | + $matches[3][$key], |
| 131 | + ])); |
| 132 | + |
| 133 | + $view = str_replace($lazyComponent, $newLazyComponent, $view); |
| 134 | + }); |
| 135 | + } |
| 136 | +} |
0 commit comments