MapClassLoader.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. * @param array $map A map where keys are classes and values the absolute file path
  21. */
  22. public function __construct(array $map)
  23. {
  24. $this->map = $map;
  25. }
  26. /**
  27. * Registers this instance as an autoloader.
  28. *
  29. * @param bool $prepend Whether to prepend the autoloader or not
  30. */
  31. public function register($prepend = false)
  32. {
  33. spl_autoload_register(array($this, 'loadClass'), true, $prepend);
  34. }
  35. /**
  36. * Loads the given class or interface.
  37. *
  38. * @param string $class The name of the class
  39. */
  40. public function loadClass($class)
  41. {
  42. if (isset($this->map[$class])) {
  43. require $this->map[$class];
  44. }
  45. }
  46. /**
  47. * Finds the path to the file where the class is defined.
  48. *
  49. * @param string $class The name of the class
  50. *
  51. * @return string|null The path, if found
  52. */
  53. public function findFile($class)
  54. {
  55. if (isset($this->map[$class])) {
  56. return $this->map[$class];
  57. }
  58. }
  59. }