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