12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- namespace Doctrine\Common\Proxy;
- use Doctrine\Common\Proxy\Exception\InvalidArgumentException;
- class Autoloader
- {
-
- public static function resolveFile($proxyDir, $proxyNamespace, $className)
- {
- if (0 !== strpos($className, $proxyNamespace)) {
- throw InvalidArgumentException::notProxyClass($className, $proxyNamespace);
- }
-
- $classNameRelativeToProxyNamespace = substr($className, strlen($proxyNamespace));
-
- $fileName = str_replace('\\', '', $classNameRelativeToProxyNamespace);
- return $proxyDir . DIRECTORY_SEPARATOR . $fileName . '.php';
- }
-
- public static function register($proxyDir, $proxyNamespace, $notFoundCallback = null)
- {
- $proxyNamespace = ltrim($proxyNamespace, '\\');
- if ( ! (null === $notFoundCallback || is_callable($notFoundCallback))) {
- throw InvalidArgumentException::invalidClassNotFoundCallback($notFoundCallback);
- }
- $autoloader = function ($className) use ($proxyDir, $proxyNamespace, $notFoundCallback) {
- if (0 === strpos($className, $proxyNamespace)) {
- $file = Autoloader::resolveFile($proxyDir, $proxyNamespace, $className);
- if ($notFoundCallback && ! file_exists($file)) {
- call_user_func($notFoundCallback, $proxyDir, $proxyNamespace, $className);
- }
- require $file;
- }
- };
- spl_autoload_register($autoloader);
- return $autoloader;
- }
- }
|