Skip to content

Commit f401529

Browse files
authored
Merge pull request #31 from burzum/annotate-extentions
Allow also inherited loadService() annotating.
2 parents ea6bdd3 + 1fc4f79 commit f401529

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

src/Annotator/ClassAnnotatorTask/ServiceAwareClassAnnotatorTask.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,25 @@ class ServiceAwareClassAnnotatorTask extends AbstractClassAnnotatorTask implemen
2020
*/
2121
public function shouldRun(string $path, string $content): bool
2222
{
23-
if (!preg_match('#\buse ServiceAwareTrait\b#', $content)) {
23+
if (preg_match('#\buse ServiceAwareTrait\b#', $content)) {
24+
return true;
25+
}
26+
27+
$className = $this->getClassName($path, $content);
28+
if (!$className) {
2429
return false;
2530
}
2631

27-
return true;
32+
try {
33+
$object = new $className();
34+
if (method_exists($object, 'loadService')) {
35+
return true;
36+
}
37+
} catch (\Throwable $exception) {
38+
// Do nothing
39+
}
40+
41+
return false;
2842
}
2943

3044
/**
@@ -81,4 +95,22 @@ protected function _getServiceAnnotations(array $usedServices): array
8195

8296
return $annotations;
8397
}
98+
99+
/**
100+
* @param string $path Path to PHP class file
101+
* @param string $content Content of PHP class file
102+
*
103+
* @return string|null
104+
*/
105+
protected function getClassName(string $path, string $content): ?string
106+
{
107+
preg_match('#^namespace (.+)\b#m', $content, $matches);
108+
if (!$matches) {
109+
return null;
110+
}
111+
112+
$className = pathinfo($path, PATHINFO_FILENAME);
113+
114+
return $matches[1] . '\\' . $className;
115+
}
84116
}

0 commit comments

Comments
 (0)