Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\ClassLoader;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * A class loader that uses a mapping file to look up paths.
|
Chris@0
|
16 *
|
Chris@0
|
17 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
18 */
|
Chris@0
|
19 class MapClassLoader
|
Chris@0
|
20 {
|
Chris@0
|
21 private $map = array();
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * Constructor.
|
Chris@0
|
25 *
|
Chris@0
|
26 * @param array $map A map where keys are classes and values the absolute file path
|
Chris@0
|
27 */
|
Chris@0
|
28 public function __construct(array $map)
|
Chris@0
|
29 {
|
Chris@0
|
30 $this->map = $map;
|
Chris@0
|
31 }
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * Registers this instance as an autoloader.
|
Chris@0
|
35 *
|
Chris@0
|
36 * @param bool $prepend Whether to prepend the autoloader or not
|
Chris@0
|
37 */
|
Chris@0
|
38 public function register($prepend = false)
|
Chris@0
|
39 {
|
Chris@0
|
40 spl_autoload_register(array($this, 'loadClass'), true, $prepend);
|
Chris@0
|
41 }
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * Loads the given class or interface.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @param string $class The name of the class
|
Chris@0
|
47 */
|
Chris@0
|
48 public function loadClass($class)
|
Chris@0
|
49 {
|
Chris@0
|
50 if (isset($this->map[$class])) {
|
Chris@0
|
51 require $this->map[$class];
|
Chris@0
|
52 }
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * Finds the path to the file where the class is defined.
|
Chris@0
|
57 *
|
Chris@0
|
58 * @param string $class The name of the class
|
Chris@0
|
59 *
|
Chris@0
|
60 * @return string|null The path, if found
|
Chris@0
|
61 */
|
Chris@0
|
62 public function findFile($class)
|
Chris@0
|
63 {
|
Chris@0
|
64 if (isset($this->map[$class])) {
|
Chris@0
|
65 return $this->map[$class];
|
Chris@0
|
66 }
|
Chris@0
|
67 }
|
Chris@0
|
68 }
|