Autoloader.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Proxy;
  20. use Doctrine\Common\Proxy\Exception\InvalidArgumentException;
  21. /**
  22. * Special Autoloader for Proxy classes, which are not PSR-0 compliant.
  23. *
  24. * @author Benjamin Eberlei <kontakt@beberlei.de>
  25. */
  26. class Autoloader
  27. {
  28. /**
  29. * Resolves proxy class name to a filename based on the following pattern.
  30. *
  31. * 1. Remove Proxy namespace from class name.
  32. * 2. Remove namespace separators from remaining class name.
  33. * 3. Return PHP filename from proxy-dir with the result from 2.
  34. *
  35. * @param string $proxyDir
  36. * @param string $proxyNamespace
  37. * @param string $className
  38. *
  39. * @return string
  40. *
  41. * @throws InvalidArgumentException
  42. */
  43. public static function resolveFile($proxyDir, $proxyNamespace, $className)
  44. {
  45. if (0 !== strpos($className, $proxyNamespace)) {
  46. throw InvalidArgumentException::notProxyClass($className, $proxyNamespace);
  47. }
  48. $className = str_replace('\\', '', substr($className, strlen($proxyNamespace) + 1));
  49. return $proxyDir . DIRECTORY_SEPARATOR . $className . '.php';
  50. }
  51. /**
  52. * Registers and returns autoloader callback for the given proxy dir and namespace.
  53. *
  54. * @param string $proxyDir
  55. * @param string $proxyNamespace
  56. * @param callable|null $notFoundCallback Invoked when the proxy file is not found.
  57. *
  58. * @return \Closure
  59. *
  60. * @throws InvalidArgumentException
  61. */
  62. public static function register($proxyDir, $proxyNamespace, $notFoundCallback = null)
  63. {
  64. $proxyNamespace = ltrim($proxyNamespace, '\\');
  65. if ( ! (null === $notFoundCallback || is_callable($notFoundCallback))) {
  66. throw InvalidArgumentException::invalidClassNotFoundCallback($notFoundCallback);
  67. }
  68. $autoloader = function ($className) use ($proxyDir, $proxyNamespace, $notFoundCallback) {
  69. if (0 === strpos($className, $proxyNamespace)) {
  70. $file = Autoloader::resolveFile($proxyDir, $proxyNamespace, $className);
  71. if ($notFoundCallback && ! file_exists($file)) {
  72. call_user_func($notFoundCallback, $proxyDir, $proxyNamespace, $className);
  73. }
  74. require $file;
  75. }
  76. };
  77. spl_autoload_register($autoloader);
  78. return $autoloader;
  79. }
  80. }