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