MapFileClassLoader.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. require_once __DIR__.'/ClassLoaderInterface.php';
  12. /**
  13. * A class loader that uses a mapping file to look up paths.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. *
  17. * @api
  18. */
  19. class MapFileClassLoader implements ClassLoaderInterface
  20. {
  21. private $map = array();
  22. /**
  23. * Constructor.
  24. *
  25. * @param string $file Path to class mapping file
  26. *
  27. * @api
  28. */
  29. public function __construct($file)
  30. {
  31. $this->map = require $file;
  32. }
  33. /**
  34. * Registers this instance as an autoloader.
  35. *
  36. * @param Boolean $prepend Whether to prepend the autoloader or not
  37. *
  38. * @api
  39. */
  40. public function register($prepend = false)
  41. {
  42. spl_autoload_register(array($this, 'loadClass'), true, $prepend);
  43. }
  44. /**
  45. * Loads the given class or interface.
  46. *
  47. * @param string $class The name of the class
  48. */
  49. public function loadClass($class)
  50. {
  51. if ('\\' === $class[0]) {
  52. $class = substr($class, 1);
  53. }
  54. if (isset($this->map[$class])) {
  55. require $this->map[$class];
  56. }
  57. }
  58. /**
  59. * Finds the path to the file where the class is defined.
  60. *
  61. * @param string $class The name of the class
  62. *
  63. * @return string|null The path, if found
  64. */
  65. public function findFile($class)
  66. {
  67. if ('\\' === $class[0]) {
  68. $class = substr($class, 1);
  69. }
  70. if (isset($this->map[$class])) {
  71. return $this->map[$class];
  72. }
  73. }
  74. }