|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Jarobe\TaskRunner\Hydrator; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
7
|
|
|
use Jarobe\TaskRunner\TaskType\TaskTypeInterface; |
|
8
|
|
|
use Jarobe\TaskRUnner\Annotation\TaskType; |
|
9
|
|
|
use Symfony\Component\Finder\Finder; |
|
10
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class Discovery implements DiscoveryInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private $namespace; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $directory; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Reflector |
|
27
|
|
|
*/ |
|
28
|
|
|
private $reflector; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The Kernel root directory |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
private $rootDir; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
private $tasks = []; |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* WorkerDiscovery constructor. |
|
44
|
|
|
* |
|
45
|
|
|
* @param $namespace |
|
46
|
|
|
* The namespace of the workers |
|
47
|
|
|
* @param $directory |
|
48
|
|
|
* The directory of the workers |
|
49
|
|
|
* @param $rootDir |
|
50
|
|
|
* @param Reflector $reflector |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct($namespace, $directory, $rootDir, Reflector $reflector) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->namespace = $namespace; |
|
55
|
|
|
$this->reflector = $reflector; |
|
56
|
|
|
$this->directory = $directory; |
|
57
|
|
|
$this->rootDir = $rootDir; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Returns all the tasks |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getTasks() |
|
65
|
|
|
{ |
|
66
|
|
|
if (!$this->tasks) { |
|
|
|
|
|
|
67
|
|
|
$this->discoverTaskTypes(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $this->tasks; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Discovers tasks |
|
75
|
|
|
*/ |
|
76
|
|
|
private function discoverTaskTypes() |
|
77
|
|
|
{ |
|
78
|
|
|
$path = $this->rootDir . '/../src/' . $this->directory; |
|
79
|
|
|
$finder = new Finder(); |
|
80
|
|
|
$finder->files()->in($path); |
|
81
|
|
|
|
|
82
|
|
|
/** @var SplFileInfo $file */ |
|
83
|
|
|
foreach ($finder as $file) { |
|
84
|
|
|
/** @var TaskTypeInterface $class */ |
|
85
|
|
|
$class = $this->namespace . '\\' . $file->getBasename('.php'); |
|
86
|
|
|
|
|
87
|
|
|
$name = $this->reflector->getNameForClass($class); |
|
|
|
|
|
|
88
|
|
|
if (!$name) { |
|
89
|
|
|
continue; |
|
90
|
|
|
} |
|
91
|
|
|
/** @var TaskType $annotation */ |
|
92
|
|
|
$this->tasks[$name] = $class; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.