1
|
|
|
<?php |
2
|
|
|
namespace Queue\Queue; |
3
|
|
|
|
4
|
|
|
use Cake\Core\App; |
5
|
|
|
use Cake\Core\Plugin; |
6
|
|
|
use Cake\Filesystem\Folder; |
7
|
|
|
|
8
|
|
|
class TaskFinder |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* |
13
|
|
|
* @var array|null |
14
|
|
|
*/ |
15
|
|
|
protected $tasks; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Returns all possible Queue tasks. |
19
|
|
|
* |
20
|
|
|
* Makes sure that app tasks are prioritized over plugin ones. |
21
|
|
|
* |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
public function allAppAndPluginTasks() |
25
|
|
|
{ |
26
|
|
|
if ($this->tasks !== null) { |
27
|
|
|
return $this->tasks; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$paths = App::path('Shell/Task'); |
31
|
|
|
$this->tasks = []; |
32
|
|
|
|
33
|
|
|
foreach ($paths as $path) { |
34
|
|
|
$Folder = new Folder($path); |
35
|
|
|
$this->tasks = $this->getAppPaths($Folder); |
36
|
|
|
} |
37
|
|
|
$plugins = Plugin::loaded(); |
38
|
|
|
foreach ($plugins as $plugin) { |
39
|
|
|
$pluginPaths = App::path('Shell/Task', $plugin); |
40
|
|
|
foreach ($pluginPaths as $pluginPath) { |
41
|
|
|
$Folder = new Folder($pluginPath); |
42
|
|
|
$pluginTasks = $this->getPluginPaths($Folder, $plugin); |
43
|
|
|
$this->tasks = array_merge($this->tasks, $pluginTasks); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $this->tasks; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* |
52
|
|
|
* @param \Cake\Filesystem\Folder $Folder The directory |
53
|
|
|
* |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
protected function getAppPaths(Folder $Folder) |
57
|
|
|
{ |
58
|
|
|
$res = array_merge($this->tasks, $Folder->find('Queue.+\.php')); |
|
|
|
|
59
|
|
|
foreach ($res as &$r) { |
60
|
|
|
$r = basename($r, 'Task.php'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $res; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* |
68
|
|
|
* @param \Cake\Filesystem\Folder $Folder The directory |
69
|
|
|
* @param string $plugin The plugin name |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
protected function getPluginPaths(Folder $Folder, $plugin) |
74
|
|
|
{ |
75
|
|
|
$res = $Folder->find('Queue.+Task\.php'); |
76
|
|
|
foreach ($res as $key => $r) { |
77
|
|
|
$name = basename($r, 'Task.php'); |
78
|
|
|
if (in_array($name, $this->tasks)) { |
|
|
|
|
79
|
|
|
unset($res[$key]); |
80
|
|
|
continue; |
81
|
|
|
} |
82
|
|
|
$res[$key] = $plugin . '.' . $name; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $res; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|