Blame view

framework/core/manifest/ClassLoader.php 2.44 KB
0084d336   Administrator   Importers CRUD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  <?php
  /**
   * A class that handles loading classes and interfaces from a class manifest
   * instance.
   *
   * @package framework
   * @subpackage manifest
   */
  class SS_ClassLoader {
  
  	/**
  	 * @var SS_ClassLoader
  	 */
  	private static $instance;
  
  	/**
  	 * @var array Map of 'instance' (SS_ClassManifest) and other options.
  	 */
  	protected $manifests = array();
  
  	/**
  	 * @return SS_ClassLoader
  	 */
  	public static function instance() {
  		return self::$instance ? self::$instance : self::$instance = new self();
  	}
  
  	/**
  	 * Returns the currently active class manifest instance that is used for
  	 * loading classes.
  	 *
  	 * @return SS_ClassManifest
  	 */
  	public function getManifest() {
  		return $this->manifests[count($this->manifests) - 1]['instance'];
  	}
  	
  	/**
  	 * Returns true if this class loader has a manifest.
  	 */
  	public function hasManifest() {
  		return (bool)$this->manifests;
  	}
  
  	/**
  	 * Pushes a class manifest instance onto the top of the stack.
  	 *
  	 * @param SS_ClassManifest $manifest
  	 * @param Boolean Marks the manifest as exclusive. If set to FALSE, will
  	 * look for classes in earlier manifests as well.
  	 */
  	public function pushManifest(SS_ClassManifest $manifest, $exclusive = true) {
  		$this->manifests[] = array('exclusive' => $exclusive, 'instance' => $manifest);
  	}
  
  	/**
  	 * @return SS_ClassManifest
  	 */
  	public function popManifest() {
  		$manifest = array_pop($this->manifests);
  		return $manifest['instance'];
  	}
  
  	public function registerAutoloader() {
  		spl_autoload_register(array($this, 'loadClass'));
  	}
  
  	/**
  	 * Loads a class or interface if it is present in the currently active
  	 * manifest.
  	 *
  	 * @param string $class
  	 * @return String
  	 */
  	public function loadClass($class) {
  		if ($path = $this->getItemPath($class)) {
  			require_once $path;
  		}
  		return $path;
  	}
  	
  	/**
  	 * Returns the path for a class or interface in the currently active manifest,
  	 * or any previous ones if later manifests aren't set to "exclusive".
  	 * 
  	 * @return String
  	 */
  	public function getItemPath($class) {
  		foreach(array_reverse($this->manifests) as $manifest) {
  			$manifestInst = $manifest['instance'];
  			if ($path = $manifestInst->getItemPath($class)) return $path;
  			if($manifest['exclusive']) break;
  		}
  		return false;
  	}
  
  	/**
  	 * Returns true if a class or interface name exists in the manifest.
  	 *
  	 * @param  string $class
  	 * @return bool
  	 */
  	public function classExists($class) {
  		return class_exists($class, false) || $this->getItemPath($class);
  	}
  
  }