diff options
Diffstat (limited to 'vendor/doctrine/orm/src/Proxy/Autoloader.php')
| -rw-r--r-- | vendor/doctrine/orm/src/Proxy/Autoloader.php | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/vendor/doctrine/orm/src/Proxy/Autoloader.php b/vendor/doctrine/orm/src/Proxy/Autoloader.php new file mode 100644 index 0000000..1013e73 --- /dev/null +++ b/vendor/doctrine/orm/src/Proxy/Autoloader.php | |||
| @@ -0,0 +1,86 @@ | |||
| 1 | <?php | ||
| 2 | |||
| 3 | declare(strict_types=1); | ||
| 4 | |||
| 5 | namespace Doctrine\ORM\Proxy; | ||
| 6 | |||
| 7 | use Closure; | ||
| 8 | |||
| 9 | use function file_exists; | ||
| 10 | use function ltrim; | ||
| 11 | use function spl_autoload_register; | ||
| 12 | use function str_replace; | ||
| 13 | use function str_starts_with; | ||
| 14 | use function strlen; | ||
| 15 | use function substr; | ||
| 16 | |||
| 17 | use const DIRECTORY_SEPARATOR; | ||
| 18 | |||
| 19 | /** | ||
| 20 | * Special Autoloader for Proxy classes, which are not PSR-0 compliant. | ||
| 21 | */ | ||
| 22 | final class Autoloader | ||
| 23 | { | ||
| 24 | /** | ||
| 25 | * Resolves proxy class name to a filename based on the following pattern. | ||
| 26 | * | ||
| 27 | * 1. Remove Proxy namespace from class name. | ||
| 28 | * 2. Remove namespace separators from remaining class name. | ||
| 29 | * 3. Return PHP filename from proxy-dir with the result from 2. | ||
| 30 | * | ||
| 31 | * @psalm-param class-string $className | ||
| 32 | * | ||
| 33 | * @throws NotAProxyClass | ||
| 34 | */ | ||
| 35 | public static function resolveFile(string $proxyDir, string $proxyNamespace, string $className): string | ||
| 36 | { | ||
| 37 | if (! str_starts_with($className, $proxyNamespace)) { | ||
| 38 | throw new NotAProxyClass($className, $proxyNamespace); | ||
| 39 | } | ||
| 40 | |||
| 41 | // remove proxy namespace from class name | ||
| 42 | $classNameRelativeToProxyNamespace = substr($className, strlen($proxyNamespace)); | ||
| 43 | |||
| 44 | // remove namespace separators from remaining class name | ||
| 45 | $fileName = str_replace('\\', '', $classNameRelativeToProxyNamespace); | ||
| 46 | |||
| 47 | return $proxyDir . DIRECTORY_SEPARATOR . $fileName . '.php'; | ||
| 48 | } | ||
| 49 | |||
| 50 | /** | ||
| 51 | * Registers and returns autoloader callback for the given proxy dir and namespace. | ||
| 52 | * | ||
| 53 | * @param Closure(string, string, class-string): void|null $notFoundCallback Invoked when the proxy file is not found. | ||
| 54 | * | ||
| 55 | * @return Closure(string): void | ||
| 56 | */ | ||
| 57 | public static function register( | ||
| 58 | string $proxyDir, | ||
| 59 | string $proxyNamespace, | ||
| 60 | Closure|null $notFoundCallback = null, | ||
| 61 | ): Closure { | ||
| 62 | $proxyNamespace = ltrim($proxyNamespace, '\\'); | ||
| 63 | |||
| 64 | $autoloader = /** @param class-string $className */ static function (string $className) use ($proxyDir, $proxyNamespace, $notFoundCallback): void { | ||
| 65 | if ($proxyNamespace === '') { | ||
| 66 | return; | ||
| 67 | } | ||
| 68 | |||
| 69 | if (! str_starts_with($className, $proxyNamespace)) { | ||
| 70 | return; | ||
| 71 | } | ||
| 72 | |||
| 73 | $file = Autoloader::resolveFile($proxyDir, $proxyNamespace, $className); | ||
| 74 | |||
| 75 | if ($notFoundCallback && ! file_exists($file)) { | ||
| 76 | $notFoundCallback($proxyDir, $proxyNamespace, $className); | ||
| 77 | } | ||
| 78 | |||
| 79 | require $file; | ||
| 80 | }; | ||
| 81 | |||
| 82 | spl_autoload_register($autoloader); | ||
| 83 | |||
| 84 | return $autoloader; | ||
| 85 | } | ||
| 86 | } | ||
