MapClassLoader.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\ClassLoader;
  11. /**
  12. * A class loader that uses a mapping file to look up paths.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class MapClassLoader
  17. {
  18. private $map = array();
  19. /**
  20. * Constructor.
  21. *
  22. * @param array $map A map where keys are classes and values the absolute file path
  23. */
  24. public function __construct(array $map)
  25. {
  26. $this->map = $map;
  27. }
  28. /**
  29. * Registers this instance as an autoloader.
  30. *
  31. * @param Boolean $prepend Whether to prepend the autoloader or not
  32. */
  33. public function register($prepend = false)
  34. {
  35. spl_autoload_register(array($this, 'loadClass'), true, $prepend);
  36. }
  37. /**
  38. * Loads the given class or interface.
  39. *
  40. * @param string $class The name of the class
  41. */
  42. public function loadClass($class)
  43. {
  44. if ('\\' === $class[0]) {
  45. $class = substr($class, 1);
  46. }
  47. if (isset($this->map[$class])) {
  48. require $this->map[$class];
  49. }
  50. }
  51. /**
  52. * Finds the path to the file where the class is defined.
  53. *
  54. * @param string $class The name of the class
  55. *
  56. * @return string|null The path, if found
  57. */
  58. public function findFile($class)
  59. {
  60. if ('\\' === $class[0]) {
  61. $class = substr($class, 1);
  62. }
  63. if (isset($this->map[$class])) {
  64. return $this->map[$class];
  65. }
  66. }
  67. }