Autoloader.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Proxy;
  20. /**
  21. * Special Autoloader for Proxy classes because them not being PSR-0 compatible.
  22. *
  23. * @author Benjamin Eberlei <kontakt@beberlei.de>
  24. */
  25. class Autoloader
  26. {
  27. /**
  28. * Resolve proxy class name to a filename based on the following pattern.
  29. *
  30. * 1. Remove Proxy namespace from class name
  31. * 2. Remove namespace seperators from remaining class name.
  32. * 3. Return PHP filename from proxy-dir with the result from 2.
  33. *
  34. * @param string $proxyDir
  35. * @param string $proxyNamespace
  36. * @param string $className
  37. * @return string
  38. */
  39. static public function resolveFile($proxyDir, $proxyNamespace, $className)
  40. {
  41. if (0 !== strpos($className, $proxyNamespace)) {
  42. throw ProxyException::notProxyClass($className, $proxyNamespace);
  43. }
  44. $className = str_replace('\\', '', substr($className, strlen($proxyNamespace) +1));
  45. return $proxyDir . DIRECTORY_SEPARATOR . $className.'.php';
  46. }
  47. /**
  48. * Register and return autoloader callback for the given proxy dir and
  49. * namespace.
  50. *
  51. * @param string $proxyDir
  52. * @param string $proxyNamespace
  53. * @param Closure $notFoundCallback Invoked when the proxy file is not found.
  54. * @return Closure
  55. */
  56. static public function register($proxyDir, $proxyNamespace, \Closure $notFoundCallback = null)
  57. {
  58. $proxyNamespace = ltrim($proxyNamespace, "\\");
  59. $autoloader = function($className) use ($proxyDir, $proxyNamespace, $notFoundCallback) {
  60. if (0 === strpos($className, $proxyNamespace)) {
  61. $file = Autoloader::resolveFile($proxyDir, $proxyNamespace, $className);
  62. if ($notFoundCallback && ! file_exists($file)) {
  63. $notFoundCallback($proxyDir, $proxyNamespace, $className);
  64. }
  65. require $file;
  66. }
  67. };
  68. spl_autoload_register($autoloader);
  69. return $autoloader;
  70. }
  71. }