ClassLoader.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\core;
12 
13 /**
14  * ClassLoader tries to load missing class definitions.
15  *
16  * @author ingo herwig <ingo@wemove.com>
17  */
18 class ClassLoader {
19 
20  private $baseDir = '';
21 
22  /**
23  * Constructor.
24  * @param $baseDir Base directory from which namespaces will be resolved
25  * (usually WCMF_BASE)
26  */
27  public function __construct($baseDir) {
28  if (!file_exists($baseDir) || is_file($baseDir)) {
29  throw new \Exception("Base dir '".$baseDir."' is not a directory.");
30  }
31  $baseDir = preg_replace('/\/\/$/', '/', $baseDir.'/');
32  $this->baseDir = $baseDir;
33  spl_autoload_register([$this, 'load'], true, true);
34  }
35 
36  /**
37  * Load the given class definition
38  * @param $className The class name
39  */
40  public function load($className) {
41  // search under baseDir assuming that namespaces match directories
42  $filename = $this->baseDir.str_replace("\\", "/", $className).'.php';
43  if (file_exists($filename)) {
44  include($filename);
45  }
46  }
47 }
48 ?>
ClassLoader tries to load missing class definitions.
Definition: ClassLoader.php:18
load($className)
Load the given class definition.
Definition: ClassLoader.php:40
Core classes.
Definition: namespaces.php:11
__construct($baseDir)
Constructor.
Definition: ClassLoader.php:27