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: * @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 NAutoLoader extends NObject
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: /**
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 InvalidStateException("Unable to load class or interface '$type'.");
41: }
42: }
43: }
44:
45:
46:
47: /**
48: * Return all registered autoloaders.
49: * @return array of AutoLoader
50: */
51: final public static function getLoaders()
52: {
53: return array_values(self::$loaders);
54: }
55:
56:
57:
58: /**
59: * Register autoloader.
60: * @return void
61: */
62: public function register()
63: {
64: if (!function_exists('spl_autoload_register')) {
65: throw new RuntimeException('spl_autoload does not exist in this PHP installation.');
66: }
67:
68: spl_autoload_register(array($this, 'tryLoad'));
69: self::$loaders[spl_object_hash($this)] = $this;
70: }
71:
72:
73:
74: /**
75: * Unregister autoloader.
76: * @return bool
77: */
78: public function unregister()
79: {
80: unset(self::$loaders[spl_object_hash($this)]);
81: return spl_autoload_unregister(array($this, 'tryLoad'));
82: }
83:
84:
85:
86: /**
87: * Handles autoloading of classes or interfaces.
88: * @param string
89: * @return void
90: */
91: abstract public function tryLoad($type);
92:
93: }
94: