|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This script patches PHP 8+ compatibility issues in Illuminate Container and Eloquent classes. |
| 5 | + * |
| 6 | + * It fixes: |
| 7 | + * 1. PSR-11 ContainerInterface compatibility (has() and get() methods) |
| 8 | + * 2. ArrayAccess interface compatibility (offsetExists, offsetGet, offsetSet, offsetUnset) |
| 9 | + * 3. Nullable parameter declarations |
| 10 | + * 4. Return type declarations for PHP 8+ |
| 11 | + */ |
| 12 | + |
| 13 | +function patchContainerPSR11($file) { |
| 14 | + if (!file_exists($file)) { |
| 15 | + echo "❌ File not found: $file\n"; |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + $content = file_get_contents($file); |
| 20 | + $originalContent = $content; |
| 21 | + |
| 22 | + // Fix has() method - PSR-11 requires: has(string $id): bool |
| 23 | + $content = preg_replace( |
| 24 | + '/public function has\(\$id\)(\s*\{)/s', |
| 25 | + 'public function has(string $id): bool$1', |
| 26 | + $content |
| 27 | + ); |
| 28 | + |
| 29 | + // Fix get() method - PSR-11 requires: get(string $id) |
| 30 | + $content = preg_replace( |
| 31 | + '/public function get\(\$id\)(\s*\{)/s', |
| 32 | + 'public function get(string $id)$1', |
| 33 | + $content |
| 34 | + ); |
| 35 | + |
| 36 | + // Fix offsetExists - ArrayAccess requires: offsetExists($key): bool |
| 37 | + $content = preg_replace( |
| 38 | + '/public function offsetExists\(\$key\)(\s*\{)/s', |
| 39 | + 'public function offsetExists($key): bool$1', |
| 40 | + $content |
| 41 | + ); |
| 42 | + |
| 43 | + // Fix offsetGet - ArrayAccess requires: offsetGet($key): mixed |
| 44 | + $content = preg_replace( |
| 45 | + '/public function offsetGet\(\$key\)(\s*\{)/s', |
| 46 | + 'public function offsetGet($key): mixed$1', |
| 47 | + $content |
| 48 | + ); |
| 49 | + |
| 50 | + // Fix offsetSet - ArrayAccess requires: offsetSet($key, $value): void |
| 51 | + $content = preg_replace( |
| 52 | + '/public function offsetSet\(\$key, \$value\)(\s*\{)/s', |
| 53 | + 'public function offsetSet($key, $value): void$1', |
| 54 | + $content |
| 55 | + ); |
| 56 | + |
| 57 | + // Fix offsetUnset - ArrayAccess requires: offsetUnset($key): void |
| 58 | + $content = preg_replace( |
| 59 | + '/public function offsetUnset\(\$key\)(\s*\{)/s', |
| 60 | + 'public function offsetUnset($key): void$1', |
| 61 | + $content |
| 62 | + ); |
| 63 | + |
| 64 | + // Fix nullable parameters - PHP 8.1+ requires explicit nullable type |
| 65 | + $content = preg_replace( |
| 66 | + '/public function resolving\(\$abstract, Closure \$callback = null\)/s', |
| 67 | + 'public function resolving($abstract, ?Closure $callback = null)', |
| 68 | + $content |
| 69 | + ); |
| 70 | + |
| 71 | + $content = preg_replace( |
| 72 | + '/public function afterResolving\(\$abstract, Closure \$callback = null\)/s', |
| 73 | + 'public function afterResolving($abstract, ?Closure $callback = null)', |
| 74 | + $content |
| 75 | + ); |
| 76 | + |
| 77 | + $content = preg_replace( |
| 78 | + '/public static function setInstance\(ContainerContract \$container = null\)/s', |
| 79 | + 'public static function setInstance(?ContainerContract $container = null)', |
| 80 | + $content |
| 81 | + ); |
| 82 | + |
| 83 | + if ($content !== $originalContent) { |
| 84 | + file_put_contents($file, $content); |
| 85 | + echo "✅ Patched Container: PSR-11 and ArrayAccess compatibility\n"; |
| 86 | + } else { |
| 87 | + echo "⚠️ Container already patched or no changes needed\n"; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +function patchReturnType($file, $method) { |
| 92 | + if (!file_exists($file)) { |
| 93 | + echo "❌ File not found: $file\n"; |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + $content = file_get_contents($file); |
| 98 | + |
| 99 | + // Check if method is already patched with the attribute |
| 100 | + $alreadyPatched = preg_match( |
| 101 | + '/#[ \t]*\\\\?ReturnTypeWillChange[ \t]*\n[ \t]*public function[ \t]+' . preg_quote($method) . '\s*\(/', |
| 102 | + $content |
| 103 | + ); |
| 104 | + |
| 105 | + // Alternatively, scan just before the method for attribute presence |
| 106 | + if (! $alreadyPatched) { |
| 107 | + // Match the method declaration with optional preceding attributes |
| 108 | + $methodPattern = '/((?:\s*#\[.*?\]\s*)*)\s*(public function[ \t]+' . preg_quote($method) . '\s*\([^)]*\))/'; |
| 109 | + |
| 110 | + $patched = preg_replace_callback($methodPattern, function($matches) use ($method) { |
| 111 | + $attributes = $matches[1]; |
| 112 | + $signature = $matches[2]; |
| 113 | + |
| 114 | + // Avoid duplicating ReturnTypeWillChange |
| 115 | + if (stripos($attributes, 'ReturnTypeWillChange') !== false) { |
| 116 | + echo "⚠️ Already patched: $method\n"; |
| 117 | + return $matches[0]; |
| 118 | + } |
| 119 | + |
| 120 | + echo "✅ Patched: $method\n"; |
| 121 | + return "#[\\ReturnTypeWillChange]\n " . $signature; |
| 122 | + }, $content, 1, $count); |
| 123 | + |
| 124 | + if ($count > 0) { |
| 125 | + file_put_contents($file, $patched); |
| 126 | + } else { |
| 127 | + echo "❌ Could not patch: $method\n"; |
| 128 | + } |
| 129 | + } else { |
| 130 | + echo "⚠️ Already patched: $method\n"; |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | + |
| 135 | +// Patch Container class for PSR-11 and ArrayAccess compatibility |
| 136 | +echo "=== Patching Illuminate Container ===\n"; |
| 137 | +$targetContainerFile = __DIR__ . '/../vendor/illuminate/container/Container.php'; |
| 138 | +patchContainerPSR11($targetContainerFile); |
| 139 | + |
| 140 | +// Patch Model and Collection classes |
| 141 | +echo "\n=== Patching Eloquent Model and Collection ===\n"; |
| 142 | +$targetModelFile = __DIR__ . '/../vendor/illuminate/database/Eloquent/Model.php'; |
| 143 | +$targetCollectionFile = __DIR__ . '/../vendor/illuminate/support/Collection.php'; |
| 144 | + |
| 145 | +$methods = ['offsetExists', 'offsetGet', 'offsetSet', 'offsetUnset', 'jsonSerialize', 'count', 'getIterator']; |
| 146 | + |
| 147 | +foreach ($methods as $method) { |
| 148 | + patchReturnType($targetModelFile, $method); |
| 149 | + patchReturnType($targetCollectionFile, $method); |
| 150 | +} |
| 151 | + |
| 152 | +// Patch Illuminate Pagination classes |
| 153 | +echo "\n=== Patching Illuminate Pagination ===\n"; |
| 154 | +$targetAbstractPaginatorFile = __DIR__ . '/../vendor/illuminate/pagination/AbstractPaginator.php'; |
| 155 | +$targetPaginatorFile = __DIR__ . '/../vendor/illuminate/pagination/Paginator.php'; |
| 156 | +$targetLengthAwarePaginatorFile = __DIR__ . '/../vendor/illuminate/pagination/LengthAwarePaginator.php'; |
| 157 | + |
| 158 | +foreach ($methods as $method) { |
| 159 | + patchReturnType($targetAbstractPaginatorFile, $method); |
| 160 | + patchReturnType($targetPaginatorFile, $method); |
| 161 | + patchReturnType($targetLengthAwarePaginatorFile, $method); |
| 162 | +} |
| 163 | + |
| 164 | +// Patch League Fractal ParamBag |
| 165 | +echo "\n=== Patching League Fractal ParamBag ===\n"; |
| 166 | +$targetParamBagFile = __DIR__ . '/../vendor/league/fractal/src/ParamBag.php'; |
| 167 | +$fractalMethods = ['offsetExists', 'offsetGet', 'offsetSet', 'offsetUnset', 'getIterator']; |
| 168 | + |
| 169 | +foreach ($fractalMethods as $method) { |
| 170 | + patchReturnType($targetParamBagFile, $method); |
| 171 | +} |
| 172 | + |
| 173 | +echo "\n✅ All patches applied successfully!\n"; |
0 commit comments