Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@
],
"require": {
"php": ">=8.1",
"doctrine/annotations": "^1.14",
"doctrine/annotations": "^2.0",
"doctrine/doctrine-bundle": "^2.8",
"doctrine/orm": "^2.14",
"doctrine/orm": "^2.2",
"elasticsearch/elasticsearch": "^7.1",
"symfony/dotenv": "6.4.*",
"symfony/expression-language": "6.4.*",
"symfony/framework-bundle": "^6.4",
"symfony/messenger": "6.4.*",
"symfony/property-info": "6.4.*",
"symfony/runtime": "6.4.*",
"symfony/serializer": "6.4.*",
"symfony/validator": "6.4.*",
"symfony/yaml": "6.4.*",
"symfony/dotenv": "7.2.*",
"symfony/expression-language": "7.2.*",
"symfony/framework-bundle": "^7.2",
"symfony/messenger": "7.2.*",
"symfony/property-info": "7.2.*",
"symfony/runtime": "7.2.*",
"symfony/serializer": "7.2.*",
"symfony/validator": "7.2.*",
"symfony/yaml": "7.2.*",
"ext-simplexml": "*",
"symfony/security-bundle": "^6.4",
"symfony/finder": "^6.4"
"symfony/security-bundle": "^7.2",
"symfony/finder": "^7.2"
},
"require-dev": {
"symfony/browser-kit": "6.4.*",
"symfony/css-selector": "6.4.*",
"symfony/console": "6.4.*",
"symfony/phpunit-bridge": "^6.4",
"symfony/test-pack": "^1.1"
"symfony/browser-kit": "7.2.*",
"symfony/css-selector": "7.2.*",
"symfony/console": "7.2.*",
"symfony/phpunit-bridge": "^7.2",
"symfony/test-pack": "1.1.0"
},
"prefer-stable": true,
"autoload": {
Expand All @@ -48,7 +48,7 @@
"dev-master": "1.0-dev"
},
"symfony": {
"require": "6.4.*"
"require": "7.2.*"
}
},
"conflict": {
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<server name="SHELL_VERBOSITY" value="-1"/>
<server name="KERNEL_DIR" value="tests/Fixtures/app/"/>
<server name="KERNEL_CLASS" value="AppKernel"/>
<server name="SYMFONY_PHPUNIT_VERSION" value="9.6"/>
</php>
<testsuites>
<testsuite name="Project Test Suite">
Expand Down
6 changes: 4 additions & 2 deletions src/Identifier/LogIdentifierExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Locastic\Loggastic\Identifier;

use Doctrine\Common\Util\ClassUtils;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\Mapping\MappingException;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
Expand All @@ -17,7 +16,10 @@ public function __construct(private readonly EntityManagerInterface $entityManag
public function getIdentifierValue(object $object): int|string|null
{
try {
$identifier = $this->entityManager->getClassMetadata(ClassUtils::getClass($object))->getSingleIdentifierFieldName();
$metadata = $this->entityManager->getClassMetadata(get_class($object));
$entityClass = $metadata->getName();

$identifier = $this->entityManager->getClassMetadata($entityClass)->getSingleIdentifierFieldName();
$identifierGetter = 'get' . $identifier;

return $object->$identifierGetter();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Locastic\Loggastic\Metadata\LoggableContext\Factory;

use Doctrine\Common\Annotations\Reader;
use Locastic\Loggastic\Annotation\Loggable;
use Locastic\Loggastic\Metadata\LoggableContext\LoggableContextCollection;
use Locastic\Loggastic\Util\RecursiveClassIterator;

final class AttributeLoggableContextCollectionFactory implements LoggableContextCollectionFactoryInterface
{
/**
* @param string[] $loggablePaths
*/
public function __construct(private readonly LoggableContextCollectionFactoryInterface $decorated, private readonly array $loggablePaths)
{
}

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

$classes = [];

if ($this->decorated) {
foreach ($this->decorated->create() as $loggableClass => $config) {
$classes[$loggableClass] = $config;
}
}

foreach (RecursiveClassIterator::getReflectionClasses($this->loggablePaths) as $className => $reflectionClass) {
if ($loggable = $this->getLoggableAttribute($reflectionClass)) {
$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;
}
}
5 changes: 2 additions & 3 deletions src/Resources/config/context.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ services:
- '@locastic_activity_logs.metadata.loggable_class_extractor.yaml'
- '@.inner'

locastic_activity_log.metadata.loggable.context_collection_factory.annotation:
class: Locastic\ActivityLog\Metadata\LoggableContext\Factory\AnnotationLoggableContextCollectionFactory
locastic_activity_log.metadata.loggable.context_collection_factory.attribute:
class: Locastic\ActivityLog\Metadata\LoggableContext\Factory\AttributeLoggableContextCollectionFactory
decorates: 'locastic_activity_log.metadata.loggable.context_collection_factory'
arguments:
- '@.inner'
- '@annotations.reader'
- '%locastic_activity_log.dir.loggable_classes%'

Locastic\ActivityLog\Metadata\LoggableContext\Factory\LoggableContextFactoryInterface:
Expand Down
5 changes: 2 additions & 3 deletions src/Resources/config/loggable_context.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ services:
- '@Locastic\Loggastic\Metadata\Extractor\YamlLoggableExtractor'
- '@.inner'

locastic_activity_log.metadata.loggable.context_collection_factory.annotation:
class: Locastic\Loggastic\Metadata\LoggableContext\Factory\AnnotationLoggableContextCollectionFactory
locastic_activity_log.metadata.loggable.context_collection_factory.attribute:
class: Locastic\Loggastic\Metadata\LoggableContext\Factory\AttributeLoggableContextCollectionFactory
decorates: 'locastic_activity_log.metadata.loggable.context_collection_factory'
arguments:
- '@.inner'
- '@annotations.reader'
- '%locastic_activity_log.dir.loggable_classes%'

Locastic\Loggastic\Metadata\LoggableContext\Factory\LoggableContextFactoryInterface:
Expand Down
28 changes: 21 additions & 7 deletions src/Serializer/ActivityLogCollectionNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,47 @@ public function __construct(
{
}

public function normalize($object, string $format = null, array $context = []): array
public function normalize($data, string $format = null, array $context = []): array
{
$data = [];
$collection = [];

if ($this->useIdentifierExtractor) {
foreach ($object as $item) {
foreach ($data as $item) {
$normalizedItem = $this->decorated->normalize($item, 'array', $context);

$logId = $this->logIdentifierExtractor->getIdentifierValue($item);
if ($logId === null) {
continue;
}

$data[$logId] = $normalizedItem;
$collection[$logId] = $normalizedItem;
}
} else {
foreach ($object as $item) {
foreach ($data as $item) {
$normalizedItem = $this->decorated->normalize($item, 'array', $context);
$data[] = $normalizedItem;
$collection[] = $normalizedItem;
}
}

return $data;
return $collection;
}

public function supportsNormalization($data, string $format = null, array $context = []): bool
{
return $data instanceof Collection && self::FORMAT === $format;
}

public function getSupportedTypes(?string $format): array
{
if (self::FORMAT !== $format) {
return [];
}

return [
Collection::class => true,
'array' => true,
'object' => true,
'Locastic\Loggastic\Model\Output\ActivityLogInterface' => true,
];
}
}
16 changes: 7 additions & 9 deletions tests/Fixtures/app/Model/DummyBlogPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,27 @@
use Locastic\Loggastic\Annotation\Loggable;
use Symfony\Component\Serializer\Annotation\Groups;

/**
* @Loggable(groups={"dummy_blog_post_log"})
*/
#[Loggable(groups: ['dummy_blog_post_log'])]
class DummyBlogPost
{
private int $id;

/** @Groups({"dummy_blog_post_log"}) */
#[Groups(['dummy_blog_post_log'])]
private ?string $title;

/** @Groups({"dummy_blog_post_log"}) */
#[Groups(['dummy_blog_post_log'])]
private array $tags = [];

/** @Groups({"dummy_blog_post_log"}) */
#[Groups(['dummy_blog_post_log'])]
private int $position = 0;

/** @Groups({"dummy_blog_post_log"}) */
#[Groups(['dummy_blog_post_log'])]
private ?\DateTime $publishAt;

/** @Groups({"dummy_blog_post_log"}) */
#[Groups(['dummy_blog_post_log'])]
private bool $enabled;

/** @Groups({"dummy_blog_post_log"}) */
#[Groups(['dummy_blog_post_log'])]
private Collection $photos;

public function __construct()
Expand Down