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:         class_exists($type);
  53. 53:     }
  54. 54:  
  55. 55:  
  56. 56:  
  57. 57:     /**
  58. 58:      * Return all registered autoloaders.
  59. 59:      * @return array of AutoLoader
  60. 60:      */
  61. 61:     final public static function getLoaders()
  62. 62:     {
  63. 63:         return array_values(self::$loaders);
  64. 64:     }
  65. 65:  
  66. 66:  
  67. 67:  
  68. 68:     /**
  69. 69:      * Register autoloader.
  70. 70:      * @return void 
  71. 71:      */
  72. 72:     public function register()
  73. 73:     {
  74. 74:         if (!function_exists('spl_autoload_register')) {
  75. 75:             throw new RuntimeException('spl_autoload does not exist in this PHP installation.');
  76. 76:         }
  77. 77:  
  78. 78:         spl_autoload_register(array($this'tryLoad'));
  79. 79:         self::$loaders[spl_object_hash($this)$this;
  80. 80:     }
  81. 81:  
  82. 82:  
  83. 83:  
  84. 84:     /**
  85. 85:      * Unregister autoloader.
  86. 86:      * @return bool 
  87. 87:      */
  88. 88:     public function unregister()
  89. 89:     {
  90. 90:         unset(self::$loaders[spl_object_hash($this)]);
  91. 91:         return spl_autoload_unregister(array($this'tryLoad'));
  92. 92:     }
  93. 93:  
  94. 94:  
  95. 95:  
  96. 96:     /**
  97. 97:      * Handles autoloading of classes or interfaces.
  98. 98:      * @param  string 
  99. 99:      * @return void 
  100. 100:      */
  101. 101:     abstract public function tryLoad($type);
  102. 102: