Elgg  Version 1.9
ClassLoader.php
Go to the documentation of this file.
1 <?php
48 
49  protected $namespaces = array();
50  protected $prefixes = array();
51  protected $fallbacks = array();
52 
56  protected $map;
57 
63  public function __construct(Elgg_ClassMap $map) {
64  $this->map = $map;
65  }
66 
72  public function getClassMap() {
73  return $this->map;
74  }
75 
81  public function getNamespaces() {
82  return $this->namespaces;
83  }
84 
90  public function getPrefixes() {
91  return $this->prefixes;
92  }
93 
100  public function registerNamespaces(array $namespaces) {
101  foreach ($namespaces as $namespace => $locations) {
102  $this->namespaces[$namespace] = (array)$locations;
103  }
104  }
105 
113  public function registerNamespace($namespace, $paths) {
114  $this->namespaces[$namespace] = (array)$paths;
115  }
116 
123  public function registerPrefixes(array $classes) {
124  foreach ($classes as $prefix => $locations) {
125  $this->prefixes[$prefix] = (array)$locations;
126  }
127  }
128 
136  public function registerPrefix($prefix, $paths) {
137  $this->prefixes[$prefix] = (array)$paths;
138  }
139 
146  public function addFallback($path) {
147  $this->fallbacks[] = rtrim($path, '/\\');
148  }
149 
155  public function register() {
156  spl_autoload_register(array($this, 'loadClass'));
157  }
158 
165  public function loadClass($class) {
166  $file = $this->map->getPath($class);
167  if ($file && is_readable($file)) {
168  require $file;
169  return;
170  }
171 
172  $file = $this->findFile($class);
173  if ($file && is_readable($file)) {
174  $this->map->setPath($class, $file);
175  $this->map->setAltered(true);
176  require $file;
177  }
178  }
179 
187  public function findFile($class) {
188  if ('\\' == $class[0]) {
189  $class = substr($class, 1);
190  }
191 
192  $pos = strrpos($class, '\\');
193  if (false !== $pos) {
194  // namespaced class name
195  $namespace = substr($class, 0, $pos);
196  $className = substr($class, $pos + 1);
197  $normalizedClass = str_replace('\\', DIRECTORY_SEPARATOR, $namespace)
198  . DIRECTORY_SEPARATOR
199  . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
200  foreach ($this->namespaces as $ns => $dirs) {
201  if (0 !== strpos($namespace, $ns)) {
202  continue;
203  }
204 
205  foreach ($dirs as $dir) {
206  $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
207  if (is_file($file)) {
208  return $file;
209  }
210  }
211  }
212 
213  } else {
214  // PEAR-like class name
215  $normalizedClass = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
216  foreach ($this->prefixes as $prefix => $dirs) {
217  if (0 !== strpos($class, $prefix)) {
218  continue;
219  }
220 
221  foreach ($dirs as $dir) {
222  $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
223  if (is_file($file)) {
224  return $file;
225  }
226  }
227  }
228  }
229 
230  foreach ($this->fallbacks as $dir) {
231  $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
232  if (is_file($file)) {
233  return $file;
234  }
235  }
236  }
237 }
addFallback($path)
Add a directory to search if no registered directory is found.
registerNamespaces(array $namespaces)
Registers an array of namespaces.
if(isset($vars['id'])) $class
Definition: ajax_loader.php:19
getClassMap()
Get the class map.
Definition: ClassLoader.php:72
getNamespaces()
Gets the configured namespaces.
Definition: ClassLoader.php:81
$classes
Definition: full.php:29
elgg require
Throw an error if the required package isn&#39;t present.
Definition: elgglib.js:164
__construct(Elgg_ClassMap $map)
Constructor.
Definition: ClassLoader.php:63
loadClass($class)
Loads the given class or interface, possibly updating the class map.
getPrefixes()
Gets the configured class prefixes.
Definition: ClassLoader.php:90
findFile($class)
Finds the path to the file where the class is defined.
registerPrefix($prefix, $paths)
Registers a set of classes using the PEAR naming convention.
registerPrefixes(array $classes)
Registers an array of classes using the PEAR naming convention.
$path
Definition: invalid.php:17
registerNamespace($namespace, $paths)
Registers a namespace.