Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-8190: Add Symfony's Serializer support #121

Open
wants to merge 96 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 82 commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
cb4e61d
Add Symfony's Serializer support
barw4 Aug 12, 2024
5bf3e43
Add `AdapterNormalizer`
barw4 Aug 22, 2024
b478bae
Remove `ValueObjectVisitorDispatcher`
barw4 Aug 23, 2024
8e1ddf2
Rollback
barw4 Aug 23, 2024
1faaf7d
Rollback
barw4 Aug 23, 2024
d59508e
Rollback
barw4 Aug 23, 2024
2e731e3
Fixup
barw4 Aug 23, 2024
b56999e
Applied review remarks
barw4 Aug 27, 2024
a6cb018
Code cleanup
barw4 Aug 27, 2024
6bc9690
Code cleanup
barw4 Aug 27, 2024
97dc64b
Code cleanup
barw4 Aug 27, 2024
9659e6a
Fixup
barw4 Aug 27, 2024
8697942
Change login code to 200
barw4 Aug 27, 2024
d182d85
Set proper encoding format
barw4 Aug 28, 2024
ae726ac
Handle XML
barw4 Sep 4, 2024
be01db6
Rollback
barw4 Sep 4, 2024
a373e56
Rollback
barw4 Sep 4, 2024
f3b23cc
Use dispatched visitor instead of creating a new one
barw4 Sep 4, 2024
a515652
Reuse already initialized visitor
barw4 Sep 5, 2024
20b0b1e
Fixup
barw4 Sep 5, 2024
ed2f886
Fix PHPStan issues
barw4 Sep 5, 2024
3d01d41
Fixups
barw4 Sep 5, 2024
847a54d
Fixups
barw4 Sep 6, 2024
5cf488f
Make unit tests use the new XML generator
barw4 Oct 1, 2024
89572a8
Rollback
barw4 Oct 1, 2024
7a6e45c
Stan
barw4 Oct 1, 2024
3b6da3a
Made `transformData` method private
Steveb-p Oct 2, 2024
176ea72
Introduced normalizers for Generator state
Steveb-p Oct 8, 2024
7c5c50a
Introduced normalizers for Generator state
Steveb-p Oct 8, 2024
81e18ea
Introduced normalizers for Generator state
Steveb-p Oct 8, 2024
6e5d5b4
Introduced normalizers for Generator state
Steveb-p Oct 8, 2024
0babd33
Introduced normalizers for Generator state
Steveb-p Oct 8, 2024
26fd4bb
Introduced normalizers for Generator state
Steveb-p Oct 8, 2024
264eb4f
Introduced normalizers for Generator state
Steveb-p Oct 8, 2024
a78bb4b
Introduced normalizers for Generator state
Steveb-p Oct 8, 2024
4a63396
Add Symfony's Serializer support
barw4 Aug 12, 2024
179d53e
Add `AdapterNormalizer`
barw4 Aug 22, 2024
5bd9f48
Remove `ValueObjectVisitorDispatcher`
barw4 Aug 23, 2024
0f4433d
Rollback
barw4 Aug 23, 2024
8c35ac3
Rollback
barw4 Aug 23, 2024
a8ab8f6
Rollback
barw4 Aug 23, 2024
07d16a9
Fixup
barw4 Aug 23, 2024
5ba013a
Applied review remarks
barw4 Aug 27, 2024
9b751b8
Code cleanup
barw4 Aug 27, 2024
2bf13b9
Code cleanup
barw4 Aug 27, 2024
23aea91
Code cleanup
barw4 Aug 27, 2024
4d82929
Fixup
barw4 Aug 27, 2024
3e2ed35
Change login code to 200
barw4 Aug 27, 2024
cca8fa2
Set proper encoding format
barw4 Aug 28, 2024
d4741e1
Handle XML
barw4 Sep 4, 2024
b918595
Rollback
barw4 Sep 4, 2024
5b34e35
Rollback
barw4 Sep 4, 2024
f7edd5a
Use dispatched visitor instead of creating a new one
barw4 Sep 4, 2024
5a60415
Reuse already initialized visitor
barw4 Sep 5, 2024
0404793
Fixup
barw4 Sep 5, 2024
effe8d8
Fix PHPStan issues
barw4 Sep 5, 2024
12d2a5c
Fixups
barw4 Sep 5, 2024
4f23fca
Fixups
barw4 Sep 6, 2024
737877a
Make unit tests use the new XML generator
barw4 Oct 1, 2024
8b57228
Rollback
barw4 Oct 1, 2024
b12d06d
Made `transformData` method private
Steveb-p Oct 2, 2024
ea56426
Introduced normalizers for Generator state
Steveb-p Oct 8, 2024
be241a3
Introduced normalizers for Generator state
Steveb-p Oct 8, 2024
a934afd
Introduced normalizers for Generator state
Steveb-p Oct 8, 2024
6b77b59
Introduced normalizers for Generator state
Steveb-p Oct 8, 2024
91976bf
Introduced normalizers for Generator state
Steveb-p Oct 8, 2024
64aeee9
Introduced normalizers for Generator state
Steveb-p Oct 8, 2024
b5e74ab
Introduced normalizers for Generator state
Steveb-p Oct 8, 2024
bb2b396
Introduced normalizers for Generator state
Steveb-p Oct 8, 2024
1243dd6
Update Generator normalizers
barw4 Oct 10, 2024
f67ee78
Generate baseline
barw4 Oct 10, 2024
730868f
Refactor encoder context
barw4 Oct 10, 2024
b08f735
Handle empty `ArrayList` object
barw4 Oct 10, 2024
d781e88
`outer_element` context item
barw4 Oct 10, 2024
d7db4db
Remove dependencies to old Xml generator
barw4 Oct 15, 2024
b59b5bc
Merge remote-tracking branch 'origin/serializer-support' into seriali…
barw4 Oct 15, 2024
e16ec64
Remove old Xml generator
barw4 Oct 15, 2024
e692018
Merge branch 'main' into serializer-support
barw4 Oct 15, 2024
46032c9
CS
barw4 Oct 15, 2024
31fcff8
Merge remote-tracking branch 'origin/serializer-support' into seriali…
barw4 Oct 15, 2024
6330b60
Update services.yml
barw4 Oct 16, 2024
9d014ce
Rollback
barw4 Oct 16, 2024
3318577
Implement integration tests
barw4 Oct 18, 2024
8f232cf
Fixup
barw4 Oct 18, 2024
b567473
CS
barw4 Oct 18, 2024
bee695f
Fixup
barw4 Oct 18, 2024
cc840b3
Fix tests
barw4 Oct 21, 2024
66deeb9
Fixup
barw4 Oct 21, 2024
d8e19c1
Cleanup generators
barw4 Oct 23, 2024
32801e9
PHPStan
barw4 Oct 23, 2024
d608d6a
CS
barw4 Oct 23, 2024
c13ec95
Remove comment
barw4 Oct 23, 2024
32ca57e
Clean up field generators
barw4 Oct 24, 2024
8db052a
PHPStan
barw4 Oct 24, 2024
c0a09bf
Applied review remarks
barw4 Nov 4, 2024
44bcf9e
Applied review remark
barw4 Nov 4, 2024
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
483 changes: 9 additions & 474 deletions phpstan-baseline.neon

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,31 @@

namespace Ibexa\Bundle\Rest\DependencyInjection\Compiler;

use Ibexa\Contracts\Rest\Output\ValueObjectVisitorDispatcher;
use Ibexa\Contracts\Rest\Output\ValueObjectVisitorResolver;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* Compiler pass for the ibexa.rest.output.value_object.visitor tag.
* Maps an fully qualified class to a value object visitor.
* Maps a fully qualified class to a value object visitor.
*/
class ValueObjectVisitorPass implements CompilerPassInterface
final readonly class ValueObjectVisitorResolverPass implements CompilerPassInterface
{
public const OUTPUT_VALUE_OBJECT_VISITOR_SERVICE_TAG = 'ibexa.rest.output.value_object.visitor';
public const string OUTPUT_VALUE_OBJECT_VISITOR_SERVICE_TAG = 'ibexa.rest.output.value_object.visitor';

public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
if (!$container->hasDefinition(ValueObjectVisitorDispatcher::class)) {
if (!$container->hasDefinition(ValueObjectVisitorResolver::class)) {
return;
}

$definition = $container->getDefinition(ValueObjectVisitorDispatcher::class);
$definition = $container->getDefinition(ValueObjectVisitorResolver::class);

$taggedServiceIds = $container->findTaggedServiceIds(
self::OUTPUT_VALUE_OBJECT_VISITOR_SERVICE_TAG
);

foreach ($taggedServiceIds as $id => $attributes) {
foreach ($attributes as $attribute) {
if (!isset($attribute['type'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/bundle/IbexaRestBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function build(ContainerBuilder $container): void
$container->addCompilerPass(new Compiler\InputHandlerPass());
$container->addCompilerPass(new Compiler\InputParserPass());
$container->addCompilerPass(new Compiler\OutputVisitorPass());
$container->addCompilerPass(new Compiler\ValueObjectVisitorPass());
$container->addCompilerPass(new Compiler\ValueObjectVisitorResolverPass());

if ($container->hasExtension('lexik_jwt_authentication')) {
$container->addCompilerPass(new Compiler\LexikAuthorizationHeaderBridgePass());
Expand Down
56 changes: 45 additions & 11 deletions src/bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -315,27 +315,31 @@ services:
ibexa.rest.output.visitor.json:
class: Ibexa\Contracts\Rest\Output\Visitor
arguments:
- '@Ibexa\Rest\Output\Generator\Json'
- '@Ibexa\Contracts\Rest\Output\ValueObjectVisitorDispatcher'
$generator: '@Ibexa\Rest\Output\Generator\Json'
$normalizer: '@ibexa.rest.serializer'
$encoder: '@ibexa.rest.serializer.encoder.json'
$valueObjectVisitorResolver: '@Ibexa\Contracts\Rest\Output\ValueObjectVisitorResolver'
$format: 'json'
tags:
- { name: ibexa.rest.output.visitor, regexps: ibexa.rest.output.visitor.json.regexps }

ibexa.rest.output.visitor.xml:
class: Ibexa\Contracts\Rest\Output\Visitor
arguments:
- '@Ibexa\Rest\Output\Generator\Xml'
- '@Ibexa\Contracts\Rest\Output\ValueObjectVisitorDispatcher'
$generator: '@Ibexa\Rest\Output\Generator\InMemory\Xml'
$normalizer: '@ibexa.rest.serializer'
$encoder: '@ibexa.rest.serializer.encoder.xml'
$valueObjectVisitorResolver: '@Ibexa\Contracts\Rest\Output\ValueObjectVisitorResolver'
$format: 'xml'
tags:
- { name: ibexa.rest.output.visitor, regexps: ibexa.rest.output.visitor.xml.regexps }

# format output generators
Ibexa\Rest\Output\Generator\Xml:
Ibexa\Rest\Output\Generator\InMemory\Xml:
arguments:
- '@Ibexa\Rest\Output\Generator\Xml\FieldTypeHashGenerator'
calls:
- [ setFormatOutput, [ "%kernel.debug%" ] ]
$fieldTypeHashGenerator: '@Ibexa\Rest\Output\Generator\InMemory\Xml\FieldTypeHashGenerator'

Ibexa\Rest\Output\Generator\Xml\FieldTypeHashGenerator:
Ibexa\Rest\Output\Generator\InMemory\Xml\FieldTypeHashGenerator:
arguments:
$normalizer: '@ibexa.rest.serializer'
$logger: '@logger'
Expand All @@ -358,8 +362,6 @@ services:
- { name: monolog.logger, channel: ibexa.rest }

# value objects visitors
Ibexa\Contracts\Rest\Output\ValueObjectVisitorDispatcher: ~

ibexa.rest.output.value_object_visitor.Exception.InvalidArgumentException:
class: Ibexa\Rest\Server\Output\ValueObjectVisitor\InvalidArgumentException
tags:
Expand Down Expand Up @@ -403,6 +405,38 @@ services:
arguments:
$parsingDispatcher: '@Ibexa\Contracts\Rest\Input\ParsingDispatcher'

ibexa.rest.serializer.encoder.json:
class: Symfony\Component\Serializer\Encoder\JsonEncoder
tags:
- ibexa.rest.serializer.encoder

ibexa.rest.serializer.encoder.xml:
class: Symfony\Component\Serializer\Encoder\XmlEncoder
tags:
- ibexa.rest.serializer.encoder

Ibexa\Contracts\Rest\Output\VisitorAdapterNormalizer:
arguments:
$valueObjectVisitorResolver: '@Ibexa\Contracts\Rest\Output\ValueObjectVisitorResolver'
tags:
- { name: ibexa.rest.serializer.normalizer, priority: -1000 }

Ibexa\Rest\Output\Normalizer\JsonObjectNormalizer:
tags:
- { name: ibexa.rest.serializer.normalizer, priority: -500 }

Ibexa\Rest\Output\Normalizer\ArrayListNormalizer:
tags:
- { name: ibexa.rest.serializer.normalizer, priority: -400 }

Ibexa\Rest\Output\Normalizer\ArrayObjectNormalizer:
tags:
- { name: ibexa.rest.serializer.normalizer, priority: -400 }

Ibexa\Contracts\Rest\Output\ValueObjectVisitorResolverInterface: '@Ibexa\Contracts\Rest\Output\ValueObjectVisitor'

Ibexa\Contracts\Rest\Output\ValueObjectVisitorResolver: ~

Ibexa\Contracts\Rest\Input\Parser\Query\SortClause\BaseSortClauseProcessor:
abstract: true
arguments:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

namespace Ibexa\Contracts\Rest\Output\Exceptions;

use Ibexa\Contracts\Rest\Output\Generator;
Expand Down
15 changes: 15 additions & 0 deletions src/contracts/Output/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,19 @@ protected function checkEnd($type, $data)
* @return mixed
*/
abstract public function serializeBool($boolValue);

public function getData(): object
{
throw new \LogicException(sprintf(
'%s does not maintain state',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH I don't understand this exception. What state and how does this relate to getData method?

static::class,
));
}

/**
* @param array<mixed> $data
*
* @return array<mixed>
*/
abstract public function getEncoderContext(array $data): array;
}
81 changes: 0 additions & 81 deletions src/contracts/Output/ValueObjectVisitorDispatcher.php

This file was deleted.

36 changes: 36 additions & 0 deletions src/contracts/Output/ValueObjectVisitorResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Rest\Output;

final class ValueObjectVisitorResolver implements ValueObjectVisitorResolverInterface
{
/** @var array<class-string, ValueObjectVisitor> */
private array $visitors;

/**
* @param class-string $visitedClassName
*/
public function addVisitor(string $visitedClassName, ValueObjectVisitor $visitor): void
{
$this->visitors[$visitedClassName] = $visitor;
}

public function resolveValueObjectVisitor(object $object): ?ValueObjectVisitor
{
$className = $object::class;

do {
if (isset($this->visitors[$className])) {
return $this->visitors[$className];
}
} while ($className = get_parent_class($className));

return null;
}
}
14 changes: 14 additions & 0 deletions src/contracts/Output/ValueObjectVisitorResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Rest\Output;

interface ValueObjectVisitorResolverInterface
{
public function resolveValueObjectVisitor(object $object): ?ValueObjectVisitor;
}
Loading
Loading