Skip to content

Commit

Permalink
fixed annotation loggable context factory
Browse files Browse the repository at this point in the history
  • Loading branch information
paullla committed Aug 3, 2023
1 parent cd51a44 commit 4d25aa1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ Now that you have the basic setup, you can add some additional options and custo
Default configuration:
```yaml
# config/packages/loggastic.yaml
loggastic:
locastic_loggastic:
# directory paths containing loggable classes or xml/yaml files
loggable_paths:
- '%kernel.project_dir%/Resources/config/loggastic'
Expand Down
2 changes: 1 addition & 1 deletion src/Annotation/Loggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Loggable

public function __construct(array $groups)
{
$this->groups = $groups['groups'];
$this->groups = $groups;
}

public function getGroups(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ public function __construct(private readonly LoggableContextCollectionFactoryInt
{
}

/**
* {@inheritdoc}
*/
public function create(): LoggableContextCollection
{
if(count($this->loggablePaths) === 0) {
if (count($this->loggablePaths) === 0) {
return new LoggableContextCollection([]);
}

Expand All @@ -34,16 +31,26 @@ public function create(): LoggableContextCollection
}

foreach (RecursiveClassIterator::getReflectionClasses($this->loggablePaths) as $className => $reflectionClass) {
if (
(\PHP_VERSION_ID >= 80000 && $reflectionClass->getAttributes(Loggable::class)) ||
(null !== $this->reader && $loggable = $this->reader->getClassAnnotation($reflectionClass, Loggable::class))
) {
if (!empty($loggable)) {
$classes[$className] = ['groups' => $loggable->getGroups()];
}
if ($loggable = $this->getLoggableAttribute($reflectionClass)) {
$classes[$className] = ['groups' => $loggable->getGroups()];
}

if (null !== $this->reader && $loggable = $this->reader->getClassAnnotation($reflectionClass, Loggable::class)) {
$classes[$className] = ['groups' => $loggable->getGroups()];
}
}

return new LoggableContextCollection($classes);
}

private function getLoggableAttribute(\ReflectionClass $reflectionClass): ?Loggable
{
foreach ($reflectionClass->getAttributes() as $attribute) {
if (is_a($attribute->getName(), Loggable::class, true)) {
return $attribute->newInstance();
}
}

return null;
}
}

0 comments on commit 4d25aa1

Please sign in to comment.