AutoLoader.class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Class to handle autoloading other classes
  4. *
  5. * @author Christopher Han <xiphux@gmail.com>
  6. * @copyright Copyright (c) 2012 Christopher Han
  7. * @package GitPHP
  8. */
  9. class GitPHP_AutoLoader
  10. {
  11. /**
  12. * Autoload a class
  13. *
  14. * @param string $classname class name
  15. */
  16. public static function AutoLoad($classname)
  17. {
  18. $filename = GitPHP_AutoLoader::ClassFilename($classname);
  19. if (empty($filename))
  20. return;
  21. $path = dirname(__FILE__) . '/' . $filename;
  22. if (is_readable($path))
  23. require($path);
  24. }
  25. /**
  26. * Get the path to a class
  27. *
  28. * @param string $classname class name
  29. * @return string path
  30. */
  31. public static function ClassFilename($classname)
  32. {
  33. if (empty($classname))
  34. return null;
  35. if (strncmp($classname, 'GitPHP_', 7) !== 0)
  36. return null;
  37. $classname = substr($classname, 7);
  38. $path = '';
  39. if (strncmp($classname, 'Controller', 10) === 0) {
  40. $path = 'controller/';
  41. } else if (strncmp($classname, 'ProjectList', 11) === 0) {
  42. $path = 'git/projectlist/';
  43. } else if (strncmp($classname, 'FileMimeType', 12) === 0) {
  44. $path = 'git/filemimetype/';
  45. } else if (strncmp($classname, 'RefList', 7) === 0) {
  46. $path = 'git/reflist/';
  47. } else if (strncmp($classname, 'TagList', 7) === 0) {
  48. $path = 'git/taglist/';
  49. } else if (strncmp($classname, 'HeadList', 8) === 0) {
  50. $path = 'git/headlist/';
  51. } else if (strncmp($classname, 'RevList', 7) === 0) {
  52. $path = 'git/revlist/';
  53. } else if (($classname == 'Project') || (strncmp($classname, 'ProjectLoad', 11) === 0)) {
  54. $path = 'git/project/';
  55. } else if (($classname == 'Blob') || (strncmp($classname, 'BlobLoad', 8) === 0)) {
  56. $path = 'git/blob/';
  57. } else if (($classname == 'Commit') || (strncmp($classname, 'CommitLoad', 10) === 0)) {
  58. $path = 'git/commit/';
  59. } else if (($classname == 'Tag') || (strncmp($classname, 'TagLoad', 7) === 0)) {
  60. $path = 'git/tag/';
  61. } else if (($classname == 'Tree') || (strncmp($classname, 'TreeLoad', 8) === 0)) {
  62. $path = 'git/tree/';
  63. } else if (($classname == 'Log') || (strncmp($classname, 'LogLoad', 7) === 0)) {
  64. $path = 'git/log/';
  65. } else if (strncmp($classname, 'Archive', 7) === 0) {
  66. $path = 'git/archive/';
  67. } else if (strncmp($classname, 'Pack', 4) === 0) {
  68. $path = 'git/pack/';
  69. } else if ((strlen($classname) > 9) && (substr_compare($classname, 'Exception', -9, 9) === 0)) {
  70. $path = 'exception/';
  71. } else if (strpos($classname, 'Cache') !== false) {
  72. $path = 'cache/';
  73. } else if (strncmp($classname, 'Route', 5) === 0) {
  74. $path = 'router/';
  75. } else if (strncmp($classname, 'User', 4) === 0) {
  76. $path = 'auth/';
  77. } else if (in_array($classname, array(
  78. 'Config',
  79. 'DebugLog',
  80. 'DebugAutoLog',
  81. 'Resource',
  82. 'Util'
  83. ))) {
  84. $path = '';
  85. } else {
  86. $path = 'git/';
  87. }
  88. if ((strlen($classname) > 10) && (substr_compare($classname, '_Interface', -10, 10) === 0)) {
  89. $classname = substr($classname, 0, -10);
  90. $path .= $classname . '.interface.php';
  91. } else {
  92. $path .= $classname . '.class.php';
  93. }
  94. return $path;
  95. }
  96. }