1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Burzum\CakeServiceLayer\Annotator\ClassAnnotatorTask; |
5
|
|
|
|
6
|
|
|
use Cake\Core\App; |
7
|
|
|
use IdeHelper\Annotation\AnnotationFactory; |
8
|
|
|
use IdeHelper\Annotator\ClassAnnotatorTask\AbstractClassAnnotatorTask; |
9
|
|
|
use IdeHelper\Annotator\ClassAnnotatorTask\ClassAnnotatorTaskInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Classes that use ServiceAwareTrait should automatically have used tables - via loadService() call - annotated. |
13
|
|
|
*/ |
14
|
|
|
class ServiceAwareClassAnnotatorTask extends AbstractClassAnnotatorTask implements ClassAnnotatorTaskInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param string $path Path |
18
|
|
|
* @param string $content Content |
19
|
|
|
* @return bool |
20
|
|
|
*/ |
21
|
|
|
public function shouldRun(string $path, string $content): bool |
22
|
|
|
{ |
23
|
|
|
if (!preg_match('#\buse ServiceAwareTrait\b#', $content)) { |
24
|
|
|
return false; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return true; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $path Path |
32
|
|
|
* @return bool |
33
|
|
|
*/ |
34
|
|
|
public function annotate(string $path): bool |
35
|
|
|
{ |
36
|
|
|
$services = $this->_getUsedServices($this->content); |
37
|
|
|
|
38
|
|
|
$annotations = $this->_getServiceAnnotations($services); |
39
|
|
|
|
40
|
|
|
return $this->annotateContent($path, $this->content, $annotations); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $content Content |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
protected function _getUsedServices(string $content): array |
49
|
|
|
{ |
50
|
|
|
preg_match_all('/\$this-\>loadService\(\'([a-z.\\/]+)\'/i', $content, $matches); |
51
|
|
|
if (empty($matches[1])) { |
52
|
|
|
return []; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$services = $matches[1]; |
56
|
|
|
|
57
|
|
|
return array_unique($services); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array $usedServices Used services |
62
|
|
|
* @return \IdeHelper\Annotation\AbstractAnnotation[] |
63
|
|
|
*/ |
64
|
|
|
protected function _getServiceAnnotations(array $usedServices): array |
65
|
|
|
{ |
66
|
|
|
$annotations = []; |
67
|
|
|
|
68
|
|
|
foreach ($usedServices as $usedService) { |
69
|
|
|
$className = App::className($usedService, 'Service', 'Service'); |
70
|
|
|
if (!$className) { |
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
list(, $name) = pluginSplit($usedService); |
74
|
|
|
|
75
|
|
|
if (strpos($name, '/') !== false) { |
76
|
|
|
$name = substr($name, strrpos($name, '/') + 1); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$annotations[] = AnnotationFactory::createOrFail('@property', '\\' . $className, '$' . $name); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $annotations; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|