-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathTaskFinder.php
More file actions
183 lines (156 loc) · 4.42 KB
/
Copy pathTaskFinder.php
File metadata and controls
183 lines (156 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
declare(strict_types=1);
namespace Queue\Queue;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RecursiveRegexIterator;
use RegexIterator;
use RuntimeException;
class TaskFinder {
/**
* @phpstan-var array<string, class-string<\Queue\Queue\Task>>|null
* @var array<string>|null
*/
protected ?array $tasks = null;
/**
* @phpstan-return array<string, class-string<\Queue\Queue\Task>>
*
* @param string $type Type of interface.
*
* @return array<string>
*/
public function allAddable(string $type = AddInterface::class): array {
$all = $this->all();
foreach ($all as $task => $class) {
if (!is_subclass_of($class, $type, true)) {
unset($all[$task]);
}
}
return $all;
}
/**
* Returns all possible Queue tasks.
*
* Makes sure that app tasks are prioritized over plugin ones.
*
* @phpstan-return array<string, class-string<\Queue\Queue\Task>>
*
* @return array<string>
*/
public function all(): array {
if ($this->tasks !== null) {
return $this->tasks;
}
$paths = App::classPath('Queue/Task');
$this->tasks = [];
foreach ($paths as $path) {
$this->tasks += $this->getTasks($path);
}
$plugins = array_merge((array)Configure::read('Queue.plugins'), Plugin::loaded());
$plugins = array_unique($plugins);
foreach ($plugins as $plugin) {
$pluginPaths = App::classPath('Queue/Task', $plugin);
foreach ($pluginPaths as $pluginPath) {
$pluginTasks = $this->getTasks($pluginPath, $plugin);
$this->tasks += $pluginTasks;
}
}
ksort($this->tasks);
return $this->tasks;
}
/**
* @phpstan-return array<string, class-string<\Queue\Queue\Task>>
*
* @param string $path
* @param string|null $plugin
*
* @return array<string>
*/
protected function getTasks(string $path, ?string $plugin = null): array {
if (!is_dir($path)) {
return [];
}
$tasks = [];
$ignoredTasks = Config::ignoredTasks();
$directoryIterator = new RecursiveDirectoryIterator($path);
$recursiveIterator = new RecursiveIteratorIterator($directoryIterator);
$iterator = new RegexIterator($recursiveIterator, '#.+\b(\w+)Task\.php$#', RecursiveRegexIterator::GET_MATCH);
/** @var array<string> $file */
foreach ($iterator as $file) {
$path = str_replace(DS, '/', $file[0]);
$pos = strpos($path, 'src/Queue/Task/');
if ($pos) {
$name = substr($path, $pos + strlen('src/Queue/Task/'), -8);
} else {
$pos = strpos($path, APP_DIR . '/Queue/Task/');
if (!$pos) {
continue;
}
$name = substr($path, $pos + strlen(APP_DIR . '/Queue/Task/'), -8);
}
$namespace = $plugin ? str_replace('/', '\\', $plugin) : Configure::read('App.namespace');
/** @phpstan-var class-string<\Queue\Queue\Task> $className */
$className = $namespace . '\Queue\Task\\' . str_replace('/', '\\', $name) . 'Task';
$key = $plugin ? $plugin . '.' . $name : $name;
if (!in_array($className, $ignoredTasks, true)) {
$tasks[$key] = $className;
}
}
return $tasks;
}
/**
* Resolves FQCN to a task name.
*
* @param class-string<\Queue\Queue\Task>|string $jobTask
*
* @return string
*/
public function resolve(string $jobTask): string {
if (Configure::read('Queue.skipExistenceCheck')) {
if (!str_contains($jobTask, '\\')) {
return $jobTask;
}
return Config::taskName($jobTask);
}
$all = $this->all();
foreach ($all as $name => $className) {
if ($jobTask === $className || $jobTask === $name) {
return $name;
}
}
if (!str_contains($jobTask, '\\')) {
// Let's try matching without plugin prefix
foreach ($all as $name => $_) {
if (!str_contains($name, '.')) {
continue;
}
[$plugin, $name] = explode('.', $name, 2);
if ($jobTask === $name) {
$message = 'You seem to be adding a plugin job without plugin syntax (' . $jobTask . '), migrate to using ' . $plugin . '.' . $name . ' instead.';
trigger_error($message, E_USER_DEPRECATED);
return $plugin . '.' . $name;
}
}
}
throw new RuntimeException('No job type can be resolved for ' . $jobTask);
}
/**
* @phpstan-return class-string<\Queue\Queue\Task>
*
* @param string $name
*
* @return string
*/
public function getClass(string $name): string {
$all = $this->all();
foreach ($all as $taskName => $className) {
if ($name === $taskName) {
return $className;
}
}
throw new RuntimeException('No such task: ' . $name);
}
}