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