Source for file AutoLoader.php

Documentation is available at AutoLoader.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette\Loaders
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * Auto loader is responsible for loading classes and interfaces.
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  19. 19:  * @package    Nette\Loaders
  20. 20:  */
  21. 21: abstract class AutoLoader extends Object
  22. 22: {
  23. 23:     /** @var array  list of registered loaders */
  24. 24:     static private $loaders array();
  25. 25:  
  26. 26:     /** @var int  for profiling purposes */
  27. 27:     public static $count 0;
  28. 28:  
  29. 29:  
  30. 30:  
  31. 31:     /**
  32. 32:      * Try to load the requested class.
  33. 33:      * @param  string  class/interface name
  34. 34:      * @return void 
  35. 35:      */
  36. 36:     final public static function load($type)
  37. 37:     {
  38. 38:         foreach (func_get_args(as $type{
  39. 39:             if (!class_exists($type)) {
  40. 40:                 throw new InvalidStateException("Unable to load class or interface '$type'.");
  41. 41:             }
  42. 42:         }
  43. 43:     }
  44. 44:  
  45. 45:  
  46. 46:  
  47. 47:     /**
  48. 48:      * Return all registered autoloaders.
  49. 49:      * @return array of AutoLoader
  50. 50:      */
  51. 51:     final public static function getLoaders()
  52. 52:     {
  53. 53:         return array_values(self::$loaders);
  54. 54:     }
  55. 55:  
  56. 56:  
  57. 57:  
  58. 58:     /**
  59. 59:      * Register autoloader.
  60. 60:      * @return void 
  61. 61:      */
  62. 62:     public function register()
  63. 63:     {
  64. 64:         if (!function_exists('spl_autoload_register')) {
  65. 65:             throw new RuntimeException('spl_autoload does not exist in this PHP installation.');
  66. 66:         }
  67. 67:  
  68. 68:         spl_autoload_register(array($this'tryLoad'));
  69. 69:         self::$loaders[spl_object_hash($this)$this;
  70. 70:     }
  71. 71:  
  72. 72:  
  73. 73:  
  74. 74:     /**
  75. 75:      * Unregister autoloader.
  76. 76:      * @return bool 
  77. 77:      */
  78. 78:     public function unregister()
  79. 79:     {
  80. 80:         unset(self::$loaders[spl_object_hash($this)]);
  81. 81:         return spl_autoload_unregister(array($this'tryLoad'));
  82. 82:     }
  83. 83:  
  84. 84:  
  85. 85:  
  86. 86:     /**
  87. 87:      * Handles autoloading of classes or interfaces.
  88. 88:      * @param  string 
  89. 89:      * @return void 
  90. 90:      */
  91. 91:     abstract public function tryLoad($type);
  92. 92:  
  93. 93: }