MapClassLoader.php 1.7 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. @trigger_error('The '.__NAMESPACE__.'\MapClassLoader class is deprecated since Symfony 3.3 and will be removed in 4.0. Use Composer instead.', \E_USER_DEPRECATED);
  12. /**
  13. * A class loader that uses a mapping file to look up paths.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. *
  17. * @deprecated since version 3.3, to be removed in 4.0.
  18. */
  19. class MapClassLoader
  20. {
  21. private $map = [];
  22. /**
  23. * @param array $map A map where keys are classes and values the absolute file path
  24. */
  25. public function __construct(array $map)
  26. {
  27. $this->map = $map;
  28. }
  29. /**
  30. * Registers this instance as an autoloader.
  31. *
  32. * @param bool $prepend Whether to prepend the autoloader or not
  33. */
  34. public function register($prepend = false)
  35. {
  36. spl_autoload_register([$this, 'loadClass'], true, $prepend);
  37. }
  38. /**
  39. * Loads the given class or interface.
  40. *
  41. * @param string $class The name of the class
  42. */
  43. public function loadClass($class)
  44. {
  45. if (isset($this->map[$class])) {
  46. require $this->map[$class];
  47. }
  48. }
  49. /**
  50. * Finds the path to the file where the class is defined.
  51. *
  52. * @param string $class The name of the class
  53. *
  54. * @return string|null The path, if found
  55. */
  56. public function findFile($class)
  57. {
  58. return isset($this->map[$class]) ? $this->map[$class] : null;
  59. }
  60. }