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