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