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