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