|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace WyriHaximus\Tactician\JobCommand; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
|
6
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
7
|
|
|
use Exception; |
|
8
|
|
|
use RecursiveDirectoryIterator; |
|
9
|
|
|
use RecursiveIteratorIterator; |
|
10
|
|
|
use ReflectionClass; |
|
11
|
|
|
use WyriHaximus\Tactician\JobCommand\Annotations\Job; |
|
12
|
|
|
|
|
13
|
|
|
final class Mapper |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
private $map = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param string $path |
|
22
|
|
|
* @param string $namespace |
|
23
|
|
|
* @return array |
|
24
|
|
|
*/ |
|
25
|
1 |
|
public function map(string $path, string $namespace) |
|
26
|
|
|
{ |
|
27
|
1 |
|
$reader = new AnnotationReader(); |
|
28
|
|
|
|
|
29
|
1 |
|
$directory = new RecursiveDirectoryIterator($path); |
|
30
|
1 |
|
$directory = new RecursiveIteratorIterator($directory); |
|
31
|
|
|
|
|
32
|
1 |
|
foreach ($directory as $node) { |
|
33
|
1 |
|
if (!is_file($node->getPathname())) { |
|
34
|
1 |
|
continue; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
$file = substr($node->getPathname(), strlen($path)); |
|
38
|
1 |
|
$file = ltrim($file, DIRECTORY_SEPARATOR); |
|
39
|
1 |
|
$file = rtrim($file, '.php'); |
|
40
|
|
|
|
|
41
|
1 |
|
$class = $namespace . '\\' . str_replace(DIRECTORY_SEPARATOR, '\\', $file); |
|
42
|
|
|
|
|
43
|
1 |
|
if (!class_exists($class)) { |
|
44
|
|
|
continue; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
$job = self::getJobFromCommand($class, $reader); |
|
48
|
|
|
|
|
49
|
1 |
|
if (!strlen($job) === 0) { |
|
50
|
|
|
continue; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
$this->map[$job] = $class; |
|
54
|
|
|
} |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $command |
|
59
|
|
|
* @param Reader $reader |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
3 |
|
public function getJobFromCommand(string $command, Reader $reader) |
|
63
|
|
|
{ |
|
64
|
3 |
|
$annotation = $reader->getClassAnnotation(new ReflectionClass($command), Job::class); |
|
65
|
|
|
|
|
66
|
3 |
|
if (!($annotation instanceof Job)) { |
|
67
|
1 |
|
return ''; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
2 |
|
return $annotation->getJob(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $job |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function hasCommand(string $job) |
|
78
|
|
|
{ |
|
79
|
1 |
|
return isset($this->map[$job]); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string $job |
|
84
|
|
|
* @return mixed |
|
85
|
|
|
* @throws Exception |
|
86
|
|
|
*/ |
|
87
|
2 |
|
public function getCommand(string $job) |
|
88
|
|
|
{ |
|
89
|
2 |
|
if (isset($this->map[$job])) { |
|
90
|
1 |
|
return $this->map[$job]; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
throw new Exception('No command known for job: ' . $job); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|