1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Gearman Bundle for Symfony2 |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* Feel free to edit as you please, and have fun. |
10
|
|
|
* |
11
|
|
|
* @author Marc Morera <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Mmoreram\GearmanBundle\Service; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Annotations\Reader; |
17
|
|
|
use ReflectionClass; |
18
|
|
|
use Symfony\Component\Finder\Finder; |
19
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
20
|
|
|
|
21
|
|
|
use Mmoreram\GearmanBundle\Driver\Gearman\Work as WorkAnnotation; |
22
|
|
|
use Mmoreram\GearmanBundle\Module\WorkerClass as Worker; |
23
|
|
|
use Mmoreram\GearmanBundle\Module\WorkerCollection; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Gearman parsing methods |
27
|
|
|
* |
28
|
|
|
* This class has responability of parsing, if needed, all defined bundle files |
29
|
|
|
* looking for some Workers. |
30
|
|
|
* |
31
|
|
|
* @since 2.3.1 |
32
|
|
|
*/ |
33
|
|
|
class GearmanParser |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
* |
39
|
|
|
* Bundles loaded by kernel |
40
|
|
|
*/ |
41
|
|
|
private $kernelBundles; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var KernelInterface |
45
|
|
|
* |
46
|
|
|
* Kernel object |
47
|
|
|
*/ |
48
|
|
|
private $kernel; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var Reader |
52
|
|
|
* |
53
|
|
|
* Annotation Reader |
54
|
|
|
*/ |
55
|
|
|
private $reader; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var Finder |
59
|
|
|
* |
60
|
|
|
* Finder |
61
|
|
|
*/ |
62
|
|
|
private $finder; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var array |
66
|
|
|
* |
67
|
|
|
* Bundles available to perform search |
68
|
|
|
*/ |
69
|
|
|
private $bundles; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var array |
73
|
|
|
* |
74
|
|
|
* Namespaces paths to be searched |
75
|
|
|
*/ |
76
|
|
|
private $resources; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var array |
80
|
|
|
* |
81
|
|
|
* Collection of servers to connect |
82
|
|
|
*/ |
83
|
|
|
private $servers; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var array |
87
|
|
|
* |
88
|
|
|
* Default settings defined by user in config.yml |
89
|
|
|
*/ |
90
|
|
|
private $defaultSettings; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Root kernel directory |
94
|
|
|
* |
95
|
|
|
* @var string |
96
|
|
|
*/ |
97
|
|
|
private $rootDir; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Construct method |
101
|
|
|
* |
102
|
|
|
* @param KernelInterface $kernel Kernel instance |
103
|
|
|
* @param Reader $reader Reader |
104
|
|
|
* @param Finder $finder Finder |
105
|
|
|
* @param array $bundles Bundle array where to parse workers, defined in configuration |
106
|
|
|
* @param array $resources Array of namespace paths to be searched for worker annotations |
107
|
|
|
* @param array $servers Server list defined on configuration |
108
|
|
|
* @param array $defaultSettings Default settings defined on configuration |
109
|
|
|
*/ |
110
|
5 |
|
public function __construct( |
111
|
|
|
KernelInterface $kernel, |
112
|
|
|
Reader $reader, |
113
|
|
|
Finder $finder, |
114
|
|
|
array $bundles, |
115
|
|
|
array $resources, |
116
|
|
|
array $servers, |
117
|
|
|
array $defaultSettings |
118
|
|
|
) |
119
|
|
|
{ |
120
|
5 |
|
$this->kernelBundles = $kernel->getBundles(); |
121
|
5 |
|
$this->kernel = $kernel; |
122
|
5 |
|
$this->reader = $reader; |
123
|
5 |
|
$this->finder = $finder; |
124
|
5 |
|
$this->bundles = $bundles; |
125
|
5 |
|
$this->resources = $resources; |
126
|
5 |
|
$this->servers = $servers; |
127
|
5 |
|
$this->defaultSettings = $defaultSettings; |
128
|
5 |
|
$this->rootDir = $this->kernel->getRootDir(); |
129
|
5 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Loads Worker Collection from parsed files |
133
|
|
|
* |
134
|
|
|
* @return WorkerCollection collection of all info |
135
|
|
|
*/ |
136
|
1 |
|
public function load() |
137
|
|
|
{ |
138
|
1 |
|
list($paths, $excludedPaths) = $this->loadBundleNamespaceMap($this->kernelBundles, $this->bundles); |
139
|
1 |
|
$paths = array_merge($paths, $this->loadResourceNamespaceMap($this->rootDir, $this->resources)); |
140
|
|
|
|
141
|
1 |
|
return $this->parseNamespaceMap($this->finder, $this->reader, $paths, $excludedPaths); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Return Gearman bundle settings, previously loaded by method load() |
146
|
|
|
* |
147
|
|
|
* If settings are not loaded, a SettingsNotLoadedException Exception is thrown |
148
|
|
|
* |
149
|
|
|
* @param array $kernelBundles Kernel bundles |
150
|
|
|
* @param array $bundles Bundle array of settings |
151
|
|
|
* |
152
|
|
|
* @return array Return an array containing paths and ignore paths |
153
|
|
|
*/ |
154
|
6 |
|
public function loadBundleNamespaceMap(array $kernelBundles, array $bundles) |
155
|
|
|
{ |
156
|
6 |
|
$paths = array(); |
157
|
6 |
|
$excludedPaths = array(); |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Iteratinc all bundle settings |
161
|
|
|
*/ |
162
|
6 |
|
foreach ($bundles as $bundleSettings) { |
163
|
|
|
|
164
|
5 |
|
if (!$bundleSettings['active']) { |
165
|
|
|
|
166
|
1 |
|
break; |
167
|
|
|
} |
168
|
|
|
|
169
|
4 |
|
$bundleNamespace = $bundleSettings['name']; |
170
|
4 |
|
$bundlePath = $kernelBundles[$bundleNamespace]->getPath(); |
171
|
|
|
|
172
|
4 |
|
if (!empty($bundleSettings['include'])) { |
173
|
|
|
|
174
|
2 |
|
foreach ($bundleSettings['include'] as $include) { |
175
|
|
|
|
176
|
2 |
|
$paths[] = rtrim(rtrim($bundlePath, '/') . '/' . $include, '/') . '/'; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
} else { |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* If no include is set, include all namespace |
183
|
|
|
*/ |
184
|
2 |
|
$paths[] = rtrim($bundlePath, '/') . '/'; |
185
|
|
|
} |
186
|
|
|
|
187
|
4 |
|
foreach ($bundleSettings['ignore'] as $ignore) { |
188
|
|
|
|
189
|
4 |
|
$excludedPaths[] = trim($ignore, '/'); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return array( |
194
|
6 |
|
$paths, |
195
|
6 |
|
$excludedPaths, |
196
|
|
|
); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get resource paths |
201
|
|
|
* @param string $rootDir |
202
|
|
|
* @param array $resources |
203
|
|
|
* @return array |
204
|
|
|
*/ |
205
|
|
|
public function loadResourceNamespaceMap($rootDir, array $resources) |
206
|
|
|
{ |
207
|
2 |
|
return array_map(function($resource) use ($rootDir) { |
208
|
1 |
|
return $rootDir . '/' . trim($resource, '/') . '/'; |
209
|
2 |
|
}, $resources); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Perform a parsing inside all namespace map |
214
|
|
|
* |
215
|
|
|
* Creates an empty worker collection and, if exist some parseable files |
216
|
|
|
* parse them, filling this object |
217
|
|
|
* |
218
|
|
|
* @param Finder $finder Finder |
219
|
|
|
* @param Reader $reader Reader |
220
|
|
|
* @param array $paths Paths where to look for |
221
|
|
|
* @param array $excludedPaths Paths to ignore |
222
|
|
|
* |
223
|
|
|
* @return WorkerCollection collection of all info |
224
|
|
|
*/ |
225
|
3 |
|
public function parseNamespaceMap( |
226
|
|
|
Finder $finder, |
227
|
|
|
Reader $reader, |
228
|
|
|
array $paths, |
229
|
|
|
array $excludedPaths |
230
|
|
|
) |
231
|
|
|
{ |
232
|
3 |
|
$workerCollection = new WorkerCollection; |
233
|
|
|
|
234
|
3 |
|
if (!empty($paths)) { |
235
|
|
|
|
236
|
|
|
$finder |
237
|
1 |
|
->files() |
238
|
1 |
|
->followLinks() |
239
|
1 |
|
->exclude($excludedPaths) |
240
|
1 |
|
->in($paths) |
241
|
1 |
|
->name('*.php'); |
242
|
|
|
|
243
|
1 |
|
$this->parseFiles($finder, $reader, $workerCollection); |
244
|
|
|
} |
245
|
|
|
|
246
|
3 |
|
return $workerCollection; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Load all workers with their jobs |
251
|
|
|
* |
252
|
|
|
* @param Finder $finder Finder |
253
|
|
|
* @param Reader $reader Reader |
254
|
|
|
* @param WorkerCollection $workerCollection Worker collection |
255
|
|
|
* |
256
|
|
|
* @return GearmanParser self Object |
257
|
|
|
*/ |
258
|
|
|
public function parseFiles( |
259
|
|
|
Finder $finder, |
260
|
|
|
Reader $reader, |
261
|
|
|
WorkerCollection $workerCollection |
262
|
|
|
) |
263
|
|
|
{ |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Every file found is parsed |
267
|
|
|
*/ |
268
|
|
|
foreach ($finder as $file) { |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* File is accepted to be parsed |
272
|
|
|
*/ |
273
|
|
|
$classNamespace = $this->getFileClassNamespace($file->getRealpath()); |
274
|
|
|
$reflectionClass = new ReflectionClass($classNamespace); |
275
|
|
|
$classAnnotations = $reader->getClassAnnotations($reflectionClass); |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Every annotation found is parsed |
279
|
|
|
*/ |
280
|
|
|
foreach ($classAnnotations as $annotation) { |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Annotation is only laoded if is typeof WorkAnnotation |
284
|
|
|
*/ |
285
|
|
|
if ($annotation instanceof WorkAnnotation) { |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Creates new Worker element with all its Job data |
289
|
|
|
*/ |
290
|
|
|
$worker = new Worker($annotation, $reflectionClass, $reader, $this->servers, $this->defaultSettings); |
291
|
|
|
$workerCollection->add($worker); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
return $this; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Returns file class namespace, if exists |
301
|
|
|
* |
302
|
|
|
* @param string $file A PHP file path |
303
|
|
|
* |
304
|
|
|
* @return string|false Full class namespace if found, false otherwise |
305
|
|
|
*/ |
306
|
2 |
|
public function getFileClassNamespace($file) |
307
|
|
|
{ |
308
|
2 |
|
$filenameBlock = explode(DIRECTORY_SEPARATOR, $file); |
309
|
2 |
|
$filename = explode('.', end($filenameBlock), 2); |
310
|
2 |
|
$filename = reset($filename); |
311
|
|
|
|
312
|
2 |
|
preg_match('/\snamespace\s+(.+?);/s', file_get_contents($file), $match); |
313
|
|
|
|
314
|
2 |
|
return is_array($match) && isset($match[1]) |
315
|
2 |
|
? $match[1] . '\\' . $filename |
316
|
2 |
|
: false; |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|