Skip to content

Commit

Permalink
Merge pull request #31 from burzum/annotate-extentions
Browse files Browse the repository at this point in the history
Allow also inherited loadService() annotating.
  • Loading branch information
dereuromark authored Aug 31, 2021
2 parents ea6bdd3 + 1fc4f79 commit f401529
Showing 1 changed file with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,25 @@ class ServiceAwareClassAnnotatorTask extends AbstractClassAnnotatorTask implemen
*/
public function shouldRun(string $path, string $content): bool
{
if (!preg_match('#\buse ServiceAwareTrait\b#', $content)) {
if (preg_match('#\buse ServiceAwareTrait\b#', $content)) {
return true;
}

$className = $this->getClassName($path, $content);
if (!$className) {
return false;
}

return true;
try {
$object = new $className();
if (method_exists($object, 'loadService')) {
return true;
}
} catch (\Throwable $exception) {
// Do nothing
}

return false;
}

/**
Expand Down Expand Up @@ -81,4 +95,22 @@ protected function _getServiceAnnotations(array $usedServices): array

return $annotations;
}

/**
* @param string $path Path to PHP class file
* @param string $content Content of PHP class file
*
* @return string|null
*/
protected function getClassName(string $path, string $content): ?string
{
preg_match('#^namespace (.+)\b#m', $content, $matches);
if (!$matches) {
return null;
}

$className = pathinfo($path, PATHINFO_FILENAME);

return $matches[1] . '\\' . $className;
}
}

0 comments on commit f401529

Please sign in to comment.