Autoloader.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Doctrine\Common\Proxy;
  3. use Closure;
  4. use Doctrine\Common\Proxy\Exception\InvalidArgumentException;
  5. use function call_user_func;
  6. use function file_exists;
  7. use function is_callable;
  8. use function ltrim;
  9. use function spl_autoload_register;
  10. use function str_replace;
  11. use function strlen;
  12. use function strpos;
  13. use function substr;
  14. use const DIRECTORY_SEPARATOR;
  15. /**
  16. * Special Autoloader for Proxy classes, which are not PSR-0 compliant.
  17. *
  18. * @internal
  19. */
  20. class Autoloader
  21. {
  22. /**
  23. * Resolves proxy class name to a filename based on the following pattern.
  24. *
  25. * 1. Remove Proxy namespace from class name.
  26. * 2. Remove namespace separators from remaining class name.
  27. * 3. Return PHP filename from proxy-dir with the result from 2.
  28. *
  29. * @param string $proxyDir
  30. * @param string $proxyNamespace
  31. * @param string $className
  32. *
  33. * @return string
  34. *
  35. * @throws InvalidArgumentException
  36. *
  37. * @psalm-param class-string $className
  38. */
  39. public static function resolveFile($proxyDir, $proxyNamespace, $className)
  40. {
  41. if (strpos($className, $proxyNamespace) !== 0) {
  42. throw InvalidArgumentException::notProxyClass($className, $proxyNamespace);
  43. }
  44. // remove proxy namespace from class name
  45. $classNameRelativeToProxyNamespace = substr($className, strlen($proxyNamespace));
  46. // remove namespace separators from remaining class name
  47. $fileName = str_replace('\\', '', $classNameRelativeToProxyNamespace);
  48. return $proxyDir . DIRECTORY_SEPARATOR . $fileName . '.php';
  49. }
  50. /**
  51. * Registers and returns autoloader callback for the given proxy dir and namespace.
  52. *
  53. * @param string $proxyDir
  54. * @param string $proxyNamespace
  55. * @param callable|null $notFoundCallback Invoked when the proxy file is not found.
  56. *
  57. * @return Closure
  58. *
  59. * @throws InvalidArgumentException
  60. */
  61. public static function register($proxyDir, $proxyNamespace, $notFoundCallback = null)
  62. {
  63. $proxyNamespace = ltrim($proxyNamespace, '\\');
  64. if ($notFoundCallback !== null && ! is_callable($notFoundCallback)) {
  65. throw InvalidArgumentException::invalidClassNotFoundCallback($notFoundCallback);
  66. }
  67. $autoloader = static function ($className) use ($proxyDir, $proxyNamespace, $notFoundCallback) {
  68. if ($proxyNamespace === '') {
  69. return;
  70. }
  71. if (strpos($className, $proxyNamespace) !== 0) {
  72. return;
  73. }
  74. $file = Autoloader::resolveFile($proxyDir, $proxyNamespace, $className);
  75. if ($notFoundCallback && ! file_exists($file)) {
  76. call_user_func($notFoundCallback, $proxyDir, $proxyNamespace, $className);
  77. }
  78. require $file;
  79. };
  80. spl_autoload_register($autoloader);
  81. return $autoloader;
  82. }
  83. }