MapFileClassLoader.php 1.7 KB

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