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