1 | <?php |
||
12 | final class Mapper |
||
13 | { |
||
14 | /** |
||
15 | * @param string $path |
||
16 | * @param string $namespace |
||
17 | * @return array |
||
18 | */ |
||
19 | 1 | public static function mapInstantiated($path, $namespace) |
|
20 | { |
||
21 | 1 | $mapping = []; |
|
22 | |||
23 | 1 | foreach (self::map($path, $namespace) as $command => $handler) { |
|
24 | 1 | $mapping[$command] = new $handler(); |
|
25 | 1 | } |
|
26 | |||
27 | 1 | return $mapping; |
|
28 | } |
||
29 | |||
30 | /** |
||
31 | * @param string $path |
||
32 | * @param string $namespace |
||
33 | * @return array |
||
34 | */ |
||
35 | 2 | public static function map($path, $namespace) |
|
36 | { |
||
37 | 2 | $reader = new AnnotationReader(); |
|
38 | 2 | $mapping = []; |
|
39 | |||
40 | 2 | $directory = new RecursiveDirectoryIterator($path); |
|
41 | 2 | $directory = new RecursiveIteratorIterator($directory); |
|
42 | |||
43 | 2 | foreach ($directory as $node) { |
|
44 | 2 | if (!is_file($node->getPathname())) { |
|
45 | 2 | continue; |
|
46 | } |
||
47 | |||
48 | 2 | $file = substr($node->getPathname(), strlen($path)); |
|
49 | 2 | $file = ltrim($file, DIRECTORY_SEPARATOR); |
|
50 | 2 | $file = rtrim($file, '.php'); |
|
51 | |||
52 | 2 | $class = $namespace . '\\' . str_replace(DIRECTORY_SEPARATOR, '\\', $file); |
|
53 | |||
54 | 2 | if (!class_exists($class)) { |
|
55 | continue; |
||
56 | } |
||
57 | |||
58 | 2 | $handler = self::getHandlerByCommand($class, $reader); |
|
59 | |||
60 | 2 | if (!class_exists($handler)) { |
|
61 | continue; |
||
62 | } |
||
63 | |||
64 | 2 | $mapping[$class] = $handler; |
|
65 | 2 | } |
|
66 | |||
67 | 2 | return $mapping; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param string $command |
||
72 | * @param Reader $reader |
||
73 | * @return string |
||
74 | */ |
||
75 | 4 | public static function getHandlerByCommand($command, Reader $reader) |
|
85 | } |
||
86 |