ClassMap.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Elgg;
  3. /**
  4. * A map of class names to absolute file paths
  5. *
  6. * @access private
  7. *
  8. * @package Elgg.Core
  9. * @subpackage Autoloader
  10. */
  11. class ClassMap {
  12. /**
  13. * @var array
  14. */
  15. protected $map = array();
  16. /**
  17. * @var bool
  18. */
  19. protected $altered = false;
  20. /**
  21. * Get the path for a class/interface/trait
  22. *
  23. * @param string $class a class/interface/trait name
  24. * @return string the file path or empty string
  25. */
  26. public function getPath($class) {
  27. if ('\\' === $class[0]) {
  28. $class = substr($class, 1);
  29. }
  30. return isset($this->map[$class]) ? $this->map[$class] : "";
  31. }
  32. /**
  33. * Set the path for a class/interface/trait, and mark map as altered
  34. *
  35. * @param string $class a class/interface/trait name
  36. * @param string $path absolute file path
  37. * @return \Elgg\ClassMap
  38. */
  39. public function setPath($class, $path) {
  40. if ('\\' === $class[0]) {
  41. $class = substr($class, 1);
  42. }
  43. $this->map[$class] = $path;
  44. $this->altered = true;
  45. return $this;
  46. }
  47. /**
  48. * Was this map altered by the class loader?
  49. *
  50. * @return bool
  51. */
  52. public function getAltered() {
  53. return $this->altered;
  54. }
  55. /**
  56. * Set the altered flag
  57. *
  58. * @param bool $altered Whether the class map has been altered
  59. * @return \Elgg\ClassMap
  60. */
  61. public function setAltered($altered) {
  62. $this->altered = (bool) $altered;
  63. return $this;
  64. }
  65. /**
  66. * Get the full map
  67. *
  68. * @return array
  69. */
  70. public function getMap() {
  71. return $this->map;
  72. }
  73. /**
  74. * Set the full map
  75. *
  76. * @param array $map array with keys being class/interface/trait names and
  77. * values the absolute file paths that define them
  78. * @return \Elgg\ClassMap
  79. */
  80. public function setMap(array $map) {
  81. $this->map = $map;
  82. return $this;
  83. }
  84. /**
  85. * Merge a class map with the current map
  86. *
  87. * @param array $map array with keys being class/interface/trait names and
  88. * values the absolute file paths that define them
  89. * @return \Elgg\ClassMap
  90. */
  91. public function mergeMap(array $map) {
  92. $this->map = array_merge($this->map, $map);
  93. return $this;
  94. }
  95. }