MapClassLoader.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 bool $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 (isset($this->map[$class])) {
  45. require $this->map[$class];
  46. }
  47. }
  48. /**
  49. * Finds the path to the file where the class is defined.
  50. *
  51. * @param string $class The name of the class
  52. *
  53. * @return string|null The path, if found
  54. */
  55. public function findFile($class)
  56. {
  57. if (isset($this->map[$class])) {
  58. return $this->map[$class];
  59. }
  60. }
  61. }