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